The TensorFlow Core API is a set of modules written in Python. It is a system where computations are represented as graphs. The R `tensorflow`** **package provides complete access to the TensorFlow API from R. TensorFlow represents computations as a data flow graph, where each node represents a mathematical operation, and directed arcs represent a multidimensional data array or tensor that operations are performed on. In this recipe, we'll build and train a model using the R interface for the TensorFlow Core API.

# TensorFlow Core API

# Getting ready

You will need the `tensorflow` library installed to continue with this recipe. You can install it using the following command:

install.packages("tensorflow")

After installing the package, load it into your environment:

library(tensorflow)

Executing the two preceding code blocks doesn't install `tensorflow` completely. Here, we need to use the `install_tensorflow()` function to install TensorFlow, as shown in the following code block:

install_tensorflow()

TensorFlow installation in R needs a Python environment with the `tensorflow` library installed in it. The `install_tensorflow()` function attempts to create an isolated python environment called `r-tensorflow` by default and installs `tensorflow` in it. It takes different values for the `method` argument, which provides various installation behaviors. These methods are explained at the following link: https://tensorflow.rstudio.com/tensorflow/articles/installation.html#installation-methods.

`virtualenv`and

`conda`methods are available on Linux and OS X, while the

`conda`and

`system`methods are available on Windows.

# How to do it...

After the initial installation and setup, we can start building deep learning models by simply loading the TensorFlow library into the R environment:

- Let's start by simulating some dummy data:

x_data = matrix(runif(1000*2),nrow = 1000,ncol = 1)

y_data = matrix(runif(1000),nrow = 1000,ncol = 1)

- Now, we need to initialize some TensorFlow variables; that is, the weights and biases:

W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))

b <- tf$Variable(tf$zeros(shape(1L)))

- Now, let's define the model:

y_hat <- W * x_data + b

- Then, we need to define the loss function and optimizer:

loss <- tf$reduce_mean((y_hat - y_data) ^ 2)

optimizer <- tf$train$GradientDescentOptimizer(0.5)

train <- optimizer$minimize(loss)

- Next, we launch the computation graph and initialize the TensorFlow variables:

sess = tf$Session()

sess$run(tf$global_variables_initializer())

- We train the model to fit the training data:

for (step in 1:201) {

sess$run(train)

if (step %% 20 == 0)

cat(step, "-", sess$run(W), sess$run(b), "\n")

}

Finally, we close the session:

sess$close()

Here are the results of every 20th iteration:

# How it works...

TensorFlow programs generate a computation graph in which the nodes of the graph are called *ops*. These ops take tensors as input and perform computations and produce tensors (tensors are n-dimensional arrays or lists). TensorFlow programs are structured in two phases: the construction phase and the execution phase. In the construction phase, we assemble the graph, while in the execution phase, we execute the graphs in the context of the session. Calling the `tensorflow` package in R creates an entry point (the `tf` object) to the TensorFlow API, through which we can access the main TensorFlow module. The `tf$Variable()` function creates a variable that holds and updates trainable parameters. TensorFlow variables are in-memory buffers containing tensors.

In *step 1*, we created some dummy data. In the next step, we created two `tf` variables for the weights and bias with initial values. In *step 3*, we defined the model. In *step 4*, we defined the loss function, as per the following equation:

The `reduce_mean()` function computes the mean of the elements across the dimensions of a tensor. In our code, it calculates the average loss over the training set. In this step, we also defined the optimization algorithm we need to use to train our network. Here, we used the gradient descent optimizer with a learning rate of 0.5. Then, we defined the objective of each training step; that is, we minimize loss.

In *step 5*, we assembled the computation graph and provided TensorFlow with a description of the computations that we wanted to execute. In our implementation, we wanted TensorFlow to minimize loss; that is, minimize the mean squared error using the gradient descent algorithm. TensorFlow does not run any computations until the session is created and the `run()` function is called. We launched the session and added an ops (node) in order to run some `tf` variable initializers. `sessrun(tfrun(tfglobal_variables_initializer())` initializes all the variables simultaneously. We should only run this ops after we have fully constructed the graph and launched it in a session. Finally, in the last step, we executed the training steps in a loop and printed the `tf` variables (the weight and bias) at each iteration.