Book Image

Deep Learning with R Cookbook

By : Swarna Gupta, Rehan Ali Ansari, Dipayan Sarkar
Book Image

Deep Learning with R Cookbook

By: Swarna Gupta, Rehan Ali Ansari, Dipayan Sarkar

Overview of this book

Deep learning (DL) has evolved in recent years with developments such as generative adversarial networks (GANs), variational autoencoders (VAEs), and deep reinforcement learning. This book will get you up and running with R 3.5.x to help you implement DL techniques. The book starts with the various DL techniques that you can implement in your apps. A unique set of recipes will help you solve binomial and multinomial classification problems, and perform regression and hyperparameter optimization. To help you gain hands-on experience of concepts, the book features recipes for implementing convolutional neural networks (CNNs), recurrent neural networks (RNNs), and Long short-term memory (LSTMs) networks, as well as sequence-to-sequence models and reinforcement learning. You’ll then learn about high-performance computation using GPUs, along with learning about parallel computation capabilities in R. Later, you’ll explore libraries, such as MXNet, that are designed for GPU computing and state-of-the-art DL. Finally, you’ll discover how to solve different problems in NLP, object detection, and action identification, before understanding how to use pre-trained models in DL apps. By the end of this book, you’ll have comprehensive knowledge of DL and DL packages, and be able to develop effective solutions for different DL problems.
Table of Contents (11 chapters)

TensorFlow Core API

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.

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.

The 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:

  1. 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)
  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)))
  1. Now, let's define the model:
y_hat <- W * x_data + b
  1. 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)
  1. Next, we launch the computation graph and initialize the TensorFlow variables:
sess = tf$Session()
sess$run(tf$global_variables_initializer())
  1. 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:

It is important that we close the session because resources are not released until we close it.

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.

It is suggested that you use one of the higher-level APIs (Keras or Estimator) rather than the lower-level core TensorFlow API.