A Minimal rTorch Tutorial
2019-09-24
Prerequisites
You need two things to get rTorch
working:
Install Python Anaconda. Preferrably, for 64-bits, and above Python 3.6+.
Install
rTorch
from CRAN or GitHub.
Note. It is not mandatory to have a previously created
Python
environment withAnaconda
, wherePyTorch
andTorchVision
have already been installed. This step is optional. You could also get it installed directly from theR
console, in very similar fashion as in R-TensorFlow using the functioninstall_pytorch
.
This book is available online via GitHub Pages, or you can also build it from source from its repository.
Installation
rTorch
is available via CRAN or GitHub.
The rTorch package can be installed from CRAN or Github.
From CRAN:
From GitHub, install rTorch
with:
Python Anaconda
Before start running rTorch
, install a Python Anaconda environment first.
Example
Create a
conda
environment from the terminal withconda create -n myenv python=3.7
Activate the new environment with
conda activate myenv
Install the
PyTorch
related packages with:
conda install python=3.6.6 pytorch-cpu torchvision-cpu matplotlib pandas -c pytorch
The last part -c pytorch
specifies the conda channel to download the PyTorch packages. Your installation may not work if you don’t indicate the channel.
Now, you can load rTorch
in R or RStudio.
Automatic installation
I use the idea from automatic installation in r-tensorflow
, to create the function rTorch::install_pytorch()
. This function will allow you to install a conda
environment complete with all PyTorch
requirements.
Note.
matplotlib
andpandas
are not really necessary forrTorch
to work, but I was asked ifmatplotlib
orpandas
would work withPyTorch
. So, I decided to install them for testing and experimentation. They both work.