Installation

PyTorch installation

Installing Python and PyTorch

Although, Python and PyTorch can be installed directly from the R console, before start running rTorch, I would recommend installing PyTorch first in a new Python or Python-Anaconda environment. Then, testing if PyTorch and Torchvision packages are imported alright. The advantage of doing it this way is that you define in advanced the base Python or Anaconda version to install. Although the same can be done from rTorch you will need to get familiar passing parameters through one its functions.

If you opt to install PyTorch from R, rTorch has functions that could help you install PyTorch from the R console.

rTorch functions to install PyTorch

install_pytorch

This function is public and can be invoked with rTorch::install_pytorch().

This function will allow you to indicate (i) the Python version; (ii) the PyTorch version; (iii) the name of the conda environment; (iv) which channel (stable or nightly); (v) if you require CUDA (GPU) computation; (vi) additional packages such as matplotlib, pandas; (vii) more.

install_pytorch(
  method = c("conda", "virtualenv", "auto"),
  conda = "auto",
  version = "default",
  envname = "r-torch",
  extra_packages = NULL,
  restart_session = TRUE,
  conda_python_version = "3.6",
  pip = FALSE,
  channel = "stable",
  cuda_version = NULL,
  dry_run = FALSE,
  ...
)

Manual installation of PyTorch in a conda environment

If you prefer do it manually, use this example:

  1. Create a conda environment with conda create -n my-torch python=3.7 -y

  2. Activate the new environment with conda activate my-torch

  3. Inside the new environment, install PyTorch and related packages with:

conda install python=3.6 pytorch torchvision matplotlib pandas -c pytorch

Note: If you you don’t specify a version, conda will install the latest PyTorch. As of this writing (August-September 2020), the latest PyTorch version is 1.6.

Alternatively, you could create and install a conda environment a specific PyTorch version with:

conda create -n my-torch python=3.6 pytorch=1.3 torchvision matplotlib pandas -c pytorch -y

conda will resolve the dependencies and versions of the other packages automatically, or let you know your options.

Note. matplotlib and pandas are not really necessary, but I was asked if matplotlib or pandas would work in PyTorch. Then, I decided to put them for testing and experimentation. They both work.

Automatic Python detection

In rTorch there is an automatic detection of Python built in in the package that will ask you to install Miniconda first if you don’t have any Python installed in your machine. For instance, in macOS, Miniconda will be installed under PREFIX=/Users/user_name/Library/r-miniconda.

After Miniconda is installed, you could proceed to install the flavor or PyTorch you want, and the packages you want, with a command like this:

rTorch:::install_conda(package="pytorch=1.4", envname="r-torch", conda="auto", conda_python_version = "3.6", pip=FALSE, channel="pytorch", extra_packages=c("torchvision", "cpuonly", "matplotlib", "pandas"))

The command above will install the stable PyTorch 1.4 version on Python 3.6, including three additional packages: torchvision, cpuonly, matplotlib and pandas.

NOTE. My experience with Miniconda is spotty and not 100% reliable, specially in macOS. I would strongly recommend using full conda for your PyTorch installation.