Tensors and Neural Networks with 'GPU' Acceleration

Provides functionality to define and train neural networks similar to 'PyTorch' by Paszke et al (2019) but written entirely in R using the 'libtorch' library. Also supports low-level tensor operations and 'GPU' acceleration.


torch

Lifecycle:experimental Test CRANstatus cuda Discord

Installation

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.

Examples

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

Simple Autograd Example

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} ]

Contributing

No matter your current skills it’s possible to contribute to torch development. See the contributing guide for more information.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("torch")

0.17.0 by Tomasz Kalinowski, 6 months ago


https://torch.mlverse.org/docs, https://github.com/mlverse/torch


Report a bug at https://github.com/mlverse/torch/issues


Browse source code at https://github.com/cran/torch


Authors: Tomasz Kalinowski [ctb, cre] , Daniel Falbel [aut, cph] , Javier Luraschi [aut] , Dmitriy Selivanov [ctb] , Athos Damiani [ctb] , Christophe Regouby [ctb] , Krzysztof Joachimiak [ctb] , Hamada S. Badr [ctb] , Sebastian Fischer [ctb] , Maximilian Pichler [ctb] , RStudio [cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports Rcpp, R6, withr, rlang, methods, utils, stats, bit64, magrittr, tools, coro, callr, cli, glue, desc, safetensors, jsonlite, scales

Suggests testthat, covr, knitr, rmarkdown, palmerpenguins, mvtnorm, numDeriv, katex

Linking to Rcpp

System requirements: LibTorch (https://pytorch.org/); Only x86_64 platforms are currently supported except for ARM system running macOS.


Imported by ARInfoLSTM, BKTR, DICErClust, EQRN, FINN, GARCHInfoLSTM, LBBNN, NNMoMo, PLNmodels, RGAN, Rhobots, SEMdeep, brulee, causalOT, chatterbox, circlus, cito, crumble, deepImp, diffuseR, engression, funcml, glmnetr, innsight, kindling, lambdaTS, luz, madgrad, n3d, pigauto, proteus, ria.test, rkaf, scDHA, shrinkGPR, sits, snap, spinner, survalis, survdnn, tabnet, temper, topicmodels.etm, torchMAUM, torchdatasets, torchopt, torchvision, torchvisionlib, unfold, whisper.

Depended on by mlr3torch.

Suggested by DistributionIV, GPUmatrix, MLwrap, ReSurv, SimplicialComplex, amorem, biomod2, bundle, butcher, codez, cudaverse, einops, fairGATE, fairGNN, genderBR, gofigR, mlr3automl, mlr3resampling, mlr3tuningspaces, neuralsbi, nn2poly, quanteda, riskutility, rtemis, safetensors, shapr, svrep, tabby, targets, timesift, tts.api, vetiver.


See at CRAN