Sling Academy
Home/PyTorch/Hands-On PyTorch Exercises to Master Model Training

Hands-On PyTorch Exercises to Master Model Training

Last updated: December 14, 2024

PyTorch is an open-source machine learning library that offers flexibility and dynamic computation needed for modern machine learning projects. If you are seeking to master model training using PyTorch, this article will guide you through hands-on exercises, focusing on building, training, and fine-tuning models using this powerful framework.

Setting Up Your Environment

Before jumping into model training, ensure you have PyTorch installed. You can install PyTorch from its official website, which provides the correct command based on your system specifications.

# Install PyTorch
pip install torch torchvision torchaudio

Coding Exercise 1: Building a Simple Neural Network

Let's begin by creating a simple neural network. We will use PyTorch's nn module to define the network's architecture. In this example, we'll build a basic feedforward network.

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

# Define a neural network class
class SimpleNet(nn.Module):
    def __init__(self):
        super(SimpleNet, self).__init__()
        self.fc1 = nn.Linear(784, 128)  # First fully connected layer
        self.fc2 = nn.Linear(128, 64)   # Second fully connected layer
        self.fc3 = nn.Linear(64, 10)    # Output layer
    
    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# Instantiate the neural network
model = SimpleNet()

Coding Exercise 2: Preparing the Data

Next, let's prepare the dataset. PyTorch provides seamless solutions to load popular datasets through the torchvision library. In this example, we'll use the MNIST dataset.

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

# Define a transform to normalize the data
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])

# Load the training and test datasets
trainset = datasets.MNIST('/mnist_data', download=True, train=True, transform=transform)
testset = datasets.MNIST('/mnist_data', download=True, train=False, transform=transform)

# Create data loaders
trainloader = DataLoader(trainset, batch_size=64, shuffle=True)
testloader = DataLoader(testset, batch_size=64, shuffle=False)

Coding Exercise 3: Training the Model

With the model and data ready, we can now focus on training. Training involves using an optimizer and loss function to update the model's weights. Here we'll use Stochastic Gradient Descent as the optimizer and Cross Entropy as the loss function.

# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Training loop
for epoch in range(5):  # Iterate over the dataset multiple times
    running_loss = 0.0
    for images, labels in trainloader:
        # Flatten images into a 784 long vector
        images = images.view(images.shape[0], -1)

        # Zero the gradients
        optimizer.zero_grad()

        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)

        # Backward pass and optimize
        loss.backward()
        optimizer.step()

        # Print statistics
        running_loss += loss.item()
    print(f"Epoch {epoch + 1}, Loss: {running_loss / len(trainloader)}")

Coding Exercise 4: Evaluating the Model

After training, it is crucial to evaluate the model's performance on the test dataset. This allows for an insight into how well the model generalizes on unseen data.

correct = 0
total = 0
# Deactivate autograd engine (no need for gradient computation here)
with torch.no_grad():
    for images, labels in testloader:
        images = images.view(images.shape[0], -1)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy of the network: {100 * correct / total} %')

Conclusion

By following these exercises, you learned the fundamental steps needed to create, train, and evaluate a neural network in PyTorch. Continuous practice with different architectures and datasets will further your mastery, enabling you to undertake more complex projects in machine learning.

Next Article: Practice Your PyTorch Skills with Real-World Exercises

Previous Article: Putting Together Everything You Learned in PyTorch

Series: The First Steps with PyTorch

PyTorch

You May Also Like

  • Addressing "UserWarning: floor_divide is deprecated, and will be removed in a future version" in PyTorch Tensor Arithmetic
  • In-Depth: Convolutional Neural Networks (CNNs) for PyTorch Image Classification
  • Implementing Ensemble Classification Methods with PyTorch
  • Using Quantization-Aware Training in PyTorch to Achieve Efficient Deployment
  • Accelerating Cloud Deployments by Exporting PyTorch Models to ONNX
  • Automated Model Compression in PyTorch with Distiller Framework
  • Transforming PyTorch Models into Edge-Optimized Formats using TVM
  • Deploying PyTorch Models to AWS Lambda for Serverless Inference
  • Scaling Up Production Systems with PyTorch Distributed Model Serving
  • Applying Structured Pruning Techniques in PyTorch to Shrink Overparameterized Models
  • Integrating PyTorch with TensorRT for High-Performance Model Serving
  • Leveraging Neural Architecture Search and PyTorch for Compact Model Design
  • Building End-to-End Model Deployment Pipelines with PyTorch and Docker
  • Implementing Mixed Precision Training in PyTorch to Reduce Memory Footprint
  • Converting PyTorch Models to TorchScript for Production Environments
  • Deploying PyTorch Models to iOS and Android for Real-Time Applications
  • Combining Pruning and Quantization in PyTorch for Extreme Model Compression
  • Using PyTorch’s Dynamic Quantization to Speed Up Transformer Inference
  • Applying Post-Training Quantization in PyTorch for Edge Device Efficiency