Building Convolutional Networks in PyTorch

Learn how to build, train, and evaluate convolutional neural networks (CNNs) in PyTorch with a practical step-by-step example using the CIFAR-10 dataset.

🔰 beginner
⏱️ 30 minutes
👤 SuperML Team

· Deep Learning · 2 min read

📋 Prerequisites

  • Basic Python and PyTorch knowledge
  • Understanding of convolutions and pooling layers

🎯 What You'll Learn

  • Build and understand CNNs using PyTorch
  • Learn how to set up datasets and data loaders
  • Train and evaluate a CNN on image data
  • Gain confidence in using PyTorch for vision projects

Introduction

Convolutional Neural Networks (CNNs) are highly effective for image classification and recognition tasks. In this tutorial, you will learn how to build, train, and evaluate a CNN using PyTorch on the CIFAR-10 dataset.


1️⃣ Install and Import Dependencies

First, ensure PyTorch and torchvision are installed:

pip install torch torchvision

Now import the required libraries:

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms

2️⃣ Prepare the Dataset

Using CIFAR-10 for colored image classification:

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)

3️⃣ Define the CNN Model

class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
        self.fc1 = nn.Linear(64 * 8 * 8, 128)
        self.fc2 = nn.Linear(128, 10)
        self.relu = nn.ReLU()

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = x.view(-1, 64 * 8 * 8)
        x = self.relu(self.fc1(x))
        x = self.fc2(x)
        return x

net = SimpleCNN()

4️⃣ Define Loss Function and Optimizer

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

5️⃣ Training the Model

epochs = 10

for epoch in range(epochs):
    running_loss = 0.0
    for images, labels in trainloader:
        optimizer.zero_grad()
        outputs = net(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    print(f"Epoch {epoch+1}, Loss: {running_loss/len(trainloader):.4f}")

print("Training completed!")

6️⃣ Evaluating the Model

correct = 0
total = 0

with torch.no_grad():
    for images, labels in testloader:
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f"Test Accuracy: {100 * correct / total:.2f}%")

Conclusion

✅ You have successfully built, trained, and evaluated a CNN using PyTorch.
✅ You now understand how to prepare datasets, define models, train, and assess performance for image classification tasks using CNNs.


What’s Next?

✅ Experiment with deeper architectures, adding dropout and batch normalization.
✅ Visualize feature maps to understand what your CNN is learning.
✅ Explore advanced CNN architectures like ResNet on superml.org.


Join the SuperML Community to share your CNN projects and receive peer feedback.


Happy Building with PyTorch! 🚀

Back to Tutorials

Related Tutorials

🔰beginner ⏱️ 60 minutes

Training a Deep Network in PyTorch

Learn how to build and train your first deep neural network using PyTorch with a clear, step-by-step example on the MNIST dataset.

Deep Learning2 min read
deep learningpytorchbeginner +1
🔰beginner ⏱️ 45 minutes

Your First Deep Learning Implementation

Build your first deep learning model to classify handwritten digits using TensorFlow and Keras, explained step-by-step for beginners.

Deep Learning2 min read
deep learningbeginnerkeras +2
🔰beginner ⏱️ 20 minutes

Convolution in Deep Learning: Final Summary

A complete, clear recap of what convolutions are, why they matter, and how they fit into the deep learning pipeline for image and signal tasks.

Deep Learning2 min read
deep learningcnnconvolutions +1
🔰beginner ⏱️ 60 minutes

Training a Deep Network in TensorFlow

Learn how to build and train your first deep neural network using TensorFlow and Keras with clear, step-by-step guidance on the MNIST dataset.

Deep Learning2 min read
deep learningtensorflowkeras +2