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 Estimator API

The Estimator is a high-level TensorFlow API that makes developing deep learning models much more manageable since you can use them to write models with high-level intuitive code. It builds a computation graph and provides an environment where we can initialize variables, load data, handle exceptions, and create checkpoints.

The tfestimators package is an R interface for the TensorFlow Estimator API. It implements various components of the TensorFlow Estimator API in R, as well as many pre-built canned models, such as linear models and deep neural networks (DNN classifiers and regressors). These are called pre-made estimators. The Estimator API does not have a direct implementation of recurrent neural networks or convolutional neural networks but supports a flexible framework for defining arbitrary new model types. This is known as the custom estimators framework.

Getting ready

In this recipe, we'll demonstrate how to build and fit a deep learning model using the Estimator API. To use the Estimator API in R, we need to install the tfestimators package.

First, let's install the library and then import it into our environment:

install.packages("tfestimators")
library(tfestimators)

Next, we need to simulate some dummy data for this exercise:

x_data_df <- as.data.frame( matrix(rnorm(1000*784), nrow = 1000, ncol = 784))
y_data_df <- as.data.frame(matrix(rnorm(1000), nrow = 1000, ncol = 1))

Let's rename the response variable to target:

colnames(y_data_df)<- c("target")

Now, let's bind the x and y data together to prepare the training data:

dummy_data_estimator <- cbind(x_data_df,y_data_df)

By doing this, we have created our input dataset.

How to do it...

In this recipe, we will use a pre-made dnn_regressor estimator. Let's get started and build and train a deep learning estimator model:

  1. We need to execute some steps before building an estimator neural network. First, we need to create a vector of feature names:
features_set <- setdiff(names(dummy_data_estimator), "target")

Here, we construct the feature columns according to the Estimator API. The feature_columns() function is a constructor for feature columns, which defines the expected shape of the input to the model:

feature_cols <- feature_columns(
column_numeric(features_set)
)
  1. Next, we define an input function so that we can select feature and response variables:
estimator_input_fn <- function(data_,num_epochs = 1) {
input_fn(data_, features = features_set, response = "target",num_epochs = num_epochs )
}
  1. Let's construct an estimator regressor model:
regressor <- dnn_regressor(
feature_columns = feature_cols,
hidden_units = c(5, 10, 8),
label_dimension = 1L,
activation_fn = "relu"
)
  1. Next, we need to train the regressor we built in the previous step:
train(regressor, input_fn = estimator_input_fn(data_ = dummy_data_estimator))
  1. Similar to what we did for the training data, we need to simulate some test data and evaluate the model's performance:
x_data_test_df <- as.data.frame( matrix(rnorm(100*784), nrow = 100, ncol = 784))
y_data_test_df <- as.data.frame(matrix(rnorm(100), nrow = 100, ncol = 1))

We need to change the column name of the response variable, just like we did for the training data:

colnames(y_data_test_df)<- c("target")

We bind the x and y data together for the test data:

dummy_data_test_df <- cbind(x_data_test_df,y_data_test_df)

Now, we generate predictions for the test dataset using the regressor model we built previously:

predictions <- predict(regressor, input_fn = estimator_input_fn(dummy_data_test_df), predict_keys = c("predictions"))

Next, we evaluate the model's performance on the test dataset:

evaluation <- evaluate(regressor, input_fn = estimator_input_fn(dummy_data_test_df))
evaluation

In the next section, you will gain a comprehensive understanding of the steps we implemented here.

How it works...

In this recipe, we implemented a DNN regressor using a pre-made estimator; the preceding program can be divided into a variety of subparts, as follows:

  • Define the feature columns: In step 1, we created a vector of strings, which contains the names of our numeric feature columns. Next, we called the feature_columns() function, which defines the expected shape value of an input tensor and how features should be transformed (numeric or categorical) while they're being modeled. In our case, the shape of the input tensor was 784, and all the values of the input tensor were numerical. We transform the numeric features by providing the names of the numeric columns to the column_numeric() function within feature_columns(). If you have categorical columns in your data that have values such as category_x, category_y and you want to assign integer values (0, 1) to these, you can do this using the column_categorical_with_identity() function.
  • Write the dataset import functions: In step 2, we defined how a pre-made estimator receives data. It is defined by the input_fn() function. It converts raw data sources into tensors and selects feature and response columns. It also configures how data is drawn during training; that is, shuffling, batch size, epochs, and so on.
  • Instantiate the relevant pre-made Estimator: In step 3, we instantiated a pre-made deep neural network (DNN) estimator by calling dnn_regressor(). The hidden_units argument value of the function defines our network; that is, the hidden layers in the network and the number of perceptrons in each layer. It consists of dense, feedforward neural network layers. It takes vectors of integers as the argument value. In our model, we had three layers with 5, 10, and 8 perceptrons, respectively. We used relu as our activation function. The label_dimension argument of the dnn_regrssor function defines the shape of the regression target per example.
  • Call a training, evaluation method: In step 4, we trained our model, and in the next step, we predicted the values for the test dataset and evaluated the performance of the model.

There's more...

Estimators provide a utility called run hooks so that we can track training, report progress, request early stopping, and more. One such utility is the hook_history_saver() function, which lets us save training history in every training step. While training an estimator, we pass our run hooks' definition to the hooks argument of the train() function, as shown in the following code block. It saves model progress after every two training iterations and returns saved training metrics. 

The following code block shows how to implement run hooks:

training_history <- train(regressor,
input_fn = estimator_input_fn(data_ = dummy_data_estimator),
hooks = list(hook_history_saver(every_n_step = 2))
)

Other pre-built run hooks are provided by the Estimator API. To find out more about them, please refer to the links in the See also section of this recipe.

See also