Bird
Raised Fist0
PyTorchml~20 mins

nn.Conv2d layers in PyTorch - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - nn.Conv2d layers
Problem:You are training a simple image classifier using a convolutional neural network (CNN) with nn.Conv2d layers on the CIFAR-10 dataset. The current model achieves high training accuracy but much lower validation accuracy.
Current Metrics:Training accuracy: 98%, Validation accuracy: 65%, Training loss: 0.05, Validation loss: 1.2
Issue:The model is overfitting: it performs very well on training data but poorly on unseen validation data.
Your Task
Reduce overfitting so that validation accuracy improves to at least 80% while keeping training accuracy below 90%.
You can only modify the model architecture and training hyperparameters.
Do not change the dataset or data preprocessing steps.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
PyTorch
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

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

# Load CIFAR-10 dataset
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
val_dataset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)

train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=64, shuffle=False)

# Define CNN model with dropout and batch normalization
class SimpleCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
        self.bn1 = nn.BatchNorm2d(32)
        self.dropout1 = nn.Dropout(0.25)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
        self.bn2 = nn.BatchNorm2d(64)
        self.dropout2 = nn.Dropout(0.25)
        self.fc1 = nn.Linear(64 * 8 * 8, 512)
        self.dropout3 = nn.Dropout(0.5)
        self.fc2 = nn.Linear(512, 10)

    def forward(self, x):
        x = self.pool(nn.functional.relu(self.bn1(self.conv1(x))))
        x = self.dropout1(x)
        x = self.pool(nn.functional.relu(self.bn2(self.conv2(x))))
        x = self.dropout2(x)
        x = x.view(-1, 64 * 8 * 8)
        x = nn.functional.relu(self.fc1(x))
        x = self.dropout3(x)
        x = self.fc2(x)
        return x

# Initialize model, loss, optimizer
model = SimpleCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
for epoch in range(10):
    model.train()
    running_loss = 0.0
    correct = 0
    total = 0
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item() * inputs.size(0)
        _, predicted = outputs.max(1)
        total += labels.size(0)
        correct += predicted.eq(labels).sum().item()
    train_loss = running_loss / total
    train_acc = 100. * correct / total

    model.eval()
    val_loss = 0.0
    val_correct = 0
    val_total = 0
    with torch.no_grad():
        for inputs, labels in val_loader:
            outputs = model(inputs)
            loss = criterion(outputs, labels)
            val_loss += loss.item() * inputs.size(0)
            _, predicted = outputs.max(1)
            val_total += labels.size(0)
            val_correct += predicted.eq(labels).sum().item()
    val_loss /= val_total
    val_acc = 100. * val_correct / val_total

    print(f'Epoch {epoch+1}: Train Loss={train_loss:.3f}, Train Acc={train_acc:.1f}%, Val Loss={val_loss:.3f}, Val Acc={val_acc:.1f}%')
Added Batch Normalization layers after each Conv2d layer to stabilize and speed up training.
Added Dropout layers after convolutional and fully connected layers to reduce overfitting.
Added MaxPooling layers to reduce spatial size and model complexity.
Used Adam optimizer with learning rate 0.001 for better convergence.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 65%, Training loss 0.05, Validation loss 1.2

After: Training accuracy 88%, Validation accuracy 82%, Training loss 0.25, Validation loss 0.65

Adding dropout and batch normalization reduces overfitting by preventing the model from memorizing training data. This improves validation accuracy and generalization.
Bonus Experiment
Try replacing the dropout layers with L2 weight regularization (weight decay) in the optimizer and compare results.
💡 Hint
Set weight_decay parameter in Adam optimizer to a small value like 0.0005 and remove dropout layers.

Practice

(1/5)
1. What does the nn.Conv2d layer in PyTorch primarily do?
easy
A. It increases the image size by adding pixels.
B. It slides filters over images to find patterns.
C. It converts images to grayscale.
D. It sorts images by color intensity.

Solution

  1. Step 1: Understand the role of convolution layers

    Convolution layers slide small filters over input images to detect features like edges or textures.
  2. Step 2: Match the function to the options

    Only It slides filters over images to find patterns. correctly describes this sliding filter action, while others describe unrelated image operations.
  3. Final Answer:

    It slides filters over images to find patterns. -> Option B
  4. Quick Check:

    Convolution = sliding filters [OK]
Hint: Conv2d = sliding filters over images to find features [OK]
Common Mistakes:
  • Thinking Conv2d changes image size by adding pixels
  • Confusing Conv2d with image color adjustments
  • Assuming Conv2d sorts or rearranges pixels
2. Which of the following is the correct way to create a Conv2d layer with 3 input channels, 16 output channels, and a 3x3 kernel in PyTorch?
easy
A. nn.Conv2d(3, 16, kernel_size=3)
B. nn.Conv2d(16, 3, kernel_size=3)
C. nn.Conv2d(3, 16, kernel=3)
D. nn.Conv2d(input=3, output=16, size=3)

Solution

  1. Step 1: Recall Conv2d constructor parameters

    The correct order is nn.Conv2d(in_channels, out_channels, kernel_size).
  2. Step 2: Check each option

    nn.Conv2d(3, 16, kernel_size=3) matches the correct parameter order and uses the correct keyword for kernel size. The other options have wrong parameter order or incorrect keywords.
  3. Final Answer:

    nn.Conv2d(3, 16, kernel_size=3) -> Option A
  4. Quick Check:

    Conv2d(in, out, kernel_size) = A [OK]
Hint: Remember Conv2d(in_channels, out_channels, kernel_size) [OK]
Common Mistakes:
  • Swapping input and output channels
  • Using wrong parameter names like 'kernel' instead of 'kernel_size'
  • Passing parameters as keywords not supported by Conv2d
3. What will be the output shape of the following PyTorch Conv2d layer when applied to an input tensor of shape (1, 3, 32, 32)?
conv = nn.Conv2d(3, 6, kernel_size=5)
output = conv(torch.randn(1, 3, 32, 32))
print(output.shape)
medium
A. torch.Size([1, 3, 28, 28])
B. torch.Size([1, 6, 32, 32])
C. torch.Size([6, 3, 28, 28])
D. torch.Size([1, 6, 28, 28])

Solution

  1. Step 1: Calculate output spatial size

    Output size = (Input size - Kernel size + 1) = (32 - 5 + 1) = 28 for both height and width.
  2. Step 2: Determine output channels and batch size

    Output channels = 6, batch size = 1, so output shape is (1, 6, 28, 28).
  3. Final Answer:

    torch.Size([1, 6, 28, 28]) -> Option D
  4. Quick Check:

    Output shape = (batch, out_channels, 28, 28) [OK]
Hint: Output size = input - kernel + 1 if stride=1, padding=0 [OK]
Common Mistakes:
  • Assuming output size equals input size without padding
  • Mixing up input and output channels in shape
  • Forgetting batch size dimension
4. Identify the error in this Conv2d layer definition:
conv = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=3)
output = conv(torch.randn(1, 3, 28, 28))
print(output.shape)
medium
A. Stride cannot be 2 in Conv2d.
B. Input tensor shape is incorrect for 3 input channels.
C. Padding is too large causing output size to increase unexpectedly.
D. Kernel size must be an odd number.

Solution

  1. Step 1: Calculate output size with given parameters

    Output size formula: floor((Input + 2*padding - kernel_size)/stride) + 1 = floor((28 + 6 - 3)/2) + 1 = floor(31/2) + 1 = 15 + 1 = 16.
  2. Step 2: Understand padding effect

    Padding=3 is large for kernel=3, causing output spatial size to increase unexpectedly, which is unusual and may cause unexpected behavior.
  3. Final Answer:

    Padding is too large causing output size to increase unexpectedly. -> Option C
  4. Quick Check:

    Large padding inflates output size [OK]
Hint: Check padding size relative to kernel size for output shape [OK]
Common Mistakes:
  • Thinking stride=2 is invalid
  • Assuming input shape is wrong for 3 channels
  • Believing kernel size must be odd always
5. You want to design a Conv2d layer that keeps the input image size (28x28) unchanged after convolution with a 5x5 kernel and stride 1. Which padding value should you use?
hard
A. Padding = 2
B. Padding = 1
C. Padding = 0
D. Padding = 3

Solution

  1. Step 1: Use output size formula for Conv2d

    Output size = floor((Input + 2*padding - kernel_size)/stride) + 1. We want output = input = 28, stride=1, kernel=5.
  2. Step 2: Solve for padding

    28 = (28 + 2*padding - 5) + 1 -> 28 = 24 + 2*padding -> 2*padding = 4 -> padding = 2.
  3. Final Answer:

    Padding = 2 -> Option A
  4. Quick Check:

    Padding 2 keeps size with 5x5 kernel [OK]
Hint: Padding = (kernel_size - 1) / 2 for same size [OK]
Common Mistakes:
  • Using zero padding and expecting same size
  • Choosing padding less than 2 for 5x5 kernel
  • Confusing stride effect with padding