← Master Index
Vol. 07 Module 7.1 Lecture

Filters

CNN Core Concepts

How This Lesson Fits the Module

The kernel lecture introduced a single 2D weight grid. Real convolution layers use filters—stacks of kernels spanning every input channel—and many of them. Filters are what turn one image into a rich set of feature maps. Getting the channel bookkeeping right here makes every later layer’s shape obvious.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define a filter as a stack of kernels across all input channels.
  • Relate the number of filters to the number of output channels.
  • Compute a convolution layer’s parameter count from its channel and kernel dimensions.
  • Explain what each filter learns to detect and how depth composes them.
  • Configure filter counts in PyTorch and read the resulting weight tensor.
  • Reason about the cost/accuracy tradeoff of widening (more filters) a layer.
Definition

A filter is a 3D block of weights of shape C_in × k × k. It convolves over all input channels simultaneously and produces exactly one output channel (one feature map). A conv layer holds C_out such filters.

From One Filter to Many Channels

Each filter looks for a different pattern. When a layer has 32 filters, it produces 32 feature maps—32 different “views” of the same input. The output channel count of a layer is simply the number of filters it contains.

Input

C_in channels (e.g. 3 RGB).

Each filter

Spans all C_in channels.

Sum

Per-channel responses add into one map.

Stack

C_out filters → C_out feature maps.

Counting Parameters

Formula

Weights in a conv layer: C_out × C_in × k × k, plus C_out bias terms.

LayerC_inC_outkParameters (with bias)
Conv1316316·3·3·3 + 16 = 448
Conv21632332·16·3·3 + 32 = 4,640
Conv33264364·32·3·3 + 64 = 18,496

Compare 4,640 weights for Conv2 to the millions a single dense layer on an image would use. This is weight sharing paying off.

Configuring Filters in PyTorch

The second argument to nn.Conv2d is out_channels—the number of filters.

import torch from torch import nn # 32 filters, each 3x3 across 16 input channels layer = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1) print(layer.weight.shape) # torch.Size([32, 16, 3, 3]) -> 32 filters print(sum(p.numel() for p in layer.parameters())) # 4640 x = torch.randn(8, 16, 28, 28) print(layer(x).shape) # torch.Size([8, 32, 28, 28]) -> 32 feature maps

What Filters Learn

Early Filters

  • Oriented edges.
  • Color contrasts.
  • Simple gradients.

Middle Filters

  • Textures and corners.
  • Repeated motifs.
  • Combinations of edges.

Deep Filters

  • Object parts (eyes, wheels).
  • Class-specific structures.
  • Highly abstract features.

More Filters: Wider, Not Deeper

More filters help

  • Richer feature vocabulary.
  • Higher capacity per layer.
  • Often improves accuracy.

But cost rises

  • Parameters and FLOPs grow.
  • More memory for activations.
  • Overfitting risk without data/regularization.
Common Misconception

“Each filter processes one channel.” No—each filter spans all input channels and collapses them into one output map. It is the number of filters that sets output channels, not the number of input channels.

Knowledge Check

  1. Short Answer: What is a filter in a conv layer? Answer: A stack of kernels spanning all input channels that produces one output channel.
  2. True/False: The number of filters equals the number of output channels. Answer: True.
  3. Multiple Choice: Conv2d(16, 32, 3) weight shape is: (a) (32,16,3,3), (b) (16,32,3,3), (c) (32,3,3). Answer: (a).
  4. Short Answer: Parameters (no bias) in Conv2d(8, 16, 3)? Answer: 16 × 8 × 3 × 3 = 1,152.
  5. True/False: A single filter produces multiple feature maps. Answer: False—one filter produces one feature map.
  6. Multiple Choice: Adding more filters makes a layer: (a) deeper, (b) wider, (c) shallower. Answer: (b).
  7. Short Answer: Which argument of nn.Conv2d sets the filter count? Answer: out_channels (the second positional argument).
  8. Short Answer: Why do conv layers have far fewer parameters than dense layers on images? Answer: Filters are small and shared across all spatial positions.
  9. True/False: Deep filters tend to represent object parts. Answer: True.
  10. Multiple Choice: Each filter’s depth matches: (a) output channels, (b) input channels, (c) kernel size. Answer: (b).

Key Takeaways

  • A filter stacks kernels over all input channels and outputs one feature map.
  • Number of filters = number of output channels = feature-map count.
  • Layer weights = C_out × C_in × k × k (+ biases), far fewer than dense layers.
  • Filters progress from edges to textures to object parts with depth.
  • Next, the Feature Map lecture examines the filters’ output.
Trainer’s Guide

Hands-on idea: Have students build three conv layers, print each weight shape and total parameter count, and confirm the formula by hand.

Discussion prompt: When would you widen a layer (more filters) versus deepen the network (more layers)?

Recap: Filters are multi-channel weight blocks; the count of filters sets how many feature maps a layer emits. Continue with Feature Map.