PyTorch Training Session

Posted by


PyTorch is an open-source machine learning library developed by Facebook’s AI Research lab that is widely used for training deep neural networks. In this tutorial, I will guide you through the process of training a neural network using PyTorch.

Step 1: Installation
Before you can start using PyTorch, you need to install it on your system. PyTorch can be installed using pip, a package manager for Python. To install PyTorch, open a terminal and run the following command:

$ pip install torch torchvision

Step 2: Create a Neural Network
Next, you need to create a neural network model that you will train using PyTorch. PyTorch provides a nn.Module class that you can use to define your neural network. Here’s an example of a simple neural network model:

import torch
import torch.nn as nn

class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(784, 128)  # Input size: 784, Output size: 128
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(128, 10)    # Input size: 128, Output size: 10

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x

In this example, we define a simple neural network with two fully connected layers. The input size of the first layer is 784, which is the number of pixels in a 28×28 image (MNIST dataset). The output size of the last layer is 10, which corresponds to the number of classes in the dataset.

Step 3: Load the Dataset
To train a neural network, you need a dataset to train it on. PyTorch provides a torchvision package that contains popular datasets like MNIST, CIFAR-10, etc. Here’s an example of how to load the MNIST dataset:

import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,),(0.5,)])
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transform, download=True)

In this example, we define a transform that normalizes the pixel values in the range [-1, 1]. We then load the MNIST dataset, both train and test, and apply the transform to the images.

Step 4: Create a DataLoader
To efficiently train a neural network, you need to create a DataLoader that batches the data and shuffles it. Here’s how you can create a DataLoader for the MNIST dataset:

train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)

In this example, we create two DataLoaders for the train and test datasets with a batch size of 64 and shuffle the train dataset.

Step 5: Train the Neural Network
Now, you can train the neural network using the DataLoader and the neural network model. Here’s an example of how to train the neural network:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = SimpleNN().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

num_epochs = 5
total_step = len(train_loader)

for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.view(-1, 28*28).to(device)
        labels = labels.to(device)

        outputs = model(images)
        loss = criterion(outputs, labels)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                   .format(epoch+1, num_epochs, i+1, total_step, loss.item()))

In this example, we move the model to the GPU if available and define a criterion (loss function) and an optimizer. We then iterate over the DataLoader and train the model. After every 100 steps, we print the epoch, step, and loss.

Step 6: Evaluate the Model
After training the model, you should evaluate its performance on the test dataset. Here’s how you can evaluate the model:

with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.view(-1, 28*28).to(device)
        labels = labels.to(device)

        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the test images: {} %'.format(100 * correct / total))

In this example, we evaluate the model on the test dataset and compute the accuracy of the model.

This concludes our tutorial on training a neural network using PyTorch. I hope this tutorial was helpful for you to get started with training deep neural networks using PyTorch. Happy coding!

0 0 votes
Article Rating
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@user-ti3qu1rq8u
2 months ago

My jupyter stuck when num_worker > 0, I do not have a GPU and I work only with a CPU. Many people say that Jupyter notebooks have known issues with mutilprocessing. How can I solve it?

@calllen
2 months ago

max system volume + youtube volume and i still cannot hear what's being said + 720p

@tonyaxis3063
2 months ago

720p with bad balanced audio? How more amateur can this get?

@dezh6345
2 months ago

I was really hoping for something more complex than mnist. Most of the time, the data won't come prepackaged for you. For instance, how do you get data into the datasets and dataloaders? There are tons of videos giving this basic rundown and have been for years. After 4 years, I was hoping for something a bit more substantial.

@artificialhuman9562
2 months ago

Inaudible!

@tayssirmoussa334
2 months ago

Thank you for this video,but i have a question about training model with pytorch,
I'm working on prediction model with pytorch and i need that the process of preparing data abd training run in parallel ,is there any way of doing that?

@user-xs9ey2rd5h
2 months ago

Love your vids man! Reading a tutorial is sometimes a little boring, seeing it in video format gives you a good gist on how stuff works and when implementing your own model you already kind off know where to look in the textual tutorial when you need to refresh the specifics.

@aseemsrivastava3230
2 months ago

480p seriously?😡