https://github.com/mlverse/torch
R Interface to Torch
https://github.com/mlverse/torch
autograd deep-learning r torch
Last synced: about 2 months ago
JSON representation
R Interface to Torch
- Host: GitHub
- URL: https://github.com/mlverse/torch
- Owner: mlverse
- License: other
- Created: 2020-01-07T14:56:32.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T13:51:54.000Z (3 months ago)
- Last Synced: 2025-04-13T06:14:57.424Z (3 months ago)
- Topics: autograd, deep-learning, r, torch
- Language: C++
- Homepage: https://torch.mlverse.org
- Size: 137 MB
- Stars: 525
- Watchers: 18
- Forks: 79
- Open Issues: 114
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
torch::torch_manual_seed(1)
```[](https://lifecycle.r-lib.org/articles/stages.html)
[](https://github.com/mlverse/torch/actions/workflows/main.yaml)
[](https://CRAN.R-project.org/package=torch)
[](https://cran.r-project.org/package=torch)
[](https://discord.com/invite/s3D5cKhBkx)## Installation
torch can be installed from CRAN with:
```r
install.packages("torch")
```You can also install the development version with:
```r
remotes::install_github("mlverse/torch")
```At the first package load additional software will be
installed. See also the full [installation guide](https://torch.mlverse.org/docs/articles/installation.html) here.## Examples
You can create torch tensors from R objects with the `torch_tensor` function and convert them back to R objects with `as_array`.
```{r}
library(torch)
x <- array(runif(8), dim = c(2, 2, 2))
y <- torch_tensor(x, dtype = torch_float64())
y
identical(x, as_array(y))
```### Simple Autograd Example
In the following snippet we let torch, using the autograd feature, calculate the derivatives:
```{r}
x <- torch_tensor(1, requires_grad = TRUE)
w <- torch_tensor(2, requires_grad = TRUE)
b <- torch_tensor(3, requires_grad = TRUE)
y <- w * x + b
y$backward()
x$grad
w$grad
b$grad
```## Contributing
No matter your current skills it's possible to contribute to `torch` development.
See the [contributing guide](https://torch.mlverse.org/docs/contributing) for more information.