---
title: "Installation"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Installation}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
## 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 testing **PyTorch** first in a new Python or Anaconda environment. Check if PyTorch and Torchvision packages are imported alright. The advantage of doing it this way is that you could have already in advance a PyTorch version that you are sure is working alright.
If you prefer to install PyTorch and its dependencies, that can be done from **rTorch** through one its functions `install_pytorch()`.
### 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.