Keras and TensorFlow in R

Contents

Introduction

It has always been a debatable topic to select between R and Python. The world of machine learning has been divided over the preference of one language over the other. But with the explosion of Deep Learning, balance shifted to Python, given that it had a huge list of deep learning libraries and frameworks that R lacked (Until now).

Personally, i switched to python from r simply because i wanted to dive into deep learning space, but with an R, it was almost impossible. But not anymore!

With the launch of Keras in R, this fight returns to the center. Python was slowly becoming the de facto language for deep learning models. But with the release of Keras library in R with tensorflow (CPU and GPU compatibility) in the backend from now on, R is likely to fight Python again for the podium even in the Deep Learning space.

Then, we will see how to install Keras with Tensorflow in R and build our first neural network model on classic MNIST dataset in RStudio.

Table of Contents

  1. Keras installation with tensorflow on the backend.
  2. Different types of models that can be built in R using Keras
  3. MNIST Handwritten Digit Classification Using an MLP in R
  4. Comparison of MNIST output with identical code in Python
  5. Final notes

1. Keras installation with tensorflow on the backend.

The steps to install Keras in RStudio are very simple. Just follow the steps below and it would be nice if you created your first neural network model in R.

install.packages("devtools")

devtools::install_github("rstudio/keras")

The above step will load the keras library from the GitHub repository. Now is the time to load keras in R and install tensorflow.

library(keras)

By default, RStudio loads the CPU version of tensorflow. Use the following command to download the CPU version of tensorflow.

install_tensorflow()

To install the single user GPU supported version of tensorflow / desktop system, use the following command.

install_tensorflow(gpu=TRUE)

For multi-user installation, see this installation guide.

Now that we have keras and tensorflow installed inside RStudio, let's get started and build our first neural network in R to solve the MNIST dataset.

2. Different types of models that can be built in R using keras

Below is the list of models that can be built in R using Keras.

  1. Multilayer Perceptrons
  2. Intricate neural networks
  3. Recurrent neural networks
  4. Modelos Skip-Gram
  5. Use previously trained models like VGG16, RESNET, etc.
  6. Fine-tune previously trained models.

Let's start with building a very simple MLP model using just a hidden layer to try and categorize the handwritten digits.

3. MNIST Handwritten Digit Classification Via MLP in R

#loading keras library
library(keras)

#loading the keras inbuilt mnist dataset
data<-dataset_mnist()

#separating train and test file
train_x<-data$train$x
train_y<-data$train$y
test_x<-data$test$x
test_y<-data$test$y

rm(data)

# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrix
train_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255
test_x <- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255

#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y<-to_categorical(train_y,10)
test_y<-to_categorical(test_y,10)

#defining a keras sequential model
model <- keras_model_sequential()

#defining the model with 1 input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer[10 neurons]
#i.e number of digits from 0 to 9

model %>%
layer_dense(units = 784, input_shape = 784) %>%
layer_dropout(rate=0.4)%>%
layer_activation(activation = 'relu') %>%
layer_dense(units = 10) %>%
layer_activation(activation = 'softmax')

#compiling the defined model with metric = accuracy and optimiser as adam.
model %>% compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics = c('accuracy')
)

#fitting the model on the training dataset
model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)

#Evaluating model on the cross validation dataset
loss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)

The code above had a training precision of 99,14 and a validation precision of 96,89. The code ran on my i5 processor and took around 13.5 seconds for a single epoch, while, on a TITANx GPU, the validation precision was 98.44 with an average time of 2 seconds.

4. MLP using hard – R vs Python

For the sake of comparison, furthermore i implemented the above MNIST problem in python. There should be no difference since keras in R creates a conda instance and runs keras on it. But still, you can find the identical python code below.

#importing the required libraries for the MLP model
import keras
from keras.models import Sequential
import numpy as np

#loading the MNIST dataset from keras
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

#reshaping the x_train, y_train, x_test and y_test to conform to MLP input and output dimensions
x_train=np.reshape(x_train,(x_train.shape[0],-1))/255
x_test=np.reshape(x_test,(x_test.shape[0],-1))/255

import pandas as pd
y_train=pd.get_dummies(y_train)
y_test=pd.get_dummies(y_test)

#performing one-hot encoding on target variables for train and test
y_train=np.array(y_train)
y_test=np.array(y_test)

#defining model with one input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer [10 #neurons]
model=Sequential()

from keras.layers import Dense

model.add(Dense(784, input_dim=784, activation='relu'))
keras.layers.core.Dropout(rate=0.4)
model.add(Dense(10,input_dim=784,activation='softmax'))

# compiling model using adam optimiser and accuracy as metric
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
# fitting model and performing validation

model.fit(x_train,y_train,epochs=50,batch_size=128,validation_data=(x_test,y_test))

The previous model achieved a validation precision of 98,42 on the same GPU. Then, as we initially assumed, the results are equals.

5. Final notes

If this was your first Deep Learning model in R, I hope you enjoyed it. With a very simple code, was able to categorize handwritten digits with an accuracy of the 98%. This should be motivation enough to start deep learning..

If you have already worked on keras deep learning library in Python, You will find that the syntax and structure of the keras library in R is very similar to Python. In reality, keras package in R creates a conda environment and installs everything essential to run keras in that environment. But now I'm more excited to see data scientists build real-life deep learning models in R. How do you say, competition should never stop. I would also like to hear your thoughts on this new development for R. Feel free to comment.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.