Neural Networks Overview
Lecture 8: Convolutional Neural Networks (Deep Learning)
Introduction to Deep Neural Networks
Neural networks with more than one hidden layer are called deep neural networks. The previous lecture covered two-layer neural networks: a hidden layer that learns new features (fully connected) and an output layer for classification or regression. Deep neural networks extend this by stacking multiple hidden layers, allowing the network to learn increasingly abstract features. This lecture focuses on a particularly successful type of deep network: convolutional neural networks (CNNs, ConvNets) , which are a hypothesis class for tasks like image classification and regression.
Key layers that define CNNs include filters (also called kernels) and max pooling. Learning in CNNs uses gradient descent, stochastic gradient descent, and backpropagation, though the gradients differ from those in fully connected networks due to the convolution operation.
Motivation: The ImageNet Breakthrough
The modern AI boom was significantly driven by the performance of deep CNNs on the ImageNet Large Scale Visual Recognition Challenge. This challenge involved ~1000 object categories (e.g., strawberry, cow, stop sign) and millions of labeled images. In 2011, the best methods (using support vector machines) still had error rates above 20%. In 2012, AlexNet, a deep CNN with approximately 8 layers, achieved a dramatic error reduction, outperforming all other approaches. By 2013, most entries were CNNs; by 2015, Microsoft’s ResNet had over 100 layers and surpassed human-level accuracy. This breakthrough was not instantaneous—foundational work existed decades earlier (neocognitron in 1980, LeCun’s CNNs for digit recognition in 1989). What changed was:
Better computation: GPUs massively parallelized the matrix operations central to CNNs.
More labeled data: Crowdsourcing platforms like Amazon Mechanical Turk and the growth of internet images provided large-scale datasets.
CNNs are now essential in medical imaging (tumor detection), image search, autonomous driving (stop sign detection), and many other fields.
Why CNNs for Images?
When working with images, prior knowledge can be encoded into the network architecture, reducing the need to relearn patterns from scratch. Two key properties of images are:
Spatial locality: Objects (e.g., a stop sign) consist of pixels that are close together. The relevant features are local, not scattered across the image.
Translation invariance: The same object (e.g., a stop sign) should be recognized regardless of its position in the image. A network should not need to learn a completely new representation for each location.
Fully connected layers treat every pixel independently; pixel 1 and pixel 20 have different weights. They cannot exploit locality or translation invariance. CNNs address this through filters (convolutions) and max pooling, which detect patterns locally and share weights across spatial positions.
Encoding Images as Inputs
Images are made of pixels. For grayscale images, each pixel takes a value between 0 (black) and P (white). For simplicity, this lecture uses binary images (0 and 1). A typical image of size 5×5 can be flattened into a vector x=[x1,x2,…,x25] by ordering pixels in a fixed sequence. This vector serves as input to a neural network. The flattening order is not critical for encoding, but influences the spatial structure exploited by convolutions.
From Fully Connected to Convolutional Layers
In a two-layer fully connected neural network, every input is connected to every hidden unit by a weight. The hidden layer learns features by linearly combining all inputs. The output layer then uses these features for classification (e.g., logistic regression) or regression. This lecture leaves the output layer unchanged but modifies the feature-learning layers to incorporate spatial structure.
Fully connected layers ignore image geometry: they treat each pixel as an independent feature. In contrast, convolutional layers apply the same small set of weights (a filter) across all spatial locations, detecting patterns like edges or corners regardless of position. This weight sharing dramatically reduces the number of parameters.
Convolution Operation (1D Example)
A filter is a small vector (in 1D) or matrix (in 2D) of weights. The convolution operation slides the filter across the input, computing a dot product at each position.
For a 1D image x=[x1,x2,…,xn] and a filter of size 3 with weights [w1,w2,w3] and bias b, the pre-activation at position i is:
(assuming appropriate padding). This is exactly a dot product between the filter and a 3‑pixel window of the image. After convolution, an activation function (commonly ReLU, ReLU(z)=max(0,z)) is applied element-wise:
Example: With image [0,0,1,1,1,0,1,0,0,0] and filter [−1,1,−1] (no bias), the convolution yields:
For window [0,0,1]: 0⋅(−1)+0⋅1+1⋅(−1)=−1
For window [0,1,1]: 0⋅(−1)+1⋅1+1⋅(−1)=0
For window [1,1,1]: 1⋅(−1)+1⋅1+1⋅(−1)=−1
Continue across the image.
After ReLU, only positive values remain. In this case, the filter detects isolated 1s (e.g., a satellite passing quickly across a telescope). The filter finds a “lonely 1” flanked by zeros.
Padding and Bias
Without padding, the filter cannot be centered at the edges of the image, reducing the output size. Zero padding adds zeros around the image, allowing convolution at every pixel and preserving the spatial dimensions. Padding is a design choice; for many applications, padding is set so the output has the same size as the input.
Adding a bias term shifts the pre-activation: zi=dot product+b. The bias works exactly like the intercept in logistic regression, giving the filter more expressive power.
Number of Parameters: Convolutional vs Fully Connected
Consider a 1D image of length 10. A convolutional layer with a filter of size 3 and a bias has 4 parameters (3 weights + 1 bias). These weights are reused for every window.
A fully connected layer with 10 inputs and 10 outputs has 110 parameters (10 outputs × 10 weights + 10 biases). The savings are enormous. Even on this small example, the reduction is 110 vs 4. For larger images (e.g., 1000×1000), the difference is astronomical. Parameter sharing is the key: the CNN learns one pattern and applies it everywhere, enforcing translation invariance and reducing overfitting.
Two-Dimensional Convolution
For 2D images, filters are 2D matrices. A 3×3 filter slides over the image, computing a dot product at each position. The operation is equivalent to flattening the 3×3 window and the filter into vectors and taking the dot product.
Example: A 5×5 image of the word “hi” (zeros and ones) is convolved with a 3×3 filter:
This filter has -1 on the boundary and +1 at the center. The convolution output at each position is:
Count the number of 1s in the 3×3 window: nones.
Compute: (−1)⋅(ones outside center)+1⋅(center value).
For a window where the center pixel is 1 and all neighbors are 0, the result is +1 (since −1⋅0+1=1). If the center is 0 and there are several surrounding 1s, the result is negative.
After ReLU, only positive values remain. This particular filter acts as a dot detector — it activates strongly for an isolated 1 (like the dot on the letter “i”). Without padding, the dot at the image edge would be missed; padding allows detection everywhere.
Filters can have arbitrary real weights; they are learned from data. The general 3×3 filter has 9 weights w1 through w9 and a bias b. Filter size (e.g., 2×2, 4×4) is a hyperparameter.
Depth and Tensors
Real images are often three-dimensional: width, height, and depth (e.g., 3 channels for RGB color). A tensor is the generalization of a matrix to three or more dimensions. Convolution filters can also have depth. A 3D filter of size 3×3×3 applied to an RGB image will have 27 weights (3×3×3) plus bias.
Even when working with 2D grayscale images, using multiple filters in a layer creates a filter bank. Each filter produces a 2D output (a channel). Stacking these channels yields a 3D tensor. For example, 10 filters applied to a 2D image produce an output tensor of depth 10. This is common in CNNs: the first layer may detect edges, the next layer detects combinations of edges, etc.
Max Pooling
Max pooling downsamples the feature maps by taking the maximum value within a small window (e.g., 2×2 or 3×3). It answers: “In this local region, was any feature detected?” Max pooling:
Reduces spatial dimensions, making the network more efficient.
Provides a degree of translation invariance — small shifts of the input do not change the pooled output much.
Has no learnable parameters; the window size and stride (step size) are fixed.
Stride controls how the window moves. Stride 1 means the window shifts by one pixel each time; stride equal to the window size gives non-overlapping regions. Example: with a 3×3 window and stride 3, the image is partitioned into disjoint blocks, each yielding the maximum value. This dramatically reduces size.
For an activation map containing a single “1” and many zeros, max pooling with a 3×3 window and stride 3 will output a 1 only in the block containing that activation. The output is a smaller grid summarizing where detections occurred.
Typical CNN Architecture
A standard CNN for image classification follows this pattern:
Input: a 3D image tensor (height, width, depth = 3 for RGB).
Convolution + ReLU: Apply a bank of filters (e.g., 32 filters of size 3×3×3), producing a 3D output of depth 32. Apply ReLU element-wise.
Max pooling: Reduce spatial size (e.g., 2×2 window, stride 2).
Repeat convolution + ReLU + max pooling several times. Each layer learns higher-level features: edges → textures → object parts → whole objects.
Flatten: Convert the final 3D tensor into a long vector.
Fully connected layer: A standard logistic regression (or linear regression) layer that maps features to class scores.
Softmax (for classification): Convert scores into probabilities.
This architecture is a deep neural network because it contains multiple hidden layers (the convolutional and pooling blocks). The output layer remains the same as in earlier models; the novelty lies in how features are learned.
Learning in CNNs: Forward Pass and Backpropagation
The learning pattern is identical to previous models:
Choose a prediction function (forward model) that depends on the data and parameters.
Choose a loss function (e.g., squared error for regression, negative log likelihood for classification).
Minimize the training loss using gradient descent or stochastic gradient descent.
The forward pass of a simple 1D CNN for regression (no bias, single filter of size 3, padding) can be written as:
Pre-activations: zi(1)=w1(1)xi−1+w2(1)xi+w3(1)xi+1, for i=1,…,5 (using zero-padded x0,x6).
Activations: ai(1)=ReLU(zi(1)).
Output: y^=∑i=15wi(2)ai(1).
Loss: L=(y^−y)2.
To perform gradient descent, we need derivatives of L with respect to w(1) and w(2). Backpropagation applies the chain rule. For w(1):
The dimensions work out: w(1) is length 3, z(1) is length 5, a(1) is length 5, L is scalar. The result ∂w(1)∂L is a 3‑vector.
The key term ∂w(1)∂z(1) is a 3×5 matrix. For example, z2=w1x1+w2x2+w3x3, so ∂w3∂z2=x3. Each entry is a shifted copy of the input (or padding), leading to a “wave” of x values. This structure differs from fully connected networks and reflects the weight sharing in the convolution.
Backpropagation for CNNs follows the same principle as for fully connected networks but computes gradients for filters instead of full weight matrices. Modern deep learning frameworks (TensorFlow, PyTorch) compute these gradients automatically.
Summary
Deep neural networks extend two-layer networks by adding more hidden layers, learning hierarchical features.
Convolutional neural networks leverage spatial locality and translation invariance through filters (convolutions) and max pooling.
Filters are small weight vectors/matrices/tensors that slide over the input, sharing parameters across positions. This dramatically reduces the number of parameters compared to fully connected layers.
Padding preserves spatial size; bias shifts activation values.
Multiple filters form a filter bank; each filter detects a different pattern.
Max pooling downsamples and provides invariance.
A typical CNN stack includes convolution + ReLU and pooling layers, followed by flattening and a fully connected output layer.
Learning uses the same loss-minimization framework (gradient descent, backpropagation) as earlier models, but gradients for the filter weights reflect the convolution operation.
CNNs were central to the modern AI revolution, starting with AlexNet’s 2012 ImageNet victory, made possible by GPUs and large datasets. They are now used across medical imaging, autonomous driving, and countless other applications.

Ask anything
We've read this whole study set, so questions about it get the best answers.