Introduction to Pytorch and Tensors

Pytorch is a free and open-source deep-learning framework developed by Meta and based on the Torch library.

It is used mainly for machine learning tasks like computer vision, natural language processing, and speech recognition.

In this post, you will discover the Pytorch library for Deep Learning.

night shot, starry sky, night sky-2553103.jpg
Introduction to the Pytorch deep learning framework

What Is Pytorch?

Pytorch is an open-source deep learning framework, created by Meta in 2016 based on the torch library and is used mainly by researchers. Pytorch uses C, and python as programming languages and is released under the Modified BSD license.

Pytorch has grown in popularity recently among developers, defeating the Tensorflow framework. And it is the framework by choice for universities like Stanford to teach deep learning.

Pytorch is based on Torch, a scientific computing framework using LuaJIT language and c/cuda implementation.

What is a tensor

You can read this article to understand tensors: http://decoderai.com/tensorflow-2-0-in-deep-learning-all-the-basics-you-need-to-know/#What_is_a_tensor

How to install Pytorch

In your CMD OR if you have Anaconda, go to Anaconda terminal (prompt) and type the following command:

pip install torch

Then, install ‘torchvision’, a computer vision library:

pip install torchvision

To use the GPU, you must also have the Cuda Toolkit installed. Or You can use google colab that has a GPU installed.

Your first model using Pytorch

We will use the torchvision package to load common datasets like MNIST, CIFAR10, ImageNet, etc.

In this example, we will create a computer vision classifier model using PyTorch to classify objects using CIFAR10 dataset.

the CIFAR10 dataset has 10 classes: ‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. the dataset is 3-channel color images of 32×32 pixels in size(3*32*32).

The project will proceed in the following order:

  1. Load and normalize the CIFAR10 training and test datasets.
  2. Create a Convolutional Neural Network model
  3. Create a loss function and optimizer.
  4. Train the network.
  5. Test the network.
source

Step 1: Load the CIFAR10 dataset

First, we will import the necessary libraries including ‘torch’ and ‘torchvision’:

import torch
import torchvision
import torchvision.transforms as transforms

We will apply this transformation function to our images. It will resize, normalize the images and transform them into tensors.

transform = transforms.Compose(
[transform.Resize(255),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
batch_size = 16
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=2)
classes = trainset.classes

Step 2: Create a CNN model

import torch.nn as nn
import torch.nn.functional as F

We will define the layers of our model but without order!

class CNNnetwork(nn.Module):
  def init(self):
    super().init()
    self.conv1 = nn.Conv2d(3, 6, 5)
    self.pool = nn.MaxPool2d(2, 2)
    self.conv2 = nn.Conv2d(6, 16, 5)
    self.fc1 = nn.Linear(16 * 5 * 5, 120)
    self.fc2 = nn.Linear(120, 84)
    self.fc3 = nn.Linear(84, 10)

Now, we are going to define in order our CNN network layers in the ‘forward’ function:

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = torch.flatten(x, 1) # flatten all dimensions except batch
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x
net = CNNnetwork()

Step 3: Define the loss function and the optimizer

Let’s define our loss function and optimizer:

import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

Step 4: Train the model

dataset_sizes=len(trainset)

for epoch in range(10):  # loop over the dataset multiple times

    running_loss = 0.0
    running_corrects = 0

    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        _, preds = torch.max(outputs, 1)

        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()* inputs.size(0)

        running_corrects += torch.sum(preds == labels.data)

    epoch_loss = running_loss / dataset_sizes

    epoch_acc = running_corrects.double() / dataset_sizes

        
    print(f'[epoch {epoch + 1}] loss: {epoch_loss:.3f} Accuracy: {epoch_acc :.3f}')

print('Finished Training')

Step 5: Test the model

correct = 0
total = 0
# since we're not training, we don't need to calculate the gradients for our outputs
with torch.no_grad():
    for data in testloader:
        images, labels = data
        # calculate outputs by running images through the network
        outputs = net(images)
        # the class with the highest energy is what we choose as prediction
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')

Summary:

In this article, we learned more about the PyTorch deep learning framework and how to create our object classification model with python.

Please leave your questions in the comments section below, and I will try my best to respond.