A Minimal rTorch Book
Last update: Sun Oct 25 12:05:18 2020 -0500 (79503f6ee)
You need couple of things to get
Install Python Anaconda. Preferably, for 64-bits, and above Python 3.6+. I have successfully tested Anaconda under four different operating systems: Windows (Win10 and Windows Server 2008); macOS (Sierra, Mojave and Catalina); Linux (Debian, Fedora and Ubuntu); and lastly, Solaris 10. All these tests are required by CRAN.
Install the R package reticulate, which is the one that provides the connection between R and Python.
Install the stable version
rTorchfrom CRAN, or the latest version under development via GitHub.
Note. While it is not mandatory to have a previously created a
TorchVisionhave already been installed, it is another option if for some reason
reticulaterefuses to communicate with the conda environment. Keep in mind that you could also get the
rTorchconda environment installed directly from the
Rconsole, in very similar fashion as in R-TensorFlow does. Use the function
install_pytorch()to install a conda environment for PyTorch.
The rTorch package can be installed from CRAN or Github.
From GitHub, install
which will install rTorch from the
If you want to play with the latest rTorch version, then install it from the
develop branch, like this:
or clone with Git from the terminal with:
This will allow you to build
rTorch from source.
If your preference is installing an Anaconda environment first, these are the steps:
- Create a
condaenvironment from the terminal with:
- Activate the new environment with
- Install the
PyTorchrelated packages with:
The last part
-c pytorch specifies the stable conda channel to download the PyTorch packages. Your conda installation may not work if you don’t indicate the channel.
Now, you can load
rTorch in R or RStudio with:
I used the idea for automatic installation in the
tensorflow package for R, to create the function
rTorch::install_pytorch(). This function will allow you to install a
conda environment complete with all
PyTorch requirements plus the packages you specify. Example:
This is explained in more detailed in the rTorch package manual.
pandasare not really necessary for
rTorchto work, but I was asked if
pandascould work with
PyTorch. So, I decided to install them for testing and experimentation. They both work.