VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/how-to-implement-various-optimization-algorithms-in-pytorch/

⇱ How to Implement Various Optimization Algorithms in Pytorch? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Implement Various Optimization Algorithms in Pytorch?

Last Updated : 23 Jul, 2025

Optimization algorithms are an essential aspect of deep learning, and PyTorch provides a wide range of optimization algorithms to help us train our neural networks effectively. In this article, we will explore various optimization algorithms in PyTorch and demonstrate how to implement them. We will use a simple neural network for the demonstration.

NOTE: If in your system, the PyTorch module is not installed, then you need to install PyTorch by running the following command in your terminal or command prompt :

pip install torch torchvision

This will install the PyTorch module along with torchvision, which is a package that provides access to popular datasets, model architectures, and image transformations for PyTorch. Once you have installed these modules, you should be able to run the code without any errors.

Implementations

Import Libraries:

First, we need to import the required libraries. We will be using the PyTorch framework, so we will import the torch library. We will also use the MNIST dataset to train our neural network, so we will import the torchvision library.

Load Data:

Next, we will load the MNIST dataset and prepare it for training. We will normalize the data and create batches of data using the DataLoader class.

Output:

Files already downloaded and verified

Build Neural Network Model:

We will define a simple neural network with two hidden layers, each with 128 neurons, and an output layer with 10 neurons, one for each digit. We will use the ReLU activation function for the hidden layers and the softmax activation function for the output layer.

Loss Function and Optimization Algorithm:

We will use the cross-entropy loss function to train our neural network. We will also use various optimization algorithms, such as stochastic gradient descent (SGD), Adam, Adagrad, and Adadelta, to train our neural network. We will define these optimization algorithms and their hyperparameters as follows:

Now, Train the Neural Network:

We will now train our neural network using the various optimization algorithms we defined earlier. We will train our neural network for 10 epochs and print the loss and accuracy after each epoch.

Output:

Epoch: 1 | Loss: 1.589 | Accuracy: 42.224 %
Epoch: 2 | Loss: 1.377 | Accuracy: 51.298 %
Epoch: 3 | Loss: 1.314 | Accuracy: 54.116 %
Epoch: 4 | Loss: 1.272 | Accuracy: 55.800 %
Epoch: 5 | Loss: 1.249 | Accuracy: 57.118 %
Epoch: 6 | Loss: 1.223 | Accuracy: 57.998 %
Epoch: 7 | Loss: 1.204 | Accuracy: 58.720 %
Epoch: 8 | Loss: 1.191 | Accuracy: 59.426 %
Epoch: 9 | Loss: 1.181 | Accuracy: 59.916 %
Epoch: 10 | Loss: 1.176 | Accuracy: 60.258 %

Use different optimization algorithms for different parts of the model

Output:

Files already downloaded and verified
Epoch: 1 | Loss: 1.634 | Accuracy: 41.848 %
Epoch: 2 | Loss: 1.436 | Accuracy: 50.932 %
Epoch: 3 | Loss: 1.367 | Accuracy: 54.456 %
Epoch: 4 | Loss: 1.318 | Accuracy: 56.632 %
Epoch: 5 | Loss: 1.287 | Accuracy: 58.154 %
Epoch: 6 | Loss: 1.270 | Accuracy: 59.088 %
Epoch: 7 | Loss: 1.247 | Accuracy: 60.192 %
Epoch: 8 | Loss: 1.235 | Accuracy: 60.676 %
Epoch: 9 | Loss: 1.226 | Accuracy: 61.344 %
Epoch: 10 | Loss: 1.220 | Accuracy: 61.608 %

Advantages and disadvantages of implementing various Optimization Algorithm in Pytorch

Advantages:

  • Improved training performance: Using different optimization algorithms for different parts of the model can improve the training performance by allowing each part of the model to learn at its optimal rate.
  • Better convergence:  Some optimization algorithms perform better for specific types of model architectures. With the help of multiple optimizations, we can take advantage of their respective strength to achieve better convergence.
  • Regularization: Different optimization algorithms can have different regularisation impacts on the model. It can prevent from overfitting and enhance the model's generalizability.

Disadvantages:

  • Increased complexity:  Implementing multiple optimization algorithms can increase the complexity which will require more training time and resources, And it may be harder to maintain and debug.
  • Risk of instability: Using several optimization algorithms can make the training process more unstable because different algorithms may attempt to optimize the same parameter in conflicting or oscillating ways.
Comment