TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. The flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API. TensorFlow was originally developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization for the purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.

The TensorFlow API is composed of a set of Python modules that enable constructing and executing TensorFlow graphs. The tensorflow package provides access to the complete TensorFlow API from within R. Here’s a simple example of making up some data in two dimensions and then fitting a line to it:

```
library(tensorflow)
# Create 100 phony x, y data points, y = x * 0.1 + 0.3
x_data <- runif(100, min=0, max=1)
y_data <- x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y <- W * x_data + b
# Minimize the mean squared errors.
loss <- tf$reduce_mean((y - y_data) ^ 2)
optimizer <- tf$train$GradientDescentOptimizer(0.5)
train <- optimizer$minimize(loss)
# Launch the graph and initialize the variables.
sess = tf$Session()
sess$run(tf$global_variables_initializer())
# Fit the line (Learns best fit is W: 0.1, b: 0.3)
for (step in 1:201) {
sess$run(train)
if (step %% 20 == 0)
cat(step, "-", sess$run(W), sess$run(b), "\n")
}
```

The first part of this code builds the data flow graph. TensorFlow does not actually run any computation until the session is created and the `run`

function is called.

To whet your appetite further, we suggest you check out what a classical machine learning problem looks like in TensorFlow. In the land of neural networks the most “classic” classical problem is the MNIST handwritten digit classification. We offer two introductions here, one for machine learning newbies, and one for pros. If you’ve already trained dozens of MNIST models in other software packages, please take the red pill. If you’ve never even heard of MNIST, definitely take the blue pill. If you’re somewhere in between, we suggest skimming blue, then red.

Images licensed CC BY-SA 4.0; original by W. Carter

If you’re already sure you want to learn and install TensorFlow you can skip these and charge ahead. Don’t worry, you’ll still get to see MNIST – we’ll also use MNIST as an example in our technical tutorial where we elaborate on TensorFlow features.

First, install the main TensorFlow distribution from here:

https://www.tensorflow.org/get_started/os_setup.html#download-and-setup

If you install TensorFlow within a virtualenv environment you’ll need to be sure to use that same environment when loading the tensorflow R package (see below for details on how to do this).

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

`devtools::install_github("rstudio/tensorflow")`

Note that the tensorflow package depends on other packages which include native C/C++ code so it’s installation requires R Tools on Windows and Command Line Tools on OS X. If the package installation fails because of inability to compile then install the appropriate tools for your platform based on the links above and try again.

When it is loaded the tensorflow R package scans the system for the version of python where TensorFlow is installed. If automatic detection doesn’t work or if you want to exercise more control over which version(s) of python and TensorFlow are used you can specify an explicit `TENSORFLOW_PYTHON`

environment variable to force probing for TensorFlow within a specific version of python, for example:

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

You can also specify Python virtualenvs or Conda envs via the `use_python`

functions documented here.

You can verify that your installation is working correctly by running this script:

```
library(tensorflow)
sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)
```

The tensorflow package provides code completion and inline help for the TensorFlow API when running within the RStudio IDE. In order to take advantage of these features you should also install the latest release of RStudio (v 1.0 or higher).

Once you’ve installed the base TensorFlow system and the tensorflow R package, you will likely want work though the series of tutorials that cover TensorFlow basics:

These articles cover the core concepts of TensorFlow in more depth as well describe the details of using the TensorFlow API from R:

These articles provide more in depth treatments of various topics:

- Variables: Creation, Initialization, Saving, and Loading
- TensorFlow Mechanics 101
- TensorBoard: Visualizing Learning
- TensorBoard: Graph Visualization

Finally, to learn more about neural networks you might enjoy the TensorFlow playground, which lets you tinker with a neural network in your browser.