Fruit Classification Using FeedForward Neural Networks In PyTorch

Original article was published on Deep Learning on Medium

Fruit Classification Using FeedForward Neural Networks In PyTorch

* So let’s first understand what basically PyTorch is:

PyTorch is an open source machine learning framework that fast tracks the path from research prototyping to production deployment. It was primarily developed by Facebook’s artificial intelligence research group.

PyTorch is predominantly used to implement various neural network architectures like recurrent neural networks (RNNs), convolution neural networks (CNNs), long term short memory (LSTM), and other similar high-level neural networks.

* Now’s lets understand FeedForward Neural Networks in detail:

The feedforward neural network is a specific type of early artificial neural network known for its simplicity of design. The feedforward neural network has an input layer, hidden layers and an output layer. Information always travels in one direction — from the input layer to the output layer — and never goes backward.

The feedforward neural network, as a primary example of neural network design, has a limited architecture. Signals go from an input layer to additional layers.

  • Input Layer: Input variables, sometimes called the visible layer.
  • Hidden Layers: Layers of nodes between the input and output layers. There may be one or more of these layers.
  • Output Layer: A layer of nodes that produce the output variables.

Now’s lets start our fruit classifier project:

There are two folder in the data set ,one for training and other is for testing.Both training data set and testing data set contains images of different classes of fruits in separate folders .

We can use the ImageFolder class from torchvision to load the data as PyTorch tensor.

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram etc.

* Training and Validation Datasets:

While building real world machine learning models, it is quite common to split the dataset into 3 parts:

Training set — used to train the model i.e. compute the loss and adjust the weights of the model using gradient descent.

Validation set — used to evaluate the model while training, adjust hyperparameters (learning rate etc.) and pick the best version of the model.

Test set — used to compare different models, or different types of modeling approaches, and report the final accuracy of the model.

Since there’s no predefined validation set, we can set aside a small portion of the training set to be used as the validation set. We’ll use the random_split helper method from PyTorch to do this. To ensure that we always create the same validation set. we’ll also set a seed for the random number generator

To ensure that we always create the same validation set, we’ll also set a seed for the random number generator


We can now createdata loaders to help us load the data in batches. Large datasets requires loading them into memory all at once. This leads to memory outage and slowing down of programs. PyTorch offers a solution for parallelizing the data loading process with the support of automatic batching as well. This is the DataLoader class present within the package.We can look at batches of images from the dataset using the make_grid method from torchvision.

Each time the following code is run, we get a different bach, since the sampler shuffles the indices before creating batches.

Batch size:

The batch size is a number of samples processed before the model is updated.

Advantages of using a batch size < number of all samples:

  • It requires less memory. Since you train the network using fewer samples, the overall training procedure requires less memory. That’s especially important if you are not able to fit the whole dataset in your machine’s memory.
  • Typically networks train faster with mini-batches. That’s because we update the weights after each propagation.


An epoch is a term used in machine learning and indicates the number of passes of the entire training dataset the machine learning algorithm has completed. Datasets are usually grouped into batches (especially when the amount of data is very large).

The number of epochs is the number of complete passes through the training dataset. The size of a batch must be more than or equal to one and less than or equal to the number of samples in the training dataset.


As the sizes of our models and datasets increase, we need to use GPUs to train our models within a reasonable amount of time.

GPUs contain hundreds of cores that are optimized for performing expensive matrix operations on floating point numbers in a short time

This makes GPU(s) ideal for training deep neural networks with many layers.

We can check if a GPU is available and the required NVIDIA CUDA drivers are installed using torch.cuda.is_available.


Feedforward neural networks were among the first and most successful learning algorithms. They are also called deep networks, multi-layer perceptron (MLP), or simply neural networks. As data travels through the network’s artificial mesh, each layer processes an aspect of the data, filters outliers, spots familiar entities and produces the final output


Input layer: This layer consists of the neurons that receive inputs and pass them on to the other layers. The number of neurons in the input layer should be equal to the attributes or features in the dataset.

Output layer: The output layer is the predicted feature and depends on the type of model you’re building.

Hidden layer: In between the input and output layer, there are hidden layers based on the type of model. Hidden layers contain a vast number of neurons which apply transformations to the inputs before passing them.

As the network is trained, the weights are updated to be more predictive.

> Introducing a hidden layer and an activation function allows the model to learn more complex, multi-layered and non-linear relationships between the inputs and the targets.




# for epoch in range(num_epochs):
# Training phase
# for batch in train_loader:
# Generate predictions
# Calculate loss
# Compute gradients
# Update weights
# Reset gradients

# Validation phase
# for batch in val_loader:
# Generate predictions
# Calculate loss
# Calculate metrics (accuracy etc.)
# Calculate average validation loss & metrics

# Log epoch, loss & metrics for inspection

We have defined an accuracy function which calculates the overall accuracy of the model on an entire batch of outputs, so that we can use it as a metric in fit.

Cross-entropy is commonly used in machine learning as a loss function.Cross-entropy is a measure of the difference between two probability distributions for a given random variable or set of events.

While the accuracy does continue to increase as we train for more epochs, the improvements get smaller with every epoch. This is easier to see using a line graph.



While we have been tracking the overall accuracy of a model so far, it’s also a good idea to look at model’s results on some sample images.



So we have successfully created a model that classifies different classes of fruits using FeedForward Neural Networks.

We have reached the accuracy of 96%.

You can get my entire notebook on:

This is my blog on PyTorch functions,so you can go through them to know more about PyTorch Framework: