Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Deep Learning with R Cookbook
  • Table Of Contents Toc
Deep Learning with R Cookbook

Deep Learning with R Cookbook

By : Gupta, Ansari, Sarkar
5 (3)
close
close
Deep Learning with R Cookbook

Deep Learning with R Cookbook

5 (3)
By: Gupta, Ansari, 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)
close
close

Introduction to convolutional operations

The generic architecture of CNN is comprised of convolutional layers followed by fully connected layers. Like other neural networks, a CNN also contains input, hidden and output layers, but it works by restructuring the data into tensors that consist of the image, and the width and height of the image. In CNN, each volume in one layer is connected only to a spatially relevant region in the next layer to ensure that when the number of layers increases, each neuron has a local influence on its specific location. A CNN may also contain pooling layers along with few fully connected layers.

The following is an example of a simple CNN with convolution and pooling layers. In this recipe, we will work with convolution layers. We will introduce the concept of pooling layers in the Getting familiar with pooling layers recipe of this chapter:

The convolution operation is an element-wise multiplication and summation between the input matrix and the filter used. Here is an example of a convolution operation:

Now we understand how a convolution layer works. Let's move on to building a convolutional neural network to classify clothing and accessories.

Getting ready

Let's start with importing the keras library:

library(keras)

In this recipe, we will work with the Fashion-MNIST dataset, which we can import directly from keras.

How to do it...

The Fashion-MNIST dataset contains images of 10 different types of items of clothing and accessories. It consists of 60,000 examples in the training set and 10,000 examples in the testing dataset. Each example is a 28 × 28 grayscale image, associated with a label from the 10 classes. 

  1. We import the Fashion-MNIST dataset in our environment:
fashion <- dataset_fashion_mnist()
x_train <- fashion$train$x
y_train <- fashion$train$y
x_test <- fashion$test$x
y_test <- fashion$test$y

We can check the dimensions of the train and test datasets using the commands:

dim(x_train)
dim(x_test)

Now let's take a look at the data for a sample image:

 x_test[1,,]

In the following screenshot, we can see that the sample image data is in the form of a matrix:

We check the label of the preceding screenshot using the following code:

paste("label of first image is:  " ,y_train[1])

In the following screenshot, we can see that the sample image belongs to class 9:

Now we define the label names for the different classes in the data:

label_names = c('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',  'Sandal',
'Shirt', 'Sneaker', 'Bag', 'Ankle boot')
If you are using the Jupyter notebook, you can use the repr library to set the plot window size using the following code: options(repr.plot.width=5, repr.plot.height=3)

Let's visualize a few sample images from the different classes:

# Visualize images

par(mfcol=c(3,3))
par(mar=c(2,2,2,2),xaxs = "i",yaxs = "i")
for (idx in 1:9) {
img <- x_train[idx,,]
img <- t(apply(img, 2, rev))
image(1:28,1:28,img, main=paste(label_names[y_train[idx]+1]),xaxt = 'n',yaxt = 'n',col= gray((0:255)/255))
}

In the following screenshot, we can see sample images along with their label names:

  1. Next, we reshape the data, normalize it, and convert the target label to a binary class matrix:
# Resize the shape of inputs
x_train <- array_reshape(x_train, c(nrow(x_train), 28, 28, 1))
x_test <- array_reshape(x_test, c(nrow(x_test), 28, 28, 1))

# Transform RGB values into [0,1] range
x_train <- x_train / 255
x_test <- x_test / 255

# Convert class vectors to binary class matrices
y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)
  1. Once we are done with data preparation, we build, compile, and train our CNN model:
# Define model
cnn_model <- keras_model_sequential() %>%
layer_conv_2d(filters = 8, kernel_size = c(4,4), activation = 'relu',
input_shape = c(28,28,1)) %>%
layer_conv_2d(filters = 16, kernel_size = c(3,3), activation = 'relu') %>%
layer_flatten() %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax')

Let's look at the summary of the model:

cnn_model %>% summary()

The following screenshot shows the details about the model:

Before compiling the model, let's define its loss function:

loss_entropy <- function(y_pred, y_true) {
loss_categorical_crossentropy(y_pred, y_true)
}

Now we compile the model:

# Compile model
cnn_model %>% compile(
loss = loss_entropy,
optimizer = optimizer_sgd(),
metrics = c('accuracy')
)

After compiling the model, we train it with a batch size of 128, number of epochs set to 5, and a validation split of 20%:

# train the model
cnn_model %>% fit(
x_train, y_train,
batch_size = 128,
epochs = 5,
validation_split = 0.2
)
  1. Finally, we evaluate the performance of the trained model and print the evaluation metrics:
scores <- cnn_model %>% evaluate(x_test,
y_test,
verbose = 0
)
# Output metrics
paste('Test loss:', scores[[1]])
paste('Test accuracy:', scores[[2]])

In the following screenshot, we can see the evaluation metrics of the model on test data:

Once we are satisfied with the model's accuracy, we can use it for predicting classes for the testing dataset:

#prediction
predicted_label <- cnn_model %>% predict_classes(x_test)

In the preceding screenshot, we can see our model achieves a good accuracy of 81.63% on test data. 

How it works...

The keras library in R provides various datasets, using which we can develop deep learning models. In step 1, we imported the Fashion-MNIST dataset, using the dataset_fashion_mnist() function and checked the dimensions of its training and testing partitions. We also looked at the data and label of a sample image. Next, we defined the label names for the data and visualized one sample image for each label. 

Base R graphics provide functions to plot interesting plots. The par() function is used to set various graphical parameters, and the image() function creates a grid of colored or grayscale rectangles with colors corresponding to the values in the image matrix.

In step 2, we reshaped our data and normalized it within the range of 0 to 1. We also one-hot encoded the target label matrix using the to_categorical() function. After we completed the data preparation, in step 3, we configured our CNN model and looked at its summary. In the model configuration, we added two convolutional layers with 8 and 16 filters of sizes 4 × 4 and 3 × 3 respectively, and each layer used the ReLU activation function.

Next, we used layer_flatten() to convert the output matrix of the convolutional layer into a linear array, which gets fed as an input into the nodes of our dense neural network. Our dense network contained a hidden layer with 16 units and an output layer with 10 units because we had 10 target labels. Next, we looked at the summary of the model; it gave us the information about output shape and number of parameters in each layer. The following are the formulas to calculate these for the convolutional layer:

  • The output shape of each layer: If the input to our convolutional layer is  and we apply  filters of , then the output shape is given by the following formula:
  • The number of parameters in each layer can be calculated by the following formula: 

After the model configuration, we compiled the model using a stochastic gradient descent optimizer and a categorical cross-entropy loss function. Next, we trained the model. Lastly, in step 4, we evaluated the performance of the model on the testing dataset and printed the evaluation metrics and generated predictions for the testing dataset.

There's more...

Until now, we have predominantly used two-dimensional convolutional layers. Apart from two-dimensional convolutions, there are one-dimensional and three-dimensional implementations of CNNs, depending upon the type of input data used.

One-dimensional CNNs are widely used for textual data analysis, for example, classifying customer reviews. Unlike images, which are mostly two-dimensional in nature, text data has one-dimensional input data. You can refer to the following example for one-dimensional convolutions, available at https://keras.rstudio.com/articles/examples/imdb_cnn.html here.

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Deep Learning with R Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon