**Table of Contents**

- Recurrent neural network example
- Video Explanation

**1. Recurrent neural network example**

This topic is based on implementing the recurrent neural network on an image dataset.

- So here we are going to use the MNIST dataset, that we have used to demonstrate the value. Here we going to show the fact that RNN can also be used to learn features from the image datasets. So the format present in the dataset is a bit different so let’s begin with the coding part:
- To implement the recurrent neural network in Pytorch you have to import torch, torch.nn, torchvision.transform and torchvision.datasets is to import the dataset to our system.
- Then we are calling dataset.MNIST to download the training dataset and testing dataset.
- As you know from the previous content, the training dataset contains 60,000 images and test datasets of 10,000 images and each image has a size of 28 x 28. This information can be used further while implementing the recurrent neural network using image datasets.
- The batch size is 100, the number of iteration is 3000. The concept here is to iterate the whole dataset.
- In 3000 iterations we define the number of epochs as shown in figure 1.
- The whole training dataset is being divided by the batch size that will be 600.
- If the total number of iteration is 3000 so the number of epochs will be 5.
- So here we are covering the whole dataset 5 times in a series of 3000 iterations. Here we have also called the train and test loaders.

Lets see how to build the basic RNN module:

- To build an RNN, we have given it a name as RNN model and then inherit nn.module and it has all the properties of deep learning in Pytorch.
- Inside the init function we have called the input dimensions, hidden dimensions, layer dimensions, and output dimensions.
- Then we call the constructor function which is basically the super function, which calls itself.
- Then we specify the layer dimension, hidden dimension, and finally the nn.RNN method in Pytorch. It has all the components which we have called in the init function.

- The batch first should be always true, make it true.
- Then the non-linearity, which is basically the activation function that we are using in RNN.
- Here in figure 2, you can see that we have mentioned as tanh. Tanh is one of the very good activation function, instead of tanh, we can also use Relu function, that means rectified Linear unit.
- Then the last layer is the linear layer which includes the method of hidden dimension and the output dimensions. As we know have 10 output dimensions as we have 10 classes.
- In the forward pass, we have mentioned before that we feed the hidden layer from the previous RNN module to the next RNN module
- In figure 3, you can see the h0 which is the RNN input hidden layer units are set to 0, and I have called torch.zeros, then it has a layer dimension and then hidden dimension.
- Then now we are initializing the hidden layers to zeros and then in the figure you can see out, hn is helpful for back-propagation. As we have seen before we back propagate RNN as we have to back-propagate for every RNN module in the whole RNN system.

- Each of the RNN modules stands for one time instances, so that means it is back-propagation over time.
- When we perform back propagation over time, there is a tendency for our gradients to either explode or vanish. To prevent that we use h0.detach(). and the whole function will take care of the output. And the input to the next layer, which is also covered here.
- Then we can see that in figure 5, the input dimension is 28, the hidden dimension is 100 and the layer dimension is 1 and then we have the output dimension is 10.
- Then we define the CUDA device.
- Then the loss function is a cross-entropy loss.
- Then the learning rate is 0.1.
- The optimizer used is the SGD which will be sufficient in this case.
- The sequence dimension is 28 as we have mentioned earlier the input dimension is 28, you can think of the sequence dimension as each time instance, we are inserting one row of 28 x 28 images. So by doing it we can cover 748 pixels. At each time instance, we are inserting 28 pixels of only one image and that is being done for 400 images.
- After this, we train our model which we ran and we can see that the final accuracy is 85 percent which is really high.

**Video Explanation**