Unit 4 Part 3 DL - 1
Unit 4 Part 3 DL - 1
Chapter-III
……………………………………………………………………………………………………………………………..
PyTorch is an optimized Deep Learning tensor library based on Python and Torch and is mainly used for
applications using GPUs and CPUs. PyTorch is favored over other Deep Learning frameworks like TensorFlow
and Keras since it uses dynamic computation graphs and is completely Pythonic.
It is open source, and is based on the popular Torch library. PyTorch is designed to provide good flexibility
and high speeds for deep neural network implementation. PyTorch is different from other deep learning
frameworks in that it uses dynamic computation graphs.
3.1.Features
The major features of PyTorch are mentioned below −
Easy Interface − PyTorch offers easy to use API; hence it is considered to be very simple to operate and runs
on Python. The code execution in this framework is quite easy.
Python usage − This library is considered to be Pythonic which smoothly integrates with the Python data
science stack. Thus, it can leverage all the services and functionalities offered by the Python environment.
Computational graphs − PyTorch provides an excellent platform which offers dynamic computational graphs.
Thus a user can change them during runtime. This is highly useful when a developer has no idea of how much
memory is required for creating a neural network model.
• Module − Neural network layer which will store state or learnable weights.
Advantages of PyTorch
We shall look into the major differences between TensorFlow and PyTorch below –
• Optimizer: An optimizer is an algorithm that updates the model's parameters during training.
• Loss: A loss function measures the error between the model's predictions and the ground truth labels.
Models are created using the torch.nn.Module class. Layers are created using the different classes provided by
the torch.nn module. For example, to create a linear layer, you would use the torch.nn.Linear class.
Optimizers are created using the classes provided by the torch.optim module. For example, to create an Adam
optimizer, you would use the torch.optim.Adam class.
Loss functions are created using the classes provided by the torch.nn.functional module. For example, to create
a mean squared error loss function, you would use the torch.nn.functional.mse_loss function.
Once you have created the model, layers, optimizer, and loss function, you can train the model using the
following steps:
1. Forward pass: The input data is passed through the model to produce predictions.
2. Loss calculation: The loss function is used to calculate the error between the predictions and the ground
truth labels.
23 | ©www.tutorialtpint.net – Prepared By D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualified
Downloaded by Korapati Usha Rani ([email protected])
lOMoARcPSD|47483061
3. Backward pass: The gradients of the loss function with respect to the model's parameters are calculated.
4. Optimizer step: The optimizer uses the gradients to update the model's parameters.
This process is repeated for a number of epochs until the model converges and achieves the desired
performance.
This code defines a simple linear model with one input layer and one output layer. The model is trained using
the Adam optimizer and the mean squared error loss function.
To implement RNNs in PyTorch, you can use the torch.nn.RNN module. This module provides a number of
different RNN architectures, including LSTM and GRU.
This code defines a simple LSTM model with one input layer, one LSTM layer, and one output layer. The LSTM
layer has 128 hidden units.
The model is trained using the model.fit() method. The model can then be used to make predictions on new
data using the model.predict() method.
For more complex tasks, you may need to use a different RNN architecture or add additional layers to the
model. You can also use PyTorch to implement bidirectional RNNs, stacked RNNs, and other advanced RNN
architectures.
PyTorch also provides a number of tools for training and evaluating RNNs, such as the torch.optim module
and the torch.nn.functional module.
PyTorch is a popular Python library for machine learning. It provides a number of features that make it easy
to build, train, and deploy CNNs.
To implement a CNN in PyTorch, you can use the torch.nn.Conv2d layer. This layer performs a convolution
operation on the input data. The convolution operation is a mathematical operation that extracts features
from the input data.
CNNs also use pooling layers to reduce the spatial size of the input data. This helps to reduce the number of
parameters in the network and makes it more efficient to train.
import torch
class CNN(torch.nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = torch.nn.Conv2d(3, 6, 5)
self.pool1 = torch.nn.MaxPool2d(2, 2)
self.pool2 = torch.nn.MaxPool2d(2, 2)
25 | ©www.tutorialtpint.net – Prepared By D.Venkata Reddy M.Tech(Ph.D), UGC NET, AP SET Qualified
Downloaded by Korapati Usha Rani ([email protected])
lOMoARcPSD|47483061
x = self.conv1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
x = x.view(-1, 16 * 5 * 5)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
model = CNN()
...
This code defines a simple CNN with two convolutional layers, two pooling layers, and three fully connected
layers. The convolutional layers have 6 and 16 filters, respectively. The pooling layers have a kernel size of
2x2 and a stride of 2. The fully connected layers have 120, 84, and 10 units, respectively.
The model is trained using the model.fit() method. The model can then be used to make predictions on new
data using the model.predict() method.
For more complex tasks, you may need to use a different CNN architecture or add additional layers to the
model. You can also use PyTorch to implement other types of neural networks, such as recurrent neural
networks (RNNs) and long short-term memory (LSTM) networks.