Prior to using the tensorflow R package you need to install a version of TensorFlow on your system. Below we describe how to install TensorFlow as well the various options available for customizing your installation.
Note that this article principally covers the use of the R
install_tensorflow() function, which provides an easy to use wrapper for the various steps required to install TensorFlow.
You can also choose to install TensorFlow manually (as described at https://www.tensorflow.org/install/). In that case the Custom Installation section covers how to arrange for the tensorflow R package to use the version you installed.
First, install the tensorflow R package from CRAN as follows:
Then, use the
install_tensorflow() function to install TensorFlow:
You can confirm that the installation succeeded with:
This will provide you with a default installation of TensorFlow suitable for use with the tensorflow R package. Read on if you want to learn about additional installation options, including installing a version of TensorFlow that takes advantage of Nvidia GPUs if you have the correct CUDA libraries installed.
TensorFlow is distributed as a Python package and so needs to be installed within a Python environment on your system. By default, the
install_tensorflow() function attempts to install TensorFlow within it’s own isolated Python environment (“r-tensorflow”).
These are the available methods and their behavior:
|auto||Automatically choose an appropriate default for the current platform.|
|virtualenv||Install into a Python virtual environment at
|conda||Install into an Anaconda Python environment named
|system||Install into the system Python environment|
The “virtualenv” and “conda” methods are available on Linux and OS X and the “conda” and “system” methods are available on Windows.
Note that the “virtualenv” method uses the system version of Python (i.e. one located at either
/usr/local/bin, in that order) rather than other versions of Python that may be installed. This is to ensure that the virtual environment is created with a version and configuration of Python that is widely tested with TensorFlow. If you want to install TensorFlow within another version of Python see the Custom Installation section below.
To override the default behavior use the
method parameter, for example:
CPU vs. GPU
TensorFlow can be configured to run on either CPUs or GPUs. The CPU version is much easier to install and configure so is the best starting place especially when you are first learning how to use TensorFlow. Here’s the guidance on CPU vs. GPU versions from the TensorFlow website:
TensorFlow with CPU support only. If your system does not have a NVIDIA® GPU, you must install this version. Note that this version of TensorFlow is typically much easier to install (typically, in 5 or 10 minutes), so even if you have an NVIDIA GPU, we recommend installing this version first.
TensorFlow with GPU support. TensorFlow programs typically run significantly faster on a GPU than on a CPU. Therefore, if your system has a NVIDIA® GPU meeting the prerequisites shown below and you need to run performance-critical applications, you should ultimately install this version.
install_tensorflow() installs the CPU version of TensorFlow.
If you want to install the GPU version please see the article on installing the GPU version.
install_tensorflow() install the latest release version of TensorFlow. You can override this behavior by specifying the
version parameter. For example:
Note that this should be a full
major.minor.patch version specification (rather than just major and minor versions).
You can install the nightly build of TensorFlow (CPU or GPU version) with:
You can install any other build of TensorFlow by specifying a URL to a TensorFlow binary. For example:
Nightly builds for different OSes and architectures are linked on the TensorFlow repository root page.
install_tensorflow() function is provided as a convenient way to get started, but is not required. If you have an existing installation of TensorFlow or just prefer your own custom installation that’s fine too.
The full instructions for installing TensorFlow on various platforms are here: https://www.tensorflow.org/install/. After installing, please refer to the sections below on locating TensorFlow and meeting additional dependencies to ensure that the tensorflow for R package functions correctly with your installation.
Once you’ve installed TensorFlow you need to ensure that the tensorflow for R package can find your installation. The package scans the system for various versions of Python, and also scans available virtual environments and conda environments, so in many cases things will just work without additional effort.
If the version of TensorFlow you installed is not found automatically, then you can use the following techniques to ensure that TensorFlow is located.
TENSORFLOW_PYTHON environment variable to force probing within a specific Python installation. For example:
Alternatively, call the
use_python family of configuration functions:
||Specify the path a specific Python binary.|
||Specify the directory containing a Python virtualenv.|
||Specify the name of a conda environment.|
Note that you can include multiple calls to the
use_ functions and all provided locations will be tried in the order they were specified.
There are some components of TensorFlow (e.g. the Keras library) which have dependencies on additional Python packages. The
install_tensorflow() function installs these dependencies automatically, however if you do a custom installation you should be sure to install them manually.
You can install the additional dependencies with the following command:
Note that binary installations of TensorFlow are provided for Windows, OS X, and Ubuntu 14.04 or higher. It’s possible that binary installations will work on other Linux variants but Ubuntu is the only platform tested and supported.
In particular, if you are running on RedHat or CentOS you will need to install from source then follow the instructions in the Custom Installation section to ensure that your installation of TensorFlow can be used with the tensorflow R package.