Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Most of the time, we create a model and skip the part of optimization. This leads to poor model accuracy and prediction. Thus, it becomes crucial to optimize a model as it will enhance accuracy and minimize errors. Optimization involves using the best parameters and hyperparameters to make the model generalize for unseen data.
In this blog, we will be learning about different methods of model optimization in PyTorch. So without any further wait, let’s start learning!
What is Model Optimization?
Model optimization is a technique in which we adjust the parameters of a neural network model during the training phase to minimize differences between actual output and predicted output. It is the medium by which the parameters of a model get updated using gradients of the loss function.
Thus choosing an optimal parameter is a crucial task that can decide how slow or fast a model will move toward the convergence point.
Below are some pre-requisites to fully understand the concept of model optimization in PyTorch:
Familiarity with Python and Deep Learning: You should be familiar with the Python programming language and deep learning concepts like neural networks, layers, optimizers, etc.
PyTorch: The basic knowledge of PyTorch, including tensors, modules, and Autograd is necessary.
Data Loading: A basic understanding of how to load and process the data using PyTorch’s modules, like DataLoader and Torchvision, is beneficial.
FashionMNIST Dataset: A basic overview of the FashionMNIST dataset and its structure will be beneficial for understanding data loading and processing.
Machine Learning Terminologies: You should also be aware of many machine learning terms like loss functions, activation functions, batch size, etc.
Requirements
Install the below libraries below proceeding further in the blog.
Python: The code for the model is written in Python, so you need to have Python installed on your machine.
PyTorch: It is an open-source machine learning library that provides flexible frameworks for building and training deep learning models.
TorchVision: It is a package in PyTorch that provides standard datasets and models for various computer vision tasks.
Note: You can install the libraries using the pip command as given below:
pip install torch torchvision
Building a Neural Network Model
Now, let’s start building a neural network model on which we will be going to apply optimizations.
Importing Libraries
First, we will import libraries that are necessary for building the model.
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
You can also try this code with Online Python Compiler
Here, we imported the PyTorch library for building and training the neural network, which is imported as nn. Next, we imported DataLoader, which loads data in batches during training. Then we imported torchvision, which contains standard datasets, and lastly, the ToTensor to convert data into PyTorch tensors.
Loading Data
Now, let’s load the data on which we will be going to build the model.
Here, we loaded the FashionMNIST dataset for training and testing with specified transformations. After running the above code, the dataset will get downloaded on your device as shown below:
Model Definition
Now, let’s build the model class and create its required functions.
class NeuralNetwork(nn.Module):
# Initialize the class
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
#creating fully connected layers
self.fc1 = nn.Linear(28*28, 512)
self.fc2 = nn.Linear(512, 512)
self.fc3 = nn.Linear(512, 10)
self.relu = nn.ReLU()
def forward(self, x):
x = self.flatten(x)
#Applying fully connected layers and Relu activation
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
output = self.fc3(x)
return output
model = NeuralNetwork()
You can also try this code with Online Python Compiler
Here, we created a neural network class with three fully connected (dense) layers and a ReLU activation function applied after each hidden layer. Then the input to the network is flattened before passing through the fully connected layers.
Now, we are done building the model, so let’s start the process of model optimization in PyTorch.
Setting Hyperparameters
Let us first set different hyperparameters for our model.
learning_rate = 1e-7
batch_size = 69
epochs = 10
You can also try this code with Online Python Compiler
Here, we created a function named loop which computes and returns the accuracy and average loss for every iteration we perform. The loop manages both training and evaluation modes based on the is_train flag variable. During training, it performs forward and backward passes to compute the loss, update the model's parameters using the optimizer, and print loss at regular intervals. At last, it returns the accuracy and average loss for the given dataset.
Optimizer and Loss Function
The loss function is used to compute the error between the actual values and the predicted values. And the optimizers are used to update the weights of the model based on the loss function.
Here, we have chosen the Cross-Entropy loss function and the Stochastic Gradient Descent optimizer with the specified learning rate. The Cross entropy loss function is ideal for multi-class classification tasks and the SDG optimizer which updates each training example's parameters one at a time.
Final Demonstration
Now, let us write a code to train our model for a specific number of epochs and which will give us the training and testing loss in each iteration.
for i in range(epochs):
print('Epoch', (i+1))
print("-------------------------------")
loop(train_dataloader, model, loss_fn, optimizer)
loop(test_dataloader, model, loss_fn, optimizer)
You can also try this code with Online Python Compiler
Here, we are iterating over the range of passed epochs and learning the best model parameters with the best accuracy.
After running the code for two iterations, we got the average loss for each epoch. In this way, we can choose the best parameters for which our model will give the best average loss and accuracy for our model.
Frequently Asked Questions
What is PyTorch?
PyTorch is an open-source deep learning framework used for building, training, and deploying machine learning models.
What is model optimization in PyTorch?
In PyTorch, model optimization refers to the process of boosting the performance of a neural network model by adjusting its parameters during the training phase.
How can I prevent overfitting during model optimization?
You can prevent the problem of overfitting data during optimization by using techniques like regularization and data augmentation.
How can I evaluate my model’s performance during optimization?
You can evaluate your model performance by using evaluation metrics, including accuracy, precision, and F1-score depending on your task (classification or regression).
Conclusion
This article discusses the concept of model optimization in PyTorch. We built a model and optimized it by adjusting various hyperparameters of our model. We hope this blog has helped you grow your knowledge of model optimization in PyTorch. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!