## Installing TensorFlow in R

This section will cover another very popular open source machine learning package, TensorFlow, which is very effective in building deep learning models.

### Getting ready

TensorFlow is another open source library developed by the Google Brain Team to build numerical computation models using data flow graphs. The core of TensorFlow was developed in C++ with the wrapper in Python. The `tensorflow`

package in R gives you access to the TensorFlow API composed of Python modules to execute computation models. TensorFlow supports both CPU- and GPU-based computations.

The `tensorflow`

package in R calls the Python tensorflow API for execution, which is essential to install the `tensorflow`

package in both R and Python to make R work. The following are the dependencies for `tensorflow`

:

- Python 2.7 / 3.x
- R (>3.2)
- devtools package in R for installing TensorFlow from GitHub
- TensorFlow in Python
- pip

### How to do it...

- Once all the mentioned dependencies are installed,
`tensorflow`

can be installed from`devtools`

directly using the`install_github`

command as follows:

devtools::install_github("rstudio/tensorflow")

- Before loading
`tensorflow`

in R, you need to set up the path for Python as the system environment variable. This can be done directly from the R environment, as shown in the following command:

Sys.setenv(TENSORFLOW_PYTHON="/usr/bin/python") library(tensorflow)

If the Python `tensorflow`

module is not installed, R will give the following error:

Error raised by R if tensorflow in Python is not installed

`tensorflow`

in Python can be installed using `pip`

:

pip install tensorflow # Python 2.7 with no GPU support pip3 install tensorflow # Python 3.x with no GPU support pip install tensorflow-gpu # Python 2.7 with GPU support pip3 install tensorflow-gpu # Python 3.x with GPU support

### How it works...

TensorFlow follows directed graph philosophy to set up computational models where mathematical operations are represented as nodes with each node supporting multiple input and output while the edges represent the communication of data between nodes. There are also edges known as **control dependencies** in TensorFlow that do not represent the data flow; rather the provide information related to control dependence such as node for the control dependence must finish processing before the destination node of control dependence starts executing.

An example TensorFlow graph for logistic regression scoring is shown in the following diagram:

TensorFlow graph for logistic regression

The preceding figure illustrates the TensorFlow graph to score logistic regression with optimized weights:

The *MatMul* node performs matrix multiplication between input feature matrix *X* and optimized weight β. The constant *C* is then added to the output from the *MatMul* node. The output from *Add* is then transformed using the *Sigmoid* function to output *Pr(y=1|X)*.

### See also

Get started with TensorFlow in R using resources at https://rstudio.github.io/tensorflow/.