Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Deep Learning with R Cookbook

You're reading from   Deep Learning with R Cookbook Over 45 unique recipes to delve into neural network techniques using R 3.5.x

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781789805673
Length 328 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Swarna Gupta Swarna Gupta
Author Profile Icon Swarna Gupta
Swarna Gupta
Rehan Ali Ansari Rehan Ali Ansari
Author Profile Icon Rehan Ali Ansari
Rehan Ali Ansari
Dipayan Sarkar Dipayan Sarkar
Author Profile Icon Dipayan Sarkar
Dipayan Sarkar
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Understanding Neural Networks and Deep Neural Networks 2. Working with Convolutional Neural Networks FREE CHAPTER 3. Recurrent Neural Networks in Action 4. Implementing Autoencoders with Keras 5. Deep Generative Models 6. Handling Big Data Using Large-Scale Deep Learning 7. Working with Text and Audio for NLP 8. Deep Learning for Computer Vision 9. Implementing Reinforcement Learning 10. Other Books You May Enjoy

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime