torch from R!

A proof of concept for calling libtorch functions from R. API will change! Use at your own risk. Most libtorch’s functionality is not implemented here too.

Installation

Installation is very simple:

CPU

Sys.setenv(TORCH_HOME="/libtorch")
devtools::install_github("dfalbel/torch")

Code above will check whether libtorch is installed to TORCH_HOME dir. If not it will automatically download libtorch binaries from pytorch.org and unpack them to TORCH_HOME. After that it will install torch R package. If you don’t set the TORCH_HOME env var it will use /libtorch as default.

Alternatively you can provide URL for binaries download by adding setting the TORCH_BINARIES environment variable.

Note: The package will return std::bad_alloc errors (and they crash the R session) if compiled with recent versions of g++ (eg. the default version of Ubuntu Xenial - 5.4.0). It’s recommended to compile the package with g++-4.9. In order to do it:

And add the following to your .R/Makevars (usethis::edit_r_makevars()):

CXX=g++-4.9
CXX11=g++-4.9

You may need to reinstall the Rcpp package.

GPU

On Linux you can also install torch with CUDA 9.0 support (still very initial stage)

Install CUDA 9.0

  • follow these instructions and add necessary repositories
  • install cuda-9.0 - sudo apt-get install cuda-9-0
  • install cuDNN > 7 - follow the instructions here.

Install libtorch and torch R package

Sys.setenv(TORCH_BACKEND = "CUDA")
devtools::install_github("dfalbel/torch")

Example

Currently this package is only a prrof of concept and you can only create a Torch Tensor from an R object. And then convert back from a torch Tensor to an R object.

Simple Autograd Example

In the following snippet we let torch, using the autograd feature, calculate the derivatives:

Linear Regression

In the following example we are going to fit a linear regression from scratch using torch’s Autograd.

Note all methods that end with _ (eg. sub_), will modify the tensors in place.