Neural Networks & Gradient Descent
Neural Network Learning via Gradient Descent
Recap of Neural Network Structure
A neural network for handwritten digit recognition takes input images as a 28x28 pixel grid, each pixel having a grayscale value between 0 and 1. These values determine the activations of 784 neurons in the input layer. Each neuron in subsequent layers computes a weighted sum of all activations from the previous layer, plus a bias, and then applies an activation function such as the sigmoid or ReLU.
A typical network might have two hidden layers of 16 neurons each, followed by an output layer of 10 neurons (one for each digit 0–9). The digit classification is determined by the output neuron with the highest activation. This layered structure is motivated by the hope that lower layers detect edges, middle layers detect patterns (loops, lines), and higher layers combine those patterns to recognize digits.
The network contains approximately 13,000 weights and biases — parameters that define the function it computes.
Cost Function: Measuring Performance
To train the network, weights and biases are initialized randomly, producing poor output. For a given training example (an image with a known label), the desired output is a vector of activations where the correct digit’s neuron is 1 and all others are 0. The cost function quantifies how far the actual output is from the desired output.
For a single training example x, the cost is:
where ai(x) is the network’s activation for output neuron i, and yi(x) is the target (0 or 1). This sum is small when the network correctly classifies the example, large when it is wrong.
The overall cost is the average over all training examples:
This cost function takes the 13,000 weights and biases as inputs and outputs a single number describing the network’s overall lousiness.
Gradient Descent: The Learning Algorithm
Minimizing the cost function by hand is impossible for such a high-dimensional space. Gradient descent is an iterative method that finds a local minimum.
Single-Variable Intuition
For a function f(x) with one input, compute the slope (derivative) at the current point. If the slope is positive, move left (decrease x); if negative, move right (increase x). Repeat until reaching a flat region. Step sizes proportional to the slope help avoid overshooting as the function flattens. This process leads to a local minimum, which may not be the global minimum.
Two-Variable Extension
For a function f(x,y), the gradient ∇f is a vector pointing in the direction of steepest ascent. Its negative −∇f points in the direction of steepest descent. The magnitude of the gradient indicates the steepness.
At each step:
Compute the gradient of the cost function.
Take a small step in the direction −∇f.
Repeat until convergence.
Gradient descent is like a ball rolling down a hill — it follows the downward slope until it settles in a valley.
Application to Neural Networks
The cost function C depends on all 13,000 weights and biases. Organize them into a single vector. The negative gradient −∇C is a vector that tells which nudges to each weight and bias will most rapidly decrease the cost. Each component of this vector indicates:
Sign: whether to increase or decrease that parameter.
Relative magnitude: how much a change to that parameter matters compared to others — some weights carry more “bang for your buck.”
Because gradient descent requires smooth, continuous outputs, artificial neurons use continuous activation functions (e.g., sigmoid, ReLU) rather than binary on/off states like biological neurons.
Backpropagation: Computing the Gradient Efficiently
The algorithm that computes the gradient of the cost function with respect to every weight and bias is called backpropagation. It is the heart of how a neural network learns. Backpropagation works by propagating error signals backward through the network, using the chain rule of calculus. (Details are covered in dedicated materials.)
Performance on Unseen Data
A network with two hidden layers of 16 neurons achieves about 96% accuracy on the MNIST test set (handwritten digits it has never seen). With tuning of hidden layer structure, accuracy can reach 98%. While not state-of-the-art, this demonstrates that gradient descent can produce a network that generalizes well from training data.
What the Hidden Layers Actually Learn
When visualizing the weights from the input layer to a neuron in the second hidden layer (treating the weights as a pixel pattern), the patterns do not resemble neat edge detectors. Instead, they appear almost random, with only loose central patterns. This indicates that the network found a local minimum that, while effective for classification, does not match the intuitive feature hierarchy (edges → patterns → digits) that motivated the layered architecture.
Furthermore, when fed a completely random image, the network confidently classifies it as a digit — it has no concept of uncertainty because the cost function never penalized being wrong on such inputs. The network learned to recognize digits within a tightly constrained universe (centered, clear digits on a small grid) but has no understanding of how to generate or generalize beyond that.
This classical neural network (from the 1980s–90s) is capable of solving interesting problems, but its internal representations are far less intelligent than one might hope.
Deeper Insights: Memorization vs. Learning
Recent research highlights boundary between memorization and true pattern recognition in neural networks.
Shuffled labels experiment: When training a deep network on a dataset where labels are randomly shuffled, the network can still achieve near-perfect training accuracy simply by memorizing the data. Its test accuracy remains at chance level.
Optimization landscape: On structured datasets, the cost function’s gradient descent converges quickly (a sharp drop in accuracy curve). On random labels, convergence is much slower and more linear — the network struggles to find a good local minimum.
Quality of local minima: Several studies suggest that the local minima that neural networks tend to find are of roughly equal quality. When the data has structure, those minima correspond to genuine features; when data is random, the minima correspond to memorization.
This distinction is crucial: minimizing the cost function does not guarantee learning of meaningful patterns — it can simply memorize. The shape of the optimization landscape and the nature of the data determine which behavior emerges.
Resources for Further Study
Michael Nielsen’s free online book Neural Networks and Deep Learning provides code and data for the MNIST example.
Chris Olah’s blog posts and the Distill journal offer visual and intuitive explanations of neural network internals.
The interview snippet with Leisha Lee discusses recent papers examining what modern image recognition networks actually learn, and the implications of the shuffled-labels experiment.

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