Module 24 of 26 · Deep Learning with PyTorch · Intermediate

capstone-project

Duration: 12 min

In this capstone project module, you will apply all the deep learning concepts and techniques you've learned using PyTorch to a real-world problem. This project will challenge you to integrate data preprocessing, model building, training, and evaluation into a cohesive solution. This module matters as it consolidates your understanding and showcases your ability to tackle complex tasks in deep learning.

Data Preprocessing

Data preprocessing is a crucial step in any deep learning project. It involves cleaning and transforming raw data into a format suitable for training a model. This includes handling missing values, normalizing data, and splitting the dataset into training, validation, and test sets. Proper preprocessing can significantly impact the performance and accuracy of your model.

import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define transformations for the training data
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])

# Download and load the training data
trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
trainloader = DataLoader(trainset, batch_size=64, shuffle=True)

# Print out the shape of a single training batch
dataiter = iter(trainloader)
images, labels = dataiter.next()
print(images.shape)

Try it in Google Colab: Open in Colab

torch.Size([64, 1, 28, 28])

Model Building

Building a deep learning model involves defining the architecture, initializing the layers, and setting up the loss function and optimizer. The architecture should be designed to handle the specific problem at hand, and the choice of loss function and optimizer can greatly affect the training process and final performance.

import torch.nn as nn
import torch.optim as optim

# Define the model architecture
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 10)

    def forward(self, x):
        x = x.view(-1, 28 * 28)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# Initialize the model, loss function, and optimizer
model = SimpleNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)

# Print the model architecture
print(model)

💡 Tip: When defining your model, ensure that the input and output dimensions match the data and the problem requirements. Mismatched dimensions can lead to errors during training.

❓ What is the purpose of the 'transform' variable in the data preprocessing code?

❓ What does the 'nn.Linear' function do in the model building code?

← Previous Continue interactively → Next →

Related Courses