Quick start

Prior to using the tensorflow R package you need to install a version of Python and TensorFlow on your system. Below we describe how to install to do this 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.

TensorFlow is tested and supported on the following 64-bit systems:

Installation

First, install the tensorflow R package from GitHub as follows:

# install.packages("remotes")
remotes::install_github("rstudio/tensorflow")

Next, make sure Python is installed on your system. You can download from www.python.org/downloads, or call the install_python() from reticulate:

reticulate::install_python()

Finally, call install_tensorflow() function to install TensorFlow. This will automatically create an isolated virtual environment named “r-tensorflow”

library(tensorflow)
install_tensorflow(envname = "r-tensorflow")

You can also use keras::install_keras(), which installs TensorFlow, in addition to some commonly used packages like “scipy”, “pandas”, and “tensorflow-datasets”.

install.packages("keras")
library(keras)
install_keras()

You can confirm that the installation succeeded with:

library(tensorflow)
Warning in value[[3L]](cond): 'r-tensorflow-website' virtual environment not found.
Please run ./_utils/create-r-tensorflow-webiste-venv.R
tf$constant("Hello TensorFlow!")
tf.Tensor(b'Hello TensorFlow!', shape=(), dtype=string)

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.

Installation methods

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 an isolated Python environment (“r-tensorflow”).

These are the available methods and their behavior:

Method Description
auto Automatically choose an appropriate default for the current platform.
virtualenv Install into a Python virtual environment
conda Install into an Anaconda Python environment

Be aware that install_tensorflow() will intentionally not install into a system Python installation (e.g., /usr/bin/python).

install_tensorflow() is a wrapper around reticulate::py_install(). Please refer to ‘Installing Python Packages’ for more information.

Alternate Versions

By default, install_tensorflow() install the latest release version of TensorFlow. You can override this behavior by specifying the version parameter. For example:

install_tensorflow(version = "2.10")

You can provide a full major.minor.patch version specification, or just a major.minor specification, in which case the latest patch is automatically selected.

The default tensorflow package is GPU capable on macOS and Linux. However, if you will not be using a GPU, you can install the smaller cpu-only package like this:

install_tensorflow(version = "cpu")

You can install the nightly build of TensorFlow (CPU or GPU version) with:

install_tensorflow(version = "nightly")      # cpu+gpu version
install_tensorflow(version = "nightly-cpu")  # cpu version

You can install any other build of TensorFlow by specifying a URL to a TensorFlow binary. For example:

install_tensorflow(version = "https://files.pythonhosted.org/packages/c2/c1/a035e377cf5a5b90eff27f096448fa5c5a90cbcf13b7eb0673df888f2c2d/tf_nightly-1.12.0.dev20180918-cp36-cp36m-manylinux1_x86_64.whl")