Ravindra BagaleCourses & study guides मराठी Track your progress

47. Convolutional Neural Networks (CNNs) for Images

Data Science

47.4 CNN Stack Overview

Why. Real CNNs stack conv → activation → pool (repeated), then flatten into a small classifier head.

What. Pattern: extract features with conv blocks, then nn.Linear layers for class scores (pedha vs chips on a fictional shelf).

How.

Sketch the stack

  1. Conv + ReLU + Pool
  2. Conv + ReLU + Pool
  3. Flatten
  4. Linear → class logits
import torch.nn as nn

class TinyCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 8, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
            nn.Conv2d(8, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
        )
        self.head = nn.Linear(16 * 7 * 7, 2) # for 28x28 input
    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        return self.head(x)

print(TinyCNN())

Next chapter

Chapter 48 trains a similarly tiny CNN on fake tensors (not a huge ImageNet download).