Deep Learning PyTorch Course, Supervised Learning

Deep learning is a field of artificial intelligence (AI) that uses multilayer neural networks to learn patterns from data.
Today, we will conduct an in-depth lecture on one of the two most commonly used learning methods in deep learning, known as supervised learning.

1. What is Supervised Learning?

Supervised learning is a method for learning predictive models based on given data.
Here, ‘supervised’ refers to labeled training data.
In supervised learning, relationships between input data and output labels are learned to create a model capable of making predictions on new data.

1.1 Types of Supervised Learning

Supervised learning can be broadly divided into two types: classification and regression.

  • Classification: Predicts whether a given input data belongs to a specific class.
    For example, classifying whether an email is spam or not falls into this category.
  • Regression: Predicts continuous numeric values based on input data.
    For instance, predicting house prices based on the area of the house is an example of regression.

2. Introduction to PyTorch

PyTorch is an open-source machine learning library developed by Facebook that provides a variety of useful features for deep learning researchers and developers.
In particular, it supports dynamic computation graphs,
which makes it easier to debug and modify models.

2.1 Installing PyTorch

To install PyTorch, you can use the following command.
The command below shows how to install PyTorch using pip:

pip install torch torchvision torchaudio

3. Creating a Deep Learning Model

Now, let’s create a simple deep learning model using PyTorch.
This example will address a classification problem, using the famous MNIST dataset to build a model that classifies handwritten digits.
The MNIST dataset consists of images of digits from 0 to 9.

3.1 Loading the Dataset

First, we will load the MNIST dataset and split it into training and testing data.

import torch
from torchvision import datasets, transforms

# Data transformation (normalization)
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))  # Normalized to mean 0.5, standard deviation 0.5
])

# Download and load the MNIST dataset
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)

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)

3.2 Defining the Model

Deep learning models are defined by inheriting from nn.Module. Here, we will define a simple neural network model.

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

class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 128)  # First layer
        self.fc2 = nn.Linear(128, 64)       # Second layer
        self.fc3 = nn.Linear(64, 10)        # Output layer

    def forward(self, x):
        x = x.view(-1, 28 * 28)  # Convert 2D image to 1D vector
        x = F.relu(self.fc1(x))  # Apply ReLU activation function
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# Create an instance of the model
model = SimpleNN()

3.3 Defining the Loss Function and Optimizer

Now we will define the loss function and optimizer. We will use the cross-entropy loss function and the Adam optimizer.

import torch.optim as optim

# Loss function
criterion = nn.CrossEntropyLoss()
# Optimizer
optimizer = optim.Adam(model.parameters(), lr=0.001)

3.4 Training the Model

Now it’s time to train the model. For each epoch, we will pass the data through the model iteratively,
compute the loss, and update the weights.

num_epochs = 5

for epoch in range(num_epochs):
    for images, labels in train_loader:
        # Zero the gradients
        optimizer.zero_grad()
        # Pass images through the model
        outputs = model(images)
        # Calculate loss
        loss = criterion(outputs, labels)
        # Backpropagation
        loss.backward()
        # Update weights
        optimizer.step()
    
    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

3.5 Evaluating the Model

Once training is complete, let’s evaluate the model using the test data.

correct = 0
total = 0

with torch.no_grad():  # Disable gradient calculation
    for images, labels in test_loader:
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)  # Extract the index of the maximum value
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

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

4. Conclusion

In this lecture, we learned how to build a basic deep learning model using PyTorch and
solve a classification problem using the MNIST dataset.
This approach utilizing supervised learning can be applied in various fields and can be expanded into more complex models.

4.1 Additional Learning Resources

If you want to learn more about deep learning, check out the following resources:

Deep learning is a vast field of research that requires continuous learning.
We hope this will help you on your deep learning journey!