Now let’s start off with the implementation in R —

## Installing the Dependencies —

First of all we need to install **Keras **package for R from github which will include installing ‘Reticulate’ package for interface of Python in R and then ‘Tensorflow’ package.

#Installing Keras and Tensorflow at the Backend#You need to install Rtools for installing ‘reticulate’ package for using the above packahes in R.#The reticulate package provides an R interface to Python modules, classes, and functions#You can install Rtools for your R version here —#https://cran.r-project.org/bin/windows/Rtools/#installing 'devtools' package for installing Packages from githubinstall.packages('devtools')#installing kerasdevtools::install_github("rstudio/keras")

The above code will load K**eras** library from github and now we need to load the keras package .

#Loading the keras packagelibrary(keras)#The R interface to Keras uses TensorFlow as it’s underlying #computation engine.So we need to install Tensorflow engine

Now we need to install the Tensorflow Engine for R. By default RStudio loads the *CPU version of tensorflow*. Use the below command to download the** CPU version of tensorflow**.

**install_tensorflow()**

This will install and download the CPU version of Tensorflow which will do all the computations in the backend on the CPU.

For installing the *GPU** version of the Tensorflow* –

**install_tensorflow(gpu = T)**

For more on how to install Tensorflow in R refer this link-

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

- Multi-Layer Perceptrons
- Convoluted Neural Networks
- Recurrent Neural Networks
- Skip-Gram Models
- Use pre-trained models like VGG16, RESNET etc.
- Fine-tune the pre-trained models.

*Let us start with building a very simple CNN model and try to classify 10,000 32X32 cifar10 images .*

#loading keras librarylibrary(keras)#loading the keras inbuilt cifar10 dataset?dataset_cifar10#to see the help file for details of datasetcifar<-dataset_cifar10()

**It might take some time to download the dataset as it is around ~ 100 MB large . After loading the dataset , lets separate Training and Test Data .**

#TRAINING DATA

train_x<-cifar$train$x/255#convert a vector class to binary class matrix

#converting the target variable to once hot encoded vectors using #keras inbuilt function 'to_categorical()train_y<-to_categorical(cifar$train$y,num_classes = 10)#TEST DATA

test_x<-cifar$test$x/255test_y<-to_categorical(cifar$test$y,num_classes=10)#checking the dimentions

dim(train_x)cat("No of training samplest",dim(train_x)[[1]],"tNo of test samplest",dim(test_x)[[1]])

## Now let’s define and configure our CNN model’s Architecture —

#a linear stack of layersmodel<-keras_model_sequential()#configuring the Modelmodel %>%#defining a 2-D convolution layer

layer_conv_2d(filter=32,kernel_size=c(3,3),padding="same", input_shape=c(32,32,3) ) %>%

layer_activation("relu") %>%#another 2-D convolution layer

layer_conv_2d(filter=32 ,kernel_size=c(3,3)) %>% layer_activation("relu") %>%#Defining a Pooling layer which reduces the dimentions of the #features map and reduces the computational complexity of the modellayer_max_pooling_2d(pool_size=c(2,2)) %>%#dropout layer to avoid overfittinglayer_dropout(0.25) %>%layer_conv_2d(filter=32 , kernel_size=c(3,3),padding="same") %>% layer_activation("relu") %>% layer_conv_2d(filter=32,kernel_size=c(3,3) ) %>% layer_activation("relu") %>%#flatten the input

layer_max_pooling_2d(pool_size=c(2,2)) %>%

layer_dropout(0.25) %>%layer_flatten() %>%layer_dense(512) %>%

layer_activation("relu") %>%layer_dropout(0.5) %>%#output layer-10 classes-10 units#applying

layer_dense(10) %>%softmaxnonlinear activation function to the output layer #to calculate cross-entropylayer_activation("softmax")#for computing Probabilities of classes-"logit(log probabilities)

Now after Defining the Architecture of our CNN model we need to** Compile** and define the type of **Loss function** and a **Optimizer** for our Model which will do the Parameter Updates.

#Model's Optimizer#defining the type of optimizer-ADAM-Adaptive Momentum Estimationopt<-optimizer_adam( lr= 0.0001 , decay = 1e-6 )#lr-learning rate , decay - learning rate decay over each updatemodel %>%

compile(loss="categorical_crossentropy",

optimizer=opt,metrics = "accuracy")#Summary of the Model and its Architecturesummary(model)

Now after all this its time to Train our Model on the images-

#TRAINING PROCESS OF THE MODELdata_augmentation <- TRUEif(!data_augmentation) {

model %>% fit( train_x,train_y ,batch_size=32,

epochs=80,validation_data = list(test_x, test_y),

shuffle=TRUE)

}else {#Generating imagesgen_images <- image_data_generator(featurewise_center = TRUE,

featurewise_std_normalization = TRUE,

rotation_range = 20,

width_shift_range = 0.30,

height_shift_range = 0.30,

horizontal_flip = TRUE )#Fit image data generator internal statistics to some sample datagen_images %>% fit_image_data_generator(train_x)#Generates batches of augmented/normalized data from image data and #labels to visually see the generated images by the Modelmodel %>% fit_generator(

flow_images_from_data(train_x, train_y,gen_images,

batch_size=32,save_to_dir="F:/PROJECTS/CNNcifarimages/"),

steps_per_epoch=as.integer(50000/32),epochs = 80,

validation_data = list(test_x, test_y) )}#use save_to_dir argument to specify the directory to save the #images generated by the Model and to visually check the Model's #output and ability to classify images.

Now The above Model gave me a **accuracy of 86.667 %** on validation set after hours of Training. It took about 3–4 hours for the model to train and learn on my **i5 Notebook with 8Gb of RAM and 64-Bit 2.63 ghz** processor.

1. Machines Demonstrate Self-Awareness

2. Visual Music & Machine Learning Workshop for Kids

3. Part-of-Speech tagging tutorial with the Keras Deep Learning library

4. Artificial Intelligence Conference

You guys can reduce or increase the number of **epochs***( no of iterations over the training data)* or adjust the other Model’s parameters to converge fast and achieve different results ,depending on your computational power.

This was literally my first Deep learning Model in R , and trust me there was a constant smile and excitement on my face while watching the model train and run .

If this was your first Deep Learning model in R like me , I hope you guys liked and enjoyed it. With a simple code, we were able to classify images with ~**87 % accuracy** .

If you have already used keras deep learning library in Python, then you will find the syntax and structure of the keras library in R to be very similar to that in Python.

Actually what happens is the keras package in R creates a conda virtual environment and installs everything required to run keras in that environment.I am very excited to see data scientists building real life deep learning models in R.

*Developers and Software Engineers are still working on the Keras and Tensorflow packages in R and constantly improving the packages everyday by adding new features and solving issues . For more details on R interface to Keras visit-**https://github.com/rstudio/keras** and spend time on reading the documentation about the R functions used in the various Deep learning Models.*

*P.S — I had some issues while setting up and installing the above Deep learning packages in R due to some outdated R packages which i had to update , so be patient while installing and running the Deep learning Models.*

*If you encounter some errors and have any doubt while running the above code feel free to comment and contact me on anishsingh.walia2015@vit.ac.in .*

## Do check out another Deep learning Model implemented by me on MNIST data set below —

** Digit Recognition **on MNIST dataset in R using a simple Multi layer perception Model —