PyTorch is a popular deep learning framework developed by Facebook’s AI Research lab. It is widely used for building deep learning models and is known for its flexibility and easy-to-use API. In this tutorial, we will cover the basics of PyTorch for beginners, including how to install PyTorch, create a simple deep learning model, and train it on a dataset.
PyTorch Installation
Before we get started with PyTorch, we need to install it on our system. PyTorch can be installed using pip, which is the Python package manager. To install PyTorch, open a terminal or command prompt and run the following command:
pip install torch torchvision
This will install PyTorch and its extensions, including torchvision, which contains popular datasets, model architectures, and preprocessing tools.
Creating a Simple Deep Learning Model
Now that we have PyTorch installed, let’s create a simple deep learning model using PyTorch. First, we need to import the necessary libraries:
import torch
import torch.nn as nn
import torch.optim as optim
Next, let’s define our model. For this tutorial, we will create a simple feedforward neural network with one hidden layer. Here’s the code:
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
In this code, we define a class named SimpleNN
that inherits from nn.Module
. We define two fully connected layers (fc1
and fc2
) with the specified input and output dimensions. In the forward
method, we apply the ReLU activation function to the output of the first hidden layer and return the final output.
Training the Model
Now that we have defined our deep learning model, let’s train it on a dataset. For this tutorial, we will use the MNIST dataset, which contains images of handwritten digits. First, we need to load the dataset and define the data loaders:
import torchvision
import torchvision.transforms as transforms
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)
Next, let’s define the loss function and optimizer:
model = SimpleNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
Now, we can train the model on the dataset:
for epoch in range(10):
for i, (inputs, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(inputs.view(-1, 784))
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if i % 100 == 0:
print(f'Epoch {epoch}, Batch {i}, Loss: {loss.item()}')
In this code, we iterate over each batch of the training data, compute the model’s output, calculate the loss using cross-entropy loss, backpropagate the gradients, and update the model parameters using stochastic gradient descent.
Conclusion
In this tutorial, we covered the basics of PyTorch for beginners, including how to install PyTorch, create a simple deep learning model, and train it on the MNIST dataset. PyTorch is a powerful deep learning framework that can be used to build and train complex neural networks for a variety of tasks. I hope this tutorial helps you get started with PyTorch and deep learning. Happy coding!
"🔥Caltech Post Graduate Program In AI And Machine Learning – https://www.simplilearn.com/artificial-intelligence-masters-program-training-course?utm_campaign=4dNmxXl4QvY&utm_medium=Comments&utm_source=Youtube
🔥IITK – Professional Certificate Course in Generative AI and Machine Learning (India Only) – https://www.simplilearn.com/iitk-professional-certificate-course-ai-machine-learning?utm_campaign=4dNmxXl4QvY&utm_medium=Comments&utm_source=Youtube
🔥Purdue – Post Graduate Program in AI and Machine Learning – https://www.simplilearn.com/pgp-ai-machine-learning-certification-training-course?utm_campaign=4dNmxXl4QvY&utm_medium=Comments&utm_source=Youtube
🔥IITG – Professional Certificate Program in Generative AI and Machine Learning (India Only) – https://www.simplilearn.com/iitg-generative-ai-machine-learning-program?utm_campaign=4dNmxXl4QvY&utm_medium=Comments&utm_source=Youtube
🔥Caltech – AI & Machine Learning Bootcamp (US Only) – https://www.simplilearn.com/ai-machine-learning-bootcamp?utm_campaign=4dNmxXl4QvY&utm_medium=Comments&utm_source=Youtube"
A huge thanks from Brazil!!