Training Feed Forward Neural Network(FFNN) on GPU — Beginners Guide | by Hargurjeet | MLearning.ai | Medium

Training Feed Forward Neural Network(FFNN) on GPU — Beginners Guide

Dataset — CIFAR 10

  1. What are GPU’s ?
    A GPU (Graphics Processing Unit) is a specialized processor with dedicated memory that conventionally perform floating point operations required for rendering graphics. In other words, it is a single-chip processor used for extensive Graphical and Mathematical computations which frees up CPU cycles for other jobs. GPU’s has more cores and are much faster than CPU.

About Dataset

The CIFAR-10 dataset (Canadian Institute For Advanced Research) is a collection of images that are commonly used to train machine learning and computer vision algorithms. It is one of the most widely used datasets for machine learning research. The CIFAR-10 dataset contains 60,000 32×32 color images in 10 different classes. The 10 different classes represent airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. There are 6,000 images of each class.

Table of contents

№1: Introduction

The CIFAR-10 dataset contains 60,000 32×32 color images in 10 different classes. CIFAR-10 is a set of images that can be used to teach a FFNN how to recognize objects. Since the images in CIFAR-10 are low-resolution (32×32), this dataset can allow researchers to quickly try different algorithms to see what works.

  1. Cars 🚗
  2. Birds 🐦
  3. Cats 😺
  4. Deer 🐆
  5. Dogs 🐶
  6. Frogs 🐸
  7. Horses 🐴
  8. Ships 🚢
  9. Trucks 🚚

№2: Data Pre Processing

Loading required libraries

Since we are using PyTorch to build the neural network. I import all the related library in single go.

Get Data

The dataset in available within the torch vision library. Alternately you can also access the dataset from Kaggle.

№3: Exploring the CIFAR10 dataset

Q: How many images does the training and testing dataset contain?

Q: How many output classes does the dataset contain?

Q: What is the shape of an image tensor from the dataset?

Q: Can you determine the number of images belonging to each class?

№4: Preparing the data for training

Splitting into training and validation sets

Before the training is started, It is important to split the data into training and testing set.

Visualizing a batch

Configuring the model

I write an accuracy function that calculate the model accuracy by comparing predicted class and the actual class label.

Moving the model to GPU

In this section, we will move our model to GPU.

№5: Training the model

We can make several attempts at training the model. Each time, try a different architecture and a different set of learning rates. Here are some ideas to try:

  • Increase of decrease the size of each hidden layer
  • Try different activation functions
  • Try training for different number of epochs
  • Try different learning rates in every epoch What’s the highest validation accuracy you can get to? Can you get to 50% accuracy? What about 60%?

№6: Testing with individual images

Loading the testing set from the Torchvision library

№7: Summary

Here is the brief summary of the article and step by step process we followed in training the FFNN on GPU.

  1. Downloaded the dataset from Torchvision library.
  2. We explored the dataset and tried understanding the overall images each class has, total images in training and validation set, etc.
  3. Data preparation before training
    i . Splitting into training and validation sets.
    ii. We visualized a batch of the dataset.
    iii. We did the basic model configurations like defining accuracy, evaluation, and fit function. We also defined the ImageClassificationBase class.
    iv. We checked the availability to GPU and moved the dataset to GPU if available else move it to CPU.
  4. We trained the model and achieved an accuracy of about 48 % and ran the same model on the test set.
  5. We randomly checked the model performance by running it on the few testing samples

№8: Future Work

  1. The model performance can future be improved if CNN architecture is implemented.
  2. Try implementing the other deep learning framework Tensorflow.
  3. Try improving the model performance by updating the number of layers, changing the optimizer and loss function in the FFNN.

№9: References

  1. You can access and execute the complete notebook on this link — https://jovian.ai/hargurjeet/cfar-10-dataset-6e9d9
  2. https://pytorch.org/
  3. https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html
  4. https://jovian.ai/learn/deep-learning-with-pytorch-zero-to-gans

Thanks for reading this article. Happy Learning 😃