Deep Learning PyTorch Course, Running Example Files on Colab

Hello! In this post, we will start with the basics of deep learning and PyTorch and write codes that can be practiced. We will also guide you on how to run the code using Google Colab. PyTorch is a deep learning library suitable for in-depth learning and research, providing an intuitive and flexible dynamic computation graph. A key feature added at each stage of the graph is that researchers and engineers can easily modify and optimize the models as needed.

1. Overview of Deep Learning

Deep learning is a field of machine learning that uses artificial neural networks to learn patterns from data. It is mainly used in image recognition, natural language processing, and speech recognition. Essentially, a deep learning model has a structure that receives input data, processes it, and outputs the results. These models are composed of numerous neurons, each processing received values along with weights to produce outputs.

2. What is PyTorch?

PyTorch is a deep learning framework developed by Facebook, popular for its ability to write Pythonic code. One of the advantages of PyTorch is its intuitive interface and powerful GPU acceleration capabilities, allowing for efficient handling of large-scale data and complex models. Additionally, it supports Dynamic Computation Graphs, enabling flexible changes to the model’s structure.

3. Setting up Google Colab

Google Colab provides an online environment to run Python code. It supports GPU acceleration using CUDA, allowing you to complete model training in a short time.

  1. Log in with your Google account.
  2. Access Google Colab.
  3. Create a new notebook.
  4. Click ‘Runtime’ -> ‘Change Runtime Type’ in the top menu and select GPU.

Your Colab environment is now ready!

4. Basic Usage of PyTorch

4.1. Installing PyTorch

PyTorch is installed by default in Colab, but if you want the latest version, you can install it using the command below.

!pip install torch torchvision

4.2. Tensor

Tensor is the core data structure of PyTorch. It fundamentally supports mathematical operations as an N-dimensional array, providing the following features:

  • Portability between CPU and GPU
  • Automatic differentiation capability

Creating Tensors

The code below is an example of creating basic tensors.

import torch

# Creating basic tensors
tensor_1d = torch.tensor([1.0, 2.0, 3.0])
tensor_2d = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(tensor_1d)
print(tensor_2d)

5. Building a Deep Learning Model

Now let’s build an actual deep learning model. We will implement a simple Deep Neural Network (DNN) and create a model to recognize handwritten digits using the MNIST dataset.

5.1. Downloading the MNIST Dataset

The MNIST dataset is a collection of handwritten digit images and is commonly used as a test dataset for deep learning models. You can easily download and load the dataset using PyTorch.

from torchvision import datasets, transforms

# Define dataset transformations
transform = transforms.Compose([
    transforms.ToTensor(),  # Convert to tensor
    transforms.Normalize((0.5,), (0.5,))  # Normalization
])

# Download 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)

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

5.2. Defining the Model

The code below is an example of defining a simple deep neural network.

import torch.nn as nn

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

    def forward(self, x):
        x = x.view(-1, 28 * 28)  # Convert 2D to 1D
        x = torch.relu(self.fc1(x))  # Activation function
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = SimpleNN()

6. Model Training and Evaluation

6.1. Setting Loss Function and Optimizer

Define the loss function and optimizer for model training. Cross Entropy Loss is commonly used for classification problems.

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

6.2. Training the Model

The process of training the model is as follows:

num_epochs = 5

for epoch in range(num_epochs):
    for images, labels in train_loader:
        optimizer.zero_grad()  # Gradient initialization
        outputs = model(images)  # Model prediction
        loss = criterion(outputs, labels)  # Calculate loss
        loss.backward()  # Calculate gradient
        optimizer.step()  # Update weights
    
    print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')  # Output loss

7. Evaluating Model Performance

After training the model, evaluate its performance using the test dataset.

correct = 0
total = 0

with torch.no_grad():  # Disable gradient calculations
    for images, labels in test_loader:
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)  # Class with the highest probability
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

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

8. Conclusion

In this post, we learned how to build a simple deep learning model using PyTorch and how to run it on Google Colab. In the future, it would be beneficial to tackle more complex models and various datasets, and to learn advanced topics such as transfer learning or reinforcement learning. Challenge yourself with various projects to gain deeper understanding and experience!