torch can be installed from CRAN with:
install.packages("torch")
You can also install the development version with:
remotes::install_github("mlverse/torch")
At the first package load additional software will be installed. See also the full installation guide here.
You can create torch tensors from R objects with the torch_tensor
function and convert them back to R objects with as_array
.
library(torch)
x <- array(runif(8), dim = c(2, 2, 2))
y <- torch_tensor(x, dtype = torch_float64())
y
#> torch_tensor
#> (1,.,.) =
#> 0.6192 0.5800
#> 0.2488 0.3681
#>
#> (2,.,.) =
#> 0.0042 0.9206
#> 0.4388 0.5664
#> [ CPUDoubleType{2,2,2} ]
identical(x, as_array(y))
#> [1] TRUE
In the following snippet we let torch, using the autograd feature, calculate the derivatives:
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
#> torch_tensor
#> 2
#> [ CPUFloatType{1} ]
w$grad
#> torch_tensor
#> 1
#> [ CPUFloatType{1} ]
b$grad
#> torch_tensor
#> 1
#> [ CPUFloatType{1} ]
No matter your current skills it’s possible to contribute to torch
development. See the contributing
guide for more
information.
install.packages('torch')
device
probs
or
logits
(but not both).dataset
probs
or logits
(but not both).
Samples are binary (0 or 1). They take the value 1
with probability p
and 0
with probability 1 - p
.loc
and scale
.df
.
This is exactly equivalent to distr_gamma(alpha=0.5*df, beta=0.5)
concentration
and rate
.script_function
or script_module
previously saved with jit_save
script_function
to a pathscript_function
or script_module
in bytecode form,
to be loaded on a mobile devicescript_function
.n
-th power of a square matrix for an integer n
.X
to the system torch_tensordot(A, X) = B
.rate
, the rate parameter.torch_tensordot()
n
columns of a product of Householder matrices.nn_parameter
MaxPool1d
.padding_value
MaxPool3d
.MaxPool2d
.nn_silu()
for more information.indices
.torch_index_put
.