Deep Learning Notes - 4: Gradient Descent, Backpropagation, and Regularization
Deep Learning Notes - 4, covering gradient descent, backpropagation, and regularization. Corresponding to Chapters 7-9 of "Deep Learning: Foundations and Concepts".
Part 4/8 of the series ← Previous | Next | Glossary
It is recommended to read Parts 1-3 first, especially the error function and regularization concepts in Part 1. This part covers how neural networks are trained (gradient descent + backpropagation) and how to prevent overfitting (regularization).
Chapter 7 Gradient Descent
Intuition first — imagine you are standing on a large mountain shrouded in dense fog, unable to see anything. You want to get down to the valley (the point of minimum error). What do you do? You feel the slope under your feet with your foot — and take a step in whichever direction is lower. By repeating this, you will eventually reach the bottom of the valley. This is the core idea of gradient descent (Gradient Descent, an optimization algorithm that minimizes the error by iteratively updating parameters along the direction of steepest error decrease).
For gradient-related material, it is recommended to study “Introduction to Optimization”. Well, I myself only managed to learn the basics of the first few chapters (lol)
Error Surface
Our goal is to find a set of parameter vectors (a vector consisting of weights and biases, which can be thought of as the network’s “knobs”) such that the error function (a function measuring the gap between the network’s predictions and the true values) is minimized. We can imagine as a “topographic map”, where the horizontal axis is the high-dimensional weight space and the vertical axis is the error value. At any point on the mountain, you can feel the “slope” under your feet — this is the gradient (Gradient, the direction and magnitude of the fastest change of a function at a given point).
- At any point , the gradient of the error function points in the direction of fastest error increase (i.e., the “uphill” direction).
- To reduce the error, we should update the weights along the opposite direction of the gradient (i.e., “downhill”): where is the learning rate (Learning Rate, a hyperparameter controlling how far to step at each iteration). The learning rate is like your “step size” — too large and you may leap across the valley onto the opposite mountain (diverging); too small and you walk too slowly and give up before reaching the bottom.

Local minimum (wA), global minimum (wB), and how the gradient direction (∇E) guides the update direction
When the gradient is zero (), we have reached a stationary point (Stationary Point, a point where the gradient is zero), which may be a local minimum, a local maximum, or a saddle point (Saddle Point, a point that is a minimum in one direction and a maximum in another, shaped like a saddle). Ideally, we want to find the global minimum, but in practice we are often content with a good local minimum.
Local Quadratic Approximation
Click to expand: mathematical derivation of the local quadratic approximation (advanced content)
To gain a deeper understanding of the optimization process, we can perform a Taylor expansion (Taylor Expansion, a method of approximating complex functions with polynomials) of the error function near a point , keeping terms up to the second order:
where is the gradient, and is the Hessian matrix (Hessian Matrix, a square matrix composed of the second-order partial derivatives of the error function with respect to all parameters, describing the local curvature of the error surface).
If is a minimum point, then the gradient , and the approximation becomes:
The eigenvectors of the Hessian form an orthogonal basis. Let be the component of in the direction of , then:
where is the eigenvalue (Eigenvalue, the scaling factor of a matrix along a specific direction).
- If all , then is a local minimum.
- If all , then it is a local maximum.
- If there are both positive and negative values, it is a saddle point.
The Hessian being positive definite (all ) is a necessary and sufficient condition for a local minimum.

Near a minimum, the contour lines of equal error are ellipses, whose principal axes are determined by the Hessian eigenvectors
Gradient Descent Optimization
Because the error function of a neural network is extremely complex (with parameter counts often in the millions), we cannot directly solve the equation , and must use iterative numerical methods (Iterative Numerical Method, a method that gradually approaches the optimal solution by repeatedly updating parameters).
General iterative formula:
where denotes the iteration step (Iteration, each parameter update counts as one step).
Use of Gradient Information
Gradient information can significantly improve optimization efficiency. The reasons are as follows:
- Without using the gradient: requires function evaluations ( being the number of parameters), each evaluation being , for a total cost of .
- Using the gradient: each gradient evaluation provides pieces of information, so in theory evaluations are enough to locate the minimum. Combined with efficient backpropagation ( ), the total cost drops to .
Therefore, the gradient is the foundation of training neural networks.
Batch Gradient Descent (Steepest Descent)
The simplest form of gradient descent, which uses the entire training set to compute the gradient:
where is the error over the full dataset. Each update requires iterating over all the data, which is computationally expensive.
Stochastic Gradient Descent (SGD)
Intuition: Batch gradient descent is like wanting to survey the average height of people across the whole country — you have to ask everyone, which is too slow. Stochastic gradient descent (Stochastic Gradient Descent, which randomly draws a single sample each time to estimate the gradient) is like random sampling — although each estimate is less accurate, it is much faster, and if you sample a few more times the overall trend is correct.
To address the inefficiency of batch gradient descent, SGD updates using only one data point (randomly) at a time:
Algorithm steps:
- Set the current data point index
- Repeat the following process until convergence:
- Update the weight vector: (i.e.: update the weights along the opposite direction of the gradient for the current data point)
- Update the data point index: (i.e.: iterate through all data points, reusing them cyclically. denotes taking the remainder; when the index n reaches the dataset size N, the modulo operation resets it to 0)
- Return the final weight vector
Advantages:
- High computational efficiency, suitable for large-scale data.
- Gradient noise helps escape local minima and saddle points.
- Insensitive to redundant data (e.g., duplicated data does not affect SGD).
Disadvantage: the gradient estimate has high variance, and the update path oscillates.
Mini-Batch
A compromise between SGD and batch gradient descent: each time a mini-batch (Mini-Batch, a small batch of data samples, e.g., 32 or 64) is used to compute the gradient.
- The gradient estimate is more stable (variance is proportional to , where is the batch size).
- Can efficiently exploit hardware parallelism (e.g., GPU).
- A batch size that is a power of 2 is recommended (e.g., 32, 64, 128) to optimize memory access.
Algorithm steps:
- Set the starting index of the current data point
- Repeat the following process until convergence:
- Update the weight vector: compute the gradient using the current mini-batch of data and update the weights: (i.e.: update the model parameters using the average gradient of the current mini-batch)
- Move to the next mini-batch: the B samples starting from index n
- If all data has been traversed:
- Shuffle the training data order, to prevent correlations between samples from affecting convergence
- Reset the starting index:
- Continue the loop
- Return the final weight vector
Key point: shuffle the data randomly before each iteration, to avoid correlations between samples affecting convergence.
Parameter Initialization
- Symmetry breaking: If all weights are initialized to the same value (e.g., 0), the symmetry causes hidden units to learn the same features, becoming redundant. Therefore, random initialization is needed.
- Common distributions: uniform distribution or Gaussian distribution .
- He initialization: For the ReLU (Rectified Linear Unit, an activation function defined as ) activation function, is recommended, where is the number of neurons in the previous layer. This keeps the variance of the signal stable as it propagates between layers.
- Bias initialization: Usually set to a small positive value (e.g., 0.1), especially for ReLU, to ensure the initial activation is non-zero and facilitate gradient flow.
Convergence Analysis
Valley problem:
When the error surface has vastly different curvature in different directions (such as a “valley” shape), standard gradient descent is inefficient:
- Learning rate too small: convergence along the valley bottom direction is extremely slow.
- too large: oscillates between the valley walls, or even diverges.

Valley problem
Momentum
Intuition: Imagine a ball rolling down a mountain. Ordinary gradient descent is like a point without inertia — each step only looks at the current slope. Momentum (Momentum, a technique that uses the “inertia” of historical update directions to accelerate convergence) is like adding inertia to the ball — even if it encounters small pits and bumps, the ball can rush through them by inertia, and only stops at a truly flat valley bottom.
To accelerate convergence and suppress oscillation, a momentum term is introduced:
where is the momentum coefficient (, usually taken as 0.9). preserves the “inertia” of previous update directions.
The learning rate effectively increases from to
- Low-curvature direction: the gradient direction is stable, momentum accumulates, the effective learning rate increases, and progress accelerates.
- High-curvature direction: the gradient direction changes frequently, momentum terms cancel each other out, the effective learning rate approaches , suppressing oscillation.
Momentum is similar to inertia in physics, making updates smoother and convergence faster.

Momentum helps cross the valley faster
Nesterov momentum: An improved momentum method: first “pre-step” according to the historical momentum, then compute the gradient at that position.
- Corrects the direction more promptly, reducing overshoot.
- Has a faster theoretical convergence rate, and often outperforms standard momentum in practice.
Adaptive Learning Rate Algorithms
Intuition: The previous algorithms use the same learning rate for all parameters, but different parameters may need different step sizes — some parameters are already close to optimal and need small, slow steps; others are still far off and need large steps forward. The core idea of adaptive learning rate algorithms is to “teach students according to their aptitude” — automatically assigning an appropriate learning rate to each parameter.
AdaGrad
AdaGrad (Adaptive Gradient) maintains an independent accumulated squared gradient for each parameter:
For the -th parameter , an value is maintained, which records the sum of squares of all its historical gradients.
The learning rate is adjusted to:
where is a small constant to prevent division by zero.
- For parameters that are updated frequently (large gradients): grows fast, the learning rate drops fast, and the update step becomes smaller.
- For parameters that are rarely updated (small gradients): grows slowly, the learning rate stays relatively large, and the update step is larger.
Disadvantage: keeps accumulating, causing the learning rate to decrease monotonically, and training nearly stalls in the later stages.
RMSProp
RMSProp (Root Mean Square Propagation) improves on AdaGrad by using an exponential moving average (Exponential Moving Average, an averaging method where recent data has greater weight and distant data is gradually “forgotten”) to compute the squared gradient:
where (decay rate, taking values in 0~1, usually taken as 0.9) controls the “forgetting” speed — the larger is, the more it relies on historical gradients; the smaller is, the more it values the current gradient.
Update formula:
- If a parameter keeps changing a lot (large gradient) → r is large → learning rate becomes smaller → update step becomes smaller
- If a parameter rarely changes (small gradient) → r is small → learning rate is larger → update step is larger It can “forget” early gradients, avoiding premature decay of the learning rate. It always remembers all the paths previously traveled, becoming more and more cautious, with smaller and smaller steps.
Adam
Intuition: Adam (Adaptive Moment Estimation) is like a “smart climber” — it both remembers the directions it has walked before (momentum / first moment) and observes the ups and downs of the terrain underfoot (adaptive learning rate / second moment), automatically adjusting the step size according to the road conditions. It is currently the most commonly used optimizer, virtually without equal.
Combining momentum and RMSProp, where and are the decay rates of the first and second moments respectively (usually , ):
First moment (momentum — remembers the directional trend of the gradient):
Second moment — remembers the magnitude variation of the gradient:
Bias correction: at the beginning, because initialization is zero, the first few estimates are not very accurate and need correction:
Final update formula:
- : tells us which direction to go (momentum)
- : tells us how large a step each parameter should take (adaptive learning rate)
Normalization Techniques
Intuition: Imagine an assembly line where the parts (data) handled at each station (layer) vary greatly in size — some huge, some tiny, making it hard for the workers to handle. Batch normalization (Batch Normalization, standardizing the data of each mini-batch) is like adding a “standardization machine” in front of each station, unifying the part sizes into a reasonable range, so the workers (the network) can work efficiently.
Batch Normalization
Normalize each feature of each mini-batch (subtract the mean, divide by the standard deviation), then scale and shift using learnable parameters (scale and shift parameters, letting the network itself decide the optimal data range).
Advantages:
- Reduces internal covariate shift (Internal Covariate Shift, the phenomenon where the input distribution of each layer of the network keeps changing during training).
- Allows a larger learning rate, accelerating convergence.
- Has a certain regularization effect.
Layer Normalization
Layer Normalization (Layer Normalization) normalizes a single sample across all its features, without depending on the batch, and is suitable for RNNs (Recurrent Neural Networks) and Transformers (an architecture based on the attention mechanism).

Left: batch normalization, right: layer normalization.
Exercise 1
When training a network, you find that the loss drops rapidly in the first 100 epochs, then stops moving. What’s going on?
There are quite a few possible reasons; some common ones:
- The learning rate is too large, bouncing back and forth at the valley bottom without going down; or too small, walking so slowly it seems like nothing is moving
- The model capacity is insufficient — it has already “done its best”, and further training won’t improve it
- It has already overfit: training loss is low but validation loss is high, meaning the model is “memorizing answers” rather than “learning patterns”
- The data itself is noisy or the labels are problematic, so the model can’t learn
If you switch from full-batch to mini-batch (batch size = 32), how would the loss curve look different?
The curve would become a bit more “jittery” — because each update only looks at 32 samples, the gradient estimate is noisy. But this noise actually has a benefit: it makes it less likely to get stuck in a local minimum, and each step is computed much faster, so overall it may converge faster instead.
Chapter 7 Summary
One-sentence version: Gradient descent is “going down the mountain” — walking along the steepest direction; SGD is “sampling estimation” — looking at only a few points each time to decide the direction; momentum is “a ball rolling down a slope” — using inertia to rush through small pits; Adam is a “smart climber” — automatically adjusting the step size; normalization is a “standardized assembly line” — keeping data within a reasonable range.
Knowledge map:
Gradient descent optimization├── Basis: error surface → gradient points uphill → go downhill in the opposite direction├── Data strategy│ ├── Batch gradient descent: use all data (slow but accurate)│ ├── SGD: use a single sample (fast but noisy)│ └── Mini-batch: compromise (most commonly used in practice)├── Solve oscillation → Momentum (inertia acceleration)├── Solve learning rate selection → Adaptive algorithms│ ├── AdaGrad: accumulate squared gradient (decays to zero)│ ├── RMSProp: exponential moving average (can "forget")│ └── Adam: momentum + adaptive (default first choice)├── Normalization techniques│ ├── Batch normalization: standardize across samples│ └── Layer normalization: standardize within a single sample└── Parameter initialization → He initialization, symmetry breakingChapter 8 Backpropagation
Computing the Gradient
Intuition first: Imagine a factory assembly line where the final product has a quality problem. You need to trace responsibility — which worker (parameter) at which step caused the problem? Backpropagation (Backpropagation, an algorithm that efficiently computes each parameter’s contribution to the error in a neural network) starts from the final error and traces backward layer by layer along the network, computing how much “responsibility” each parameter should bear. Essentially, it is a systematic application of the chain rule (Chain Rule, the differentiation rule for composite functions).
Training a neural network typically involves minimizing an error function , where represents all the weight and bias parameters of the network. The most commonly used method is gradient-based optimization algorithms (such as gradient descent), so efficiently and accurately computing the gradient is crucial.
Forward Propagation and Backpropagation
The core idea of the backpropagation algorithm is to use a local message passing (Local Message Passing, where each node only needs to exchange information with adjacent nodes) scheme, passing the error information from the output end of the network backward to the input end.
Forward propagation (Forward Propagation, the process where data flows layer by layer from the input layer to the output layer):
- Input data passes through the network, computing the activation value (the weighted sum of inputs received by a neuron) and the output value (the final output after processing by the activation function) for each unit layer by layer.
- For the -th unit, its activation is the weighted sum of all its inputs : .
- Then apply the activation function (Activation Function, a function that introduces nonlinear transformation, such as sigmoid, ReLU, etc.) to obtain the output: .
- Finally obtain the network output , and compute the error function (usually for a single data point , denoted as ).
Backpropagation (Backward Propagation, the process where error information is passed backward layer by layer from the output layer):
- Starting from the output layer, compute the “sensitivity” or “error term” of each unit to the error (i.e., the partial derivative of the error with respect to that unit’s activation, measuring how much “responsibility” that unit bears for the final error).
- For an output unit , is defined as the partial derivative (Partial Derivative, the derivative when only one variable changes and others are fixed) of the error function with respect to that unit’s activation : For example, for the mean squared error , we have .
- For a hidden unit , its error term is computed from the error terms of subsequent layers via the chain rule: This formula shows that the error of a hidden unit is the sum of the products of all connection weights at its output end and the downstream error terms, multiplied by the derivative of its activation function.
- : the “contribution” of unit to the final error equals the weighted sum of its influence on all lower-layer units
- : the influence weight of unit on unit
- : the sensitivity of unit to the error
- product : the error transmitted through the connection
- : the modulating effect of the activation function on error propagation
- if the activation function changes gently at this point (small derivative), error propagation weakens; if the activation function changes sharply at this point (large derivative), error propagation strengthens
Gradient computation:
- Once all the of all units are computed, the partial derivative of the error function with respect to any weight can be directly computed: This result is very concise: the gradient equals the error term at the output end of the target weight multiplied by the activation value at its input end.
The backpropagation algorithm can be summarized in the following steps (for a single data point ):
- Forward propagation: compute the activation and output of all units.
- Compute output errors: for each output unit , compute .
- Backpropagate errors: from back to front layer by layer, for each hidden unit , compute .
- Compute gradients: for each weight , compute .
For batch or mini-batch training, the gradient of the total error is the sum of the gradients of all data points in the batch:
A Simple Example
To illustrate the backpropagation algorithm concretely, consider a standard two-layer feedforward network (one hidden layer, one output layer), using the sum-of-squares error function.
Network structure and notation:
- Input layer: input units (), where is the bias term.
- Hidden layer: hidden units ().
- Output layer: output units ().
Use superscripts and to distinguish the weights of the two layers:
- : the weight from input unit to hidden unit . (superscript 1 = layer 1, subscript ji = from i to j)
- : the weight from hidden unit to output unit . (superscript 2 = layer 2, subscript kj = from j to k)
Tip for reading notation: the subscript is always “where it comes from → where it goes”, and the superscript is “which layer”. For example, means “in layer 2, the weight from j to k”.
Forward Propagation Computation
For a training sample :
- Compute the weighted input of the hidden layer:
- Compute the activation output of the hidden layer: Using the hyperbolic tangent (tanh) as the activation function: Its derivative has a simple form: .
- Compute the weighted input of the output layer: (Note: is the bias of the hidden layer)
- Compute the activation output of the output layer: Using the linear activation function (i.e., the identity function): Its derivative is 1.
- Compute the error: Using the sum-of-squares error:
Backpropagation Computation (Computing Gradients)
- Compute the error term of the output layer: Since the output layer activation function is linear, .
- Compute the error term of the hidden layer: According to the backpropagation formula: Substituting :
- Compute weight gradients:
Gradient of the second-layer weight :
Gradient of the first-layer weight :
Complexity:
- The computation of one forward propagation is roughly , where is the total number of weights in the network.
- The computation of one backpropagation is also roughly .
- Therefore, the total cost of computing all gradients is .
Thus, backpropagation provides a computationally feasible method for training large neural networks.
Jacobian Matrix
Click to expand: Jacobian matrix and numerical differentiation verification (advanced content)
The Jacobian matrix (Jacobian Matrix, a matrix describing how the network output changes with respect to the input) is applicable to scenarios that require analyzing how input changes affect the output. Consider a neural network with inputs and outputs .
The Jacobian matrix is a matrix whose element at the -th row and -th column is:
That is, the partial derivative of the -th output with respect to the -th input.
Physical meaning: The Jacobian matrix tells us how the network output changes when the input changes by a small amount. It measures the “sensitivity” or “stability” of the network.
Computing the Jacobian matrix can also leverage an efficient algorithm similar to backpropagation.
- Forward propagation:
- Apply the input vector , perform standard forward propagation, and compute the activation values and of all hidden and output layers.
- For each row of the Jacobian matrix (corresponding to an output ):
- Initialization: set the “error term” of the -th unit in the output layer to 1, and the other output units to 0. This corresponds to .
- If the output layer is linear, .
- If it is Sigmoid, .
- Backpropagation: use the same recursive formula as standard backpropagation: Propagate this “error” backward from the output layer to all hidden-layer units, until the input layer.
- When backpropagation reaches the input layer, for the -th input , its corresponding “gradient” is the element at the -th row and -th column of the Jacobian matrix: (because the input layer has no activation function, is its “weighted input”).
Computing the full Jacobian matrix requires independent “backpropagation” processes (one for each output ), each with a computation cost of about .
Numerical Differentiation Verification
To verify the correctness of the Jacobian matrix computation, one can use the central difference method (Central Difference Method, a numerical method that approximates derivatives using function values on both sides):
where is the unit vector with the -th component equal to 1.
Computing the entire Jacobian matrix requires forward propagations, for a total computation cost of . When is large, this is more expensive than the cost of backpropagation (especially when , backpropagation is more efficient).
Hessian Matrix
Click to expand: Hessian matrix and Hessian-vector product (advanced content)
The Hessian matrix (Hessian Matrix, a square matrix of second-order partial derivatives of the error function with respect to all weights) describes the error function’s second-order partial derivatives with respect to network weights. Treat all weight and bias parameters as a large vector , where is the total number of parameters.
The Hessian matrix is a square matrix whose element at the -th row and -th column is:
That is, the second-order partial derivative of the error function with respect to the two weights and .
Physical meaning: The Hessian matrix describes the local curvature of the error function surface. It tells us how the gradient changes in the weight space. A positive definite Hessian means the point is a local minimum.
Directly computing and storing a Hessian matrix is extremely costly.
- Storage space: requires memory. For a network with millions of parameters (), storing a matrix on the order of is impractical.
- Computation time: the naive method requires operations.
However, by extending the backpropagation algorithm, one can design an algorithm with computational efficiency , which is much more efficient than numerical differentiation (which requires ).
Hessian-Vector Product
In practical applications, we usually do not need to explicitly construct the entire Hessian matrix , but rather need to compute its product with some vector , . (In many optimization algorithms (such as variants of Newton’s method), what we need is the product of the Hessian matrix with a vector, not the Hessian matrix itself.)
This can be computed efficiently via two backpropagations (also called “forward-over-reverse” mode), with a computation cost of only , comparable to a single gradient computation.
- Forward mode: given a direction vector , compute the forward-mode derivative .
- Reverse mode: use standard backpropagation to compute the gradient , then apply backpropagation again to compute .
Automatic Differentiation
Backpropagation is a special case of automatic differentiation (Automatic Differentiation, a technique for precisely computing the gradient of any differentiable function in a computer program, more powerful and efficient than symbolic and numerical differentiation). It is more powerful and efficient than symbolic differentiation (deriving formulas) and numerical differentiation (approximate computation).
Core Idea
The key idea of automatic differentiation is to decompose the computation process of a function into a series of basic, differentiable elementary operations (such as addition, subtraction, multiplication, division, exponentiation, logarithm, trigonometric functions, etc.). By tracking the execution of these basic operations (usually represented as a computation graph (Computation Graph, a directed graph using nodes and edges to represent computation steps and their dependencies) or computation trace), and applying the chain rule, it automatically builds the code for computing gradients.
Automatic differentiation has two main modes: (the book has two examples on P213 for better understanding)
Forward mode:
- While computing the function value, compute its derivative with respect to some input variable.
- It introduces an extra “tangent” variable for each intermediate variable (called the “primal variable”), representing that variable’s derivative with respect to some input.
- During forward propagation, compute the tuple simultaneously.
- For a function with inputs, computing the full gradient requires forward-mode computations.
Reverse mode:
- The mode used by backpropagation.
- First perform one forward propagation, compute and store the values of all intermediate variables.
- Then perform one backpropagation, introducing an “adjoint” variable for each intermediate variable , representing the partial derivative of the final output with respect to .
- Starting from the output, using the stored intermediate values, compute each backward according to the chain rule.
- For a function with outputs and inputs, computing the Jacobian matrix from all outputs to all inputs, the reverse mode is usually more efficient than the forward mode, especially when (which is exactly the case for neural networks, where is the scalar error and is the massive number of parameters).
Relationship with Backpropagation
Backpropagation is the concrete implementation of reverse-mode automatic differentiation applied to the neural network error function. It efficiently computes the gradient of the scalar error with respect to all network parameters .
The core of modern deep learning frameworks (such as PyTorch, TensorFlow) is a reverse-mode automatic differentiation engine. Users only need to write the forward propagation code (defining the network and loss function), and the framework automatically computes the gradient, without manually deriving complex partial derivative formulas.
Exercise 2
A minimal network: input , hidden layer , output , loss . Use the chain rule to compute .
The chain rule just multiplies things “link by link”:
Note at the ReLU: when the derivative is 1, otherwise it is 0. So if a neuron is “not activated” (input is negative), the gradient is simply cut off — receives no update signal at all.
If the hidden layer is expanded to 100 neurons, roughly how many times the computation of forward propagation is backpropagation?
Roughly 1 times. Forward propagation computes about 200 multiplications (one weight multiplication per neuron + 100 for the output layer), and backpropagation is also about 200. So the extra overhead of backpropagation is tiny — which is why it can be so widely used: computing the gradient is almost “free”.
Chapter 8 Summary
One-sentence version: Backpropagation = systematic application of the chain rule. Starting from the output error, trace “responsibility” backward layer by layer, compute the gradient of each parameter; the computation cost is about the same as one forward propagation.
Knowledge map:
Backpropagation├── Forward propagation: data → compute activations layer by layer → output → compute error├── Backpropagation: error → compute error term δ layer by layer → obtain gradient│ ├── Output layer: δ = (prediction - truth) × activation function derivative│ └── Hidden layer: δ = activation function derivative × weighted sum of downstream δ├── Gradient formula: ∂E/∂wⱼᵢ = δⱼ × zᵢ (concise!)├── Complexity: O(W), same as forward propagation└── Extended applications ├── Jacobian matrix: sensitivity of output to input ├── Hessian matrix: curvature of the error surface └── Automatic differentiation: framework computes the gradient for you automaticallyChapter 9 Regularization
Intuition first: Remember the example of fitting data with a high-degree polynomial from Part 1? (see Chapter 1 - Regularization Term) When the polynomial degree is too high, the curve wildly wiggles to pass through every training point — this is overfitting (Overfitting, the phenomenon where a model performs very well on training data but poorly on new data). Regularization (Regularization, a family of techniques that prevent overfitting by constraining model complexity) is like putting a “tightening curse” on the model — preventing it from wiggling around too freely, so it learns smoother, more generalizable patterns.
How to improve the model’s generalization ability on limited training data by introducing inductive bias (Inductive Bias, a learning algorithm’s preference for a certain hypothesis)
Earlier (in Chapter 1 - Regularization Term) we saw regularization used to solve the overfitting problem in polynomial curve fitting. Its core idea is: add a penalty term to the loss function to limit the magnitude of the model parameters.
The form of the regularized error function is:
where:
- is the original error function (e.g., mean squared error)
- is the model parameter vector
- is the regularization term (the part that penalizes model complexity, e.g., )
- is the regularization hyperparameter (Regularization Hyperparameter, the “knob” controlling the regularization strength — the larger it is, the heavier the penalty and the simpler the model)
Regularization essentially introduces a certain bias in the bias-variance tradeoff (Bias-Variance Tradeoff, where higher model complexity means lower bias but higher variance, and vice versa) to significantly reduce the model’s variance, thereby improving generalization performance.
Inductive Bias
Most machine learning tasks are inverse problems (Inverse Problem, a problem of inferring causes from limited results): we only have limited sample data, yet we must infer the entire data distribution. Since there are infinitely many possible distributions that could generate this data, a preference must be introduced to select a specific solution. This preference is called inductive bias or prior knowledge (Prior Knowledge, the assumption held before seeing the data).
For example:
- Assume small changes in the input lead to small changes in the output → encourage the model to learn smooth functions
- In image recognition, the position of an object does not affect its category → introduce translation invariance
Without inductive bias, learning from data is impossible. Learning is essentially using prior knowledge to narrow down the hypothesis space.
“No Free Lunch” Theorem
On all possible problems, the average performance of all learning algorithms is the same. If some algorithm performs better on certain problems, it must perform worse on others.
- Deep neural networks are powerful because they have built-in inductive biases suited to real-world problems (such as smoothness, locality)
- The “general-purpose learning algorithm” we pursue is actually about finding inductive biases applicable to broad practical scenarios
- For specific tasks, adding stronger domain-specific biases yields better results
Symmetry and Invariance
Many tasks require the model output to remain invariant under certain transformations of the input. For example:
- Translation invariance: changing the object’s position in an image does not change the classification result
- Scale invariance: changing the object’s size does not change the classification result
Learning these invariances from data alone is very difficult, because a tiny translation may cause drastic changes in pixel values, and the combinations of transformations grow exponentially.
Four methods to achieve invariance:
- Preprocessing: extract features invariant to the transformation (e.g., SIFT)
- Regularized error function: penalize the change of the output under input transformations (e.g., tangent propagation)
- Data augmentation: add transformed samples during training (e.g., flipping, rotating images)
- Network structure design: embed the invariance into the network structure (e.g., convolutional neural networks)

Data augmentation example
The introduction of the above inductive biases will be reflected concretely in later chapters: the regularization term reflects a preference for parameters, the network structure design reflects a preference for function form, and data augmentation reflects a preference for transformation invariance.
Weight Decay
Generalized Weight Decay
The general form of simple quadratic regularization:

Regularization function contour lines for different q values
Generalized regularization using is called L1 regularization (also called Lasso, which uses the sum of absolute values of weights as the penalty term):
Its characteristic is: produces sparse solutions (Sparse Solution, i.e., many weights are pushed exactly to zero), achieving automatic feature selection (the weights corresponding to unimportant features directly become zero, as if “eliminated”).
Basic Form
The most common regularization term is the sum of squared weights (L2 regularization, also called Ridge or weight decay, which uses the sum of squares of weights as the penalty term):
The corresponding regularized error function is:
In gradient descent, its gradient is:
This means that after each update, the weights “decay” a bit (multiplied by ), hence the name “weight decay”.
Regularization “suppresses” parameters that have little influence on the error, driving them toward zero. The number of parameters that actually take effect is called the effective number of parameters. As increases, the effective number of parameters decreases.
Consistent Regularizer
Standard weight decay treats all weights equally, but when a linear transformation (such as normalization) is applied to the input or output, the optimal weights transform accordingly. Standard regularization breaks this consistency, leading to models with different performance under different data preprocessing methods.
Layer-wise Regularization
Use different regularization coefficients for the weights of different layers, and exclude the bias term b:
where represent the weights of the first and second layers respectively.

Layer-wise regularization
represent the first-layer bias, weights, and the second-layer bias, weights respectively.
Learning Curves
The learning curve (Learning Curve, a chart showing how the training and validation set errors change with the number of iterations during training) is an important tool for diagnosing the training state:
- Observe the training progress of the model;
- Determine whether overfitting has occurred;
- Control the effective complexity of the model.
In a typical training process, the training error usually decreases monotonically as the number of iterations increases. However, the validation set’s (Validation Set, a dataset not involved in training, specifically used to check the model’s generalization ability) error may first decrease and then rise, indicating that the model is starting to overfit the training data.
Early Stopping
Early stopping (Early Stopping, a technique that stops training when the validation set error no longer decreases) is an important technique for controlling the effective complexity of the model, and is widely used especially in deep learning. Its basic idea is:
Stop training when the validation set error reaches its minimum, rather than waiting until the training error fully converges.
- At the very beginning of training, the model complexity is low (e.g., weights are close to initial values, such as zero).
- As training proceeds, the model gradually fits the training data, the weights are continuously updated, and the model’s “effective complexity” gradually increases.
- If training continues, the model will overfit the training data, causing the validation error to rise.
Therefore, early stopping is equivalent to limiting the model’s effective number of parameters or effective complexity, thereby preventing overfitting.
For a quadratic error function, early stopping has an effect similar to L2 regularization (i.e., weight decay):
- Assume the weights start from the origin and update along the negative gradient direction.
- Due to the large differences in the eigenvalues of the Hessian matrix, the weight path will first descend rapidly along the low-curvature direction, then slowly approach the global minimum.
- If we stop at some intermediate point, the resulting weight vector ŵ is similar to that obtained using L2 regularization.

Early stopping and L2
represents the maximum likelihood solution corresponding to the minimum of the unregularized error function. If the weight vector starts from the origin and moves according to the local negative gradient direction, then the weight vector will advance along the path shown in the figure. By stopping training early, we can find a weight vector closer to the origin. This is functionally similar to L2 adding the term to the loss function, which biases the optimal solution toward the origin.
It can be proved mathematically that the product of the number of iterations and the learning rate, τη, in early stopping plays a role similar to the reciprocal of the regularization coefficient λ.
Double Descent
Double descent (Double Descent, a modern phenomenon: the test error first decreases, then rises, then decreases again in a non-monotonic behavior) breaks the traditional understanding of the bias-variance tradeoff:
- Training error decreases monotonically as complexity increases;
- Test error first decreases, then rises (the classic overfitting region), then decreases again
When the model complexity is large enough to completely fit the training data (i.e., training error is zero), the test error instead continues to decrease, and the model performs better.

ResNet18 double descent
- Interpolation threshold: the minimum complexity required for the model to perfectly fit the training data.
- After the model complexity exceeds this threshold, it enters the “interpolation region”, where although the training error is zero, the test error may continue to decrease.
In the “critical complexity” region (i.e., near the peak of the test error), increasing the amount of training data may instead cause the test error to rise. Because more data pushes the “interpolation threshold” to the right, temporarily placing the model in a region more prone to overfitting.

Transformer: increasing training data instead causes test error to rise
Significance:
- Do not stop training too early: traditional early stopping may miss the later performance improvement
- Long training may be beneficial: it makes sense that modern large models train for weeks or even months
- Monitor the long-term changes in validation error: observe whether the double descent phenomenon appears
Parameter Sharing
Parameter sharing (Parameter Sharing, a technique that forces multiple locations in the network to use the same set of weights) is a hard constraint that forces a group of weights in the network to be set to the same value. These shared parameters are learned as a whole from the data.
- Advantage: significantly reduces the degrees of freedom of the model (i.e., the effective number of parameters), thereby lowering the risk of overfitting.
- Disadvantage: can only be used for specific problems, and requires knowing in advance which parameters should be shared.
Typical application: the convolution kernels in convolutional neural networks (CNNs). The same convolution kernel slides over the entire input image, its weights shared across all positions, embodying the prior knowledge of “translation invariance”.
Soft Parameter Sharing
Unlike hard sharing, soft sharing uses a regularization term to encourage a group of weights to tend toward similar values, rather than forcing them to be equal. This method is more flexible, and the “grouping” of shared weights can also be learned automatically during training.
Implementation:
- Use a Gaussian mixture model as the prior distribution of the weights: where is the number of Gaussian components, and are learnable parameters.
- The corresponding regularization term is the negative log prior:
- The total error function is:
Training mechanism:
- Introduce the posterior probability : representing the probability that weight belongs to the -th Gaussian component (computed via Bayes’ theorem).
- During gradient updates, each weight is pulled toward the mean of its affiliated Gaussian, with the pull strength determined by .
- At the same time, are also updated according to the posterior probabilities of all weights.
Optimization method:
- Use the EM algorithm for alternating optimization:
- E-step: compute the posterior probability
- M-step: update the parameters
Benefit of soft sharing: no need to pre-specify the grouping; allows weights to form multiple “clusters”; more flexible and adaptive.
Residual Connections
Similar to parameter sharing, the residual connection (Residual Connection, also called skip connection, a structure that lets information skip intermediate layers and pass directly to deeper layers) can also be viewed as a form of structured parameter sharing, achieving cross-layer parameter “sharing” through identity mapping. For a deeper discussion of residual networks, refer to Part 6.
As the number of network layers increases, training becomes increasingly difficult, and even with batch normalization and proper initialization, the following may still occur:
- Gradient vanishing/exploding: in deep networks, gradients are hard to propagate effectively.
- Gradient shattering: the gradient of a deep network is extremely sensitive to the input, becoming “noisy” and non-smooth, destroying the stability of gradient descent.
Theoretically, a deeper network should at least be no worse than a shallow one, but in practice a deeper network’s performance actually degrades.
Example: learning math
- Traditional way: it is too hard to learn directly from first grade all the way to PhD math; intermediate steps easily lose information, and you may forget the fundamentals after learning the later material.
- Residual way: retain the fundamentals at every stage; PhD math = fundamentals + newly learned content, and the fundamentals are always present (via the shortcut connection). Therefore the network only needs to learn the “incremental” part, so that even if the newly learned content is zero, the fundamentals are not lost.
Residual Block
Standard layer: ,
Input x → layer 1 → layer 2 → … → layer L → output z
Residual layer: ; where is a nonlinear transformation (e.g., several convolutions + BN + ReLU), the part the network learns. (The network learns the mapping from x to (z-x), i.e., learns the “residual”)
Key idea: the network no longer directly learns the output , but learns the “residual” .
Advantages
- Identity mapping is easy to achieve:
- If the optimal solution is close to the identity transformation (), just train the weights of close to zero.
- Whereas in a plain network, achieving identity mapping requires precisely tuning a large number of parameters.
- Gradient propagation is more stable:
- The residual connection provides a “shortcut” that allows the gradient to be passed back directly, alleviating the gradient vanishing problem.
- The error surface is smoother:
- Experimental visualizations show that residual connections make the loss function surface smoother and the optimization path more stable.
- Implicit ensemble effect:
- Expanding the residual network reveals it is equivalent to a parallel combination of multiple paths (of different depths).
- This gives the network both the stability of a shallow network and the expressive power of a deep network.
Implementation
- Dimension matching: when the input and the output have different dimensions, adjust the shortcut path via a learnable linear transformation :
- Connection position: usually add the residual connection before the ReLU activation (i.e., add first, then activate)

Two implementations of residual networks, combined as a residual block
Model Averaging
In machine learning, when we have multiple models to solve the same problem, rather than picking a single “best” model, averaging the prediction results of these models usually yields better generalization performance. This method of combining models is also called the committee method or ensemble method (Ensemble Method, a technique that combines the prediction results of multiple models to obtain better performance).
For models whose output is a probability distribution, the ensemble model’s prediction is the average of the individual models’ predictions:
- is the output probability of the -th model;
- is the total number of models.
This averaging operation helps reduce the variance in predictions, thereby improving overall performance.
From the perspective of the bias-variance decomposition, the prediction error of a single model can be decomposed into bias and variance:
When we average the predictions of multiple models, if these models’ errors are uncorrelated, then their variance components cancel each other out, reducing the overall error.
Imagine you are practicing dart throwing:
- Single model: you throw 10 times, all deviating from the bullseye but in similar ways → high bias, low variance
- Ensemble model: 10 people each throw 10 times; each person has their own bias (similar bias) but each person’s random errors differ (uncorrelated variance). Take each person’s average landing point, then average those 10 average points, and the result is closer to the center of the bullseye.
In practice we only have one dataset, so we need to artificially introduce differences between models. Common methods include:
- Bootstrap aggregating:
- Randomly sample with replacement from the original dataset to generate multiple new datasets of size (called bootstrap datasets).
- Each bootstrap dataset is used to train one model.
- The final prediction is the average of all models’ predictions.
- Use models of different structures (e.g., different numbers of layers, different activation functions, etc.), then average.
Consider a regression problem with M trained models , where the ensemble prediction is:
Let the true function be h(x), and each model’s output can be expressed as:
where is the -th model’s error.
Average error of individual models:
Expected error of the ensemble model:
Assume the error mean is zero and the errors are uncorrelated
If the following hold:
- (when )
then we get:
It can be proved that the ensemble error will not exceed the average error of the individual models, i.e.: , so the ensemble is at least not worse.
Unlike bootstrap aggregating, boosting is a sequential ensemble method:
- Base classifiers are trained one by one;
- Each new classifier, when trained, pays more attention to the samples that previous classifiers got wrong (by increasing the weights of these samples);
- The final prediction combines the results of all base classifiers via weighted voting.
The advantage of boosting is that even if each base classifier is only slightly better than random guessing, the combination may still achieve very good performance.
Dropout
Intuition: Imagine a team where every day some people are randomly told to “take a day off” (not come to work). This way nobody can slack off expecting others to do their job, and everyone has to learn to complete tasks independently. Dropout (a technique that randomly “turns off” a portion of neurons during training) is exactly like this — each training session randomly lets some neurons “take a day off”, forcing every neuron to learn useful features, rather than relying on a few “star” neurons.
Dropout is a very effective regularization technique, which can be seen as implicitly performing approximate model averaging over an exponential number of sub-networks during training.
- In each training iteration, randomly “drop” (i.e., set to 0) a portion of neurons (including input and hidden layers, but not the output layer);
- Each time data is fed in, a new “mask” is generated, deciding which neurons are kept (typically the hidden layer keep probability ρ=0.5, the input layer ρ=0.8);
- Training only performs forward and backward propagation on the current “pruned” network.

Dropout illustration (random ignore)
Effects:
- Prevent co-adaptation:
- Each neuron cannot rely on the presence of other specific neurons;
- Must make useful contributions in many different contexts;
- Reduces overfitting to the noise in the training data.
- Implicit model averaging:
- A network with M non-output nodes can theoretically generate different sub-networks;
- Dropout implicitly averages the predictions of these sub-networks during training.
Since it is infeasible to exactly average all sub-networks at test time (computationally intractable), there are two approximation methods:
- Monte Carlo Dropout
- Keep dropout;
- Perform multiple forward propagations on the same input (each using a different dropout mask);
- Average the multiple outputs as the final prediction. where is the mask sampled at the t-th time.
- is typically 10-100 samples to obtain a stable result.
- Weight scaling method (commonly used)
- Do not use dropout at test time;
- Multiply the trained weights by the keep probability ρ;
- This ensures the expected input to the neurons at test time is consistent with that during training.
Exercise 3
You trained a 5-layer network, with 99% accuracy on the training set but only 75% on the validation set. What do you do?
Typical overfitting — the model has “memorized” the training set. Try these tricks:
- Dropout (p=0.5): randomly cut half the neurons, forcing the rest not to rely too much on teammates
- L2 weight decay: don’t let the weights get too large
- Early stopping: stop when the validation loss stops decreasing, don’t stubbornly keep going
- Data augmentation: flip, rotate, crop, find ways to expand the training set
At test time, Dropout has two usages: “weight scaling” and “Monte Carlo”. What’s the difference?
Weight scaling: turn off Dropout at test time, multiply the weights by the keep probability. Fast, done in one forward pass, but the output is fixed.
Monte Carlo: keep Dropout on at test time, run the same input many times and average. Slow, but it tells you “how uncertain the model is” — if you want to know how confident the model is about its answer, use this.
Chapter 9 Summary
One-sentence version: Regularization = putting a “tightening curse” on the model, preventing it from overfitting too freely. L2 weight decay keeps weights from getting too large, early stopping keeps it from learning too long, Dropout keeps neurons from over-relying on teammates, and ensembling lets multiple models learn from each other’s strengths.
Knowledge map:
Regularization (prevent overfitting)├── Why needed? → Inductive bias (prior knowledge narrows the hypothesis space)├── Weight decay│ ├── L2 (sum of squares): shrink weights overall, most common│ ├── L1 (absolute value): produces sparse solutions, automatic feature selection│ └── Layer-wise: different strengths for different layers├── Training control│ ├── Early stopping: stop when validation error no longer drops│ └── Double descent: error drops again after crossing the interpolation threshold├── Structural constraints│ ├── Parameter sharing (hard sharing / soft sharing)│ └── Residual connection (shortcut passes gradients)└── Model averaging ├── Ensemble method: average multiple models └── Dropout: randomly turn off neurons during trainingCross-chapter connections: The regularization techniques in this part (L1/L2 weight decay) echo the penalty term for polynomial fitting in Part 1; for more details on residual connections, see Part 6.