Learning, Backpropagation and OptimizationIntroductionA neural network learns by adjusting numerical parameters so that its predictions become more accurate on a defined task. This process combines four ideas: forward propagation, which computes predictions; a loss function, which measures error; backpropagation, which computes how each parameter contributes to that error; and optimization, which updates parameters to reduce the loss. These ideas form the computational core of supervised deep learning. They apply to multilayer perceptrons, convolutional networks, recurrent networks, transformers, and many other architectures. Although modern software performs most derivatives automatically, an understanding of the underlying mathematics is essential for diagnosing unstable training, selecting optimizers, interpreting learning curves, and designing reliable systems. Learning ObjectivesBy the end of this chapter, you should be able to: - derive forward propagation through a multilayer network;
- select suitable loss functions for regression and classification;
- explain gradient descent and mini-batch training;
- derive backpropagation using the chain rule;
- distinguish symbolic differentiation, numerical differentiation, and automatic differentiation;
- compare momentum, RMSProp, Adam, and related optimizers;
- use learning-rate schedules appropriately;
- explain the purpose of weight initialization and identify common training failures;
- apply these ideas to a practical machine-learning problem.
1. The Computational Model of a Neural NetworkA neuron receives inputs, forms a weighted sum, adds a bias, and applies an activation function. For input vector x ∈ ℝⁿ, weight vector w ∈ ℝⁿ, bias b ∈ ℝ, and activation function φ, its output is z = wᵀx + b, a = φ(z) Here z is the pre-activation and a is the activation. The bias allows the neuron to shift its activation independently of the input. A layer containing m neurons is written z = Wx + b, a = φ(z) where W ∈ ℝᵐˣⁿ, b ∈ ℝᵐ, and φ is normally applied element by element. The dimensions must be compatible: an n-dimensional input produces an m-dimensional output. Common activations include: | Activation | Definition | Typical use | Main issue |
|---|
| Sigmoid | σ(z)=1/(1+e-z) | Binary output | Saturation and small gradients | | Tanh | tanh(z)=(ez-e-z)/(ez+e-z) | Historical hidden layers | Saturation | | ReLU | max(0,z) | Hidden layers | Dead units for persistent negative inputs | | Leaky ReLU | max(αz,z), α > 0 | Hidden layers | Requires choosing α | | Softmax | pi=ezi/∑j ezj | Multiclass output | Sensitive to large logits unless stabilized |
The derivative of ReLU is 1 for z > 0 and 0 for z < 0. At z = 0 it is not differentiable; implementations select a convenient subgradient. 2. Forward PropagationConsider a network with L layers. Let a⁰ = x be the input. For layer l, where l = 1, …, L, zˡ = Wˡaˡ⁻¹ + bˡ, aˡ = φˡ(zˡ) The final activation aᴸ is the prediction ŷ. Forward propagation evaluates these equations from the input layer to the output layer. During training, the intermediate values zˡ and aˡ are retained because backpropagation needs them. For a batch of m examples, it is efficient to place examples in columns. If X ∈ ℝⁿˣᵐ, then Zˡ = WˡAˡ⁻¹ + bˡ1ᵀ where 1 is an m-dimensional vector of ones. Broadcasting adds each bias vector to every example. Vectorization improves computational efficiency and produces consistent matrix dimensions. Output-layer choicesFor a real-valued regression target, the output may be linear: ŷ = zᴸ. For binary classification, a sigmoid output gives a probability p of the positive class. For K-class classification, softmax produces non-negative probabilities whose sum is 1. In practice, softmax and cross-entropy are often implemented together using a numerically stable log-sum-exp calculation rather than explicitly forming very large exponentials. 3. Loss FunctionsA loss function ℓ(ŷ, y) measures error for one example, where y is the target and ŷ is the prediction. For a dataset of m examples, the empirical risk is commonly the mean loss: J(θ) = (1/m)∑i=1m ℓ(ŷ⁽ⁱ⁾, y⁽ⁱ⁾) The vector θ represents all trainable weights and biases. Regularization may be added: Jreg(θ) = J(θ) + λR(θ) where λ ≥ 0 controls the strength of the penalty and R is a regularization function. Mean squared errorFor regression, mean squared error is MSE = (1/m)∑i=1m (ŷ⁽ⁱ⁾ - y⁽ⁱ⁾)² It strongly penalizes large errors and is differentiable everywhere. If the target has extreme outliers, mean absolute error or a robust loss may be preferable. Binary cross-entropyFor y ∈ {0, 1} and predicted probability p ∈ (0, 1), binary cross-entropy is ℓ = -[y log(p) + (1-y)log(1-p)] The logarithm is natural unless otherwise stated. The loss is low when the model assigns high probability to the correct class and becomes large when it is confidently wrong. Multiclass cross-entropyFor one-hot target vector y and softmax probabilities p, ℓ = -∑k=1K yk log(pk) Because exactly one yₖ equals 1, this equals the negative logarithm of the probability assigned to the correct class. Cross-entropy is not the same as classification accuracy: accuracy uses a threshold or largest-probability decision, whereas cross-entropy evaluates the quality of the probabilities. RegularizationL2 regularization adds R(θ) = (1/2)∑j θj² so its derivative with respect to θⱼ is θⱼ. L1 regularization uses R(θ)=∑j|θj| and encourages sparsity, although it is not differentiable at zero. Regularization can reduce overfitting but does not replace validation data, good features, or appropriate model selection. 4. Gradient DescentThe gradient ∇θJ is the vector of partial derivatives of the objective with respect to all parameters. It points in the direction of greatest local increase. Therefore, basic gradient descent moves in the opposite direction: θt+1 = θt - η∇_θJ(θt) Here η > 0 is the learning rate and t is the update index. The learning rate has no physical unit in ordinary machine-learning parameterizations, but it controls the scale of parameter change relative to the gradient. There are three common data regimes: | Method | Data used per update | Advantage | Limitation |
|---|
| Batch gradient descent | Entire training set | Stable gradient estimate | Expensive for large data | | Stochastic gradient descent | One example | Frequent, inexpensive updates | Very noisy | | Mini-batch SGD | Small batch | Efficient compromise | Requires batch-size choice |
For a mini-batch B of size |B|, the update uses gt = (1/|B|)∑i∈B ∇_θℓi(θt) and θₜ₊₁ = θₜ − ηgₜ. Randomly shuffled mini-batches provide approximate gradients and can sometimes help the optimizer escape narrow regions or saddle points. However, the objective being optimized is still an empirical approximation to the desired population risk. 5. Backpropagation: Derivation and InterpretationBackpropagation is an efficient application of the chain rule. It does not define a new kind of derivative; it organizes existing derivatives so that a network with many parameters can be differentiated with computational cost comparable to a small multiple of the forward pass. For a single example, define the error signal at layer l as δˡ = ∂J/∂zˡ For the output layer, δᴸ = (∂J/∂aᴸ) ⊙ φ'ᴸ(zᴸ) where ⊙ denotes elementwise multiplication. For an earlier layer, δˡ = (Wˡ⁺¹)ᵀδˡ⁺¹ ⊙ φ'ˡ(zˡ) The parameter gradients are aˡ⁻¹ ∂J/∂Wˡ = δˡ(aˡ⁻¹)ᵀ, ∂J/∂bˡ = δˡ For a batch, gradients are summed or averaged across examples according to the loss convention. These equations assume column-vector activations and the layer definition zˡ = Wˡaˡ⁻¹ + bˡ. Important special case: softmax with cross-entropyWhen softmax is combined with multiclass cross-entropy, the output-layer gradient simplifies to ∂ℓ/∂zᴸ = p - y This simplification follows from differentiating both functions together. It is one reason the pairing is computationally and statistically useful. Worked exampleConsider a single linear neuron with x = 2, target y = 5, weight w = 1, and bias b = 0. The prediction is ŷ = wx + b = 1×2 + 0 = 2. Using the one-example squared loss ℓ = (1/2)(ŷ − y)², ℓ = (1/2)(2 − 5)² = 4.5. Since ∂ℓ/∂ŷ = ŷ − y = −3, the parameter derivatives are ∂ℓ/∂w = (ŷ-y)x = -3×2 = -6 and ∂ℓ/∂b = ŷ − y = −3. With learning rate η = 0.1, w_new = 1 − 0.1(−6) = 1.6, b_new = 0 − 0.1(−3) = 0.3. The new prediction is 1.6×2 + 0.3 = 3.5, which moves toward the target 5. This is one update, not a guarantee of immediate convergence. 6. Automatic DifferentiationThree methods are often confused: - Symbolic differentiation manipulates algebraic expressions to produce a derivative expression.
- Numerical differentiation approximates a derivative using function evaluations, commonly (f(x+h)-f(x-h))/(2h). It is useful for checking gradients but can suffer from cancellation and step-size errors.
- Automatic differentiation decomposes a program into elementary operations and applies the chain rule exactly up to floating-point arithmetic.
Deep-learning libraries construct a computational graph during the forward pass. Reverse-mode automatic differentiation then traverses that graph backward, accumulating gradients. Reverse mode is efficient when a scalar loss depends on many parameters, which is the usual neural-network situation. Automatic differentiation does not eliminate conceptual responsibility. Data leakage, incorrect target encoding, unsuitable losses, exploding gradients, and shape errors remain possible. Gradient checking on a small model can compare analytical or automatic gradients with finite-difference estimates, using a sufficiently small but not excessively small h. 7. OptimizersMomentumMomentum maintains a moving direction: vt = βvt-1 + gt θt+1 = θt - ηvt where 0 ≤ β < 1. Momentum smooths noisy gradients and accelerates progress along consistent directions. Some implementations use a factor (1 − β)gₜ; both conventions are valid if the learning rate is interpreted consistently. RMSPropRMSProp maintains an exponentially weighted average of squared gradients: st = ρst-1 + (1-ρ)gt² θt+1 = θt - η gt/(√st + ε) Operations on gₜ² and the square root are elementwise. ε > 0 prevents division by zero. Parameters with persistently large gradients receive smaller effective steps. AdamAdam combines momentum-like first moments and RMSProp-like second moments: mt = β₁mt-1 + (1-β₁)gt vt = β₂vt-1 + (1-β₂)gt² Because both moments start at zero, bias correction is used: m̂t = mt/(1-β₁ᵗ), v̂t = vt/(1-β₂ᵗ) The update is θt+1 = θt - η m̂t/(√v̂t+ε) Typical starting values are β₁ = 0.9, β₂ = 0.999, and ε close to 10⁻⁸, but they are not universal laws. Adam often converges quickly, while SGD with momentum may sometimes produce better generalization after careful tuning. Optimizer choice should be evaluated on validation performance, not training loss alone. 8. Learning-Rate SchedulesA fixed learning rate may be adequate for a small problem, but schedules can improve training. Common approaches include: - Step decay: reduce η by a factor after specified epochs.
- Exponential decay: ηt=η0γt, where 0 < γ < 1.
- Cosine decay: gradually vary the rate according to a cosine curve.
- Warm-up: begin with a small rate and increase it during early updates.
- One-cycle schedules: increase and then decrease the rate over a planned training cycle.
- Reduce-on-plateau: lower the rate when validation performance stops improving.
A schedule should be linked to a measurable training objective. Excessively rapid decay can freeze learning; an excessive rate can cause divergence or oscillation. Learning-rate range tests, validation curves, and reproducible experiment logs are useful practical tools. 9. Weight Initialization and Signal PropagationPoor initialization can make a network learn extremely slowly or not at all. Initializing all weights to zero is unsuitable for hidden layers because neurons receive identical gradients and remain identical. Biases can often be initialized to zero, although specialized architectures may use different choices. For a layer with n_in inputs, Xavier/Glorot initialization is designed to maintain approximate activation variance for symmetric activations: Var(Wij) ≈ 2/(nin+nout) For ReLU-like activations, He initialization is commonly used: Var(Wij) ≈ 2/nin A normal distribution with this variance or a corresponding uniform distribution may be used. These are variance heuristics, not guarantees. Their assumptions include roughly independent inputs, comparable feature scales, and a particular activation behavior. Batch normalization, residual connections, normalization layers, and careful preprocessing also affect signal propagation. Feature scaling is important. If one input is measured in thousands and another in fractions, the geometry of the optimization problem may become poorly conditioned. Standardization commonly uses x' = (x-μ)/σ where μ and σ are computed from the training set only. The same training-set statistics must be applied to validation and test data. 10. Diagnosing Training Problems| Symptom | Likely causes | Useful responses |
|---|
| Loss is NaN | Excessive learning rate, overflow, invalid data | Lower rate, stabilize logits, inspect inputs | | Loss oscillates | Rate too high or batches too noisy | Lower rate, increase batch size, use momentum | | Loss barely changes | Rate too low, saturated activation, bad initialization | Increase rate, use suitable initialization, inspect gradients | | Training loss low, validation loss high | Overfitting or leakage in evaluation | Regularize, augment data, simplify model | | All hidden units behave identically | Symmetric initialization | Randomize weights | | Gradients become tiny | Saturating activations or excessive depth | ReLU-like units, normalization, residual paths | | Gradients become huge | Unstable scale or long products | Lower rate, gradient clipping, normalization |
Gradient clipping limits the norm when it exceeds a chosen threshold c: g ← g × min(1, c/||g||) This changes the update direction only when necessary and is particularly common in recurrent or very deep models. It is a safeguard, not a substitute for finding the underlying cause. 11. Applied Case: Illustrative Credit-Default ClassifierBackground and problemA financial institution wants to estimate whether an applicant will default within twelve months. The available features include income, debt ratio, account history, and repayment indicators. This is an illustrative case, not a recommendation for real credit decisions. AnalysisThe target is binary, so a sigmoid output and binary cross-entropy are suitable. The data must be divided by time or by carefully designed sampling where appropriate; random splitting can produce leakage if multiple records from the same customer appear in different partitions. Numerical features are standardized using training-set statistics. Sensitive attributes should not be casually used as predictive inputs, and performance must be examined across relevant groups. ApproachA small multilayer perceptron contains an input layer, two ReLU hidden layers, and one sigmoid output. Parameters are initialized with He initialization. Training uses mini-batches, Adam initially, early stopping based on validation loss, and a learning-rate reduction when validation loss plateaus. The model is evaluated using discrimination and calibration measures, not accuracy alone. For example, ROC-AUC assesses ranking, while calibration examines whether predicted probabilities correspond to observed frequencies. Outcome and governanceSuppose validation cross-entropy decreases while the gap between training and validation loss grows. This suggests overfitting, so the team may add regularization, reduce model capacity, or collect more representative data. Even a numerically strong model should not be deployed without documenting data provenance, monitoring drift, investigating disparate error rates, providing appropriate explanations, and defining human review and appeal procedures. A prediction is not automatically a justified decision. Learning pointsThe case demonstrates that optimization is inseparable from problem formulation. Correct gradients cannot repair biased labels, leakage, inappropriate metrics, or an unjustified deployment policy. Technical performance and responsible governance must be assessed together. 12. Best Practices- Begin with a simple baseline and a clearly defined metric.
- Verify tensor shapes and gradients on a tiny synthetic dataset.
- Separate training, validation, and test data before preprocessing.
- Scale features using training data only.
- Select the output activation and loss as a compatible pair.
- Monitor both optimization loss and application-relevant validation metrics.
- Log random seeds, data versions, hyperparameters, and software versions.
- Tune the learning rate before making the network unnecessarily large.
- Inspect gradient norms and activation distributions when training fails.
- Reassess fairness, calibration, privacy, security, and drift before deployment.
Chapter SummaryNeural-network learning consists of a forward computation, a loss evaluation, gradient calculation, and parameter update. Forward propagation applies zˡ=Wˡaˡ⁻¹+bˡ and an activation at each layer. Loss functions translate prediction quality into an objective; mean squared error is common for regression, while cross-entropy is common for classification. Gradient descent updates parameters opposite the loss gradient, and mini-batches provide an efficient stochastic approximation. Backpropagation applies the chain rule from the output toward the input. Automatic differentiation implements this process through a computational graph, while numerical differentiation is mainly a diagnostic tool. Momentum, RMSProp, and Adam modify gradient descent by incorporating historical gradient information. Learning-rate schedules regulate the scale of updates over time. Weight initialization, feature scaling, normalization, and activation choice strongly influence whether signals and gradients remain usable. Finally, successful optimization does not guarantee a valid AI system: data quality, evaluation design, fairness, privacy, monitoring, and human governance remain essential. Key TermsForward propagation: Computing activations from inputs to outputs. Pre-activation: The weighted sum plus bias before an activation function. Activation function: A nonlinear function applied to a neuron’s pre-activation. Logit: An unnormalized output score, especially before softmax or sigmoid. Loss function: A scalar measure of prediction error. Empirical risk: The average loss over a finite dataset. Regularization: A method that penalizes model complexity. Gradient: The vector of partial derivatives of an objective. Gradient descent: Optimization by moving opposite the gradient. Mini-batch: A subset of training examples used for one update. Backpropagation: Efficient reverse application of the chain rule. Error signal: The derivative of loss with respect to a layer’s pre-activation. Automatic differentiation: Computational evaluation of exact chain-rule derivatives up to numerical precision. Momentum: An optimizer mechanism that accumulates a moving direction. RMSProp: An optimizer that scales updates using a moving average of squared gradients. Adam: An adaptive optimizer combining first- and second-moment estimates. Learning-rate schedule: A rule that changes the learning rate during training. Weight initialization: The procedure for assigning starting parameter values. Gradient clipping: Limiting gradient magnitude to improve numerical stability. Calibration: Agreement between predicted probabilities and observed frequencies. Review QuestionsShort Answer Questions- What roles do forward propagation and the loss function play in supervised learning?
- Why is the learning rate important in gradient descent?
- State the backpropagation recurrence for a hidden layer.
- How does automatic differentiation differ from numerical differentiation?
- Why is initializing every hidden-layer weight to zero problematic?
Long Answer Questions- Derive the gradient for a single linear neuron using the half-squared-error loss.
- Explain binary cross-entropy and why it is appropriate for sigmoid outputs.
- Compare SGD with momentum, RMSProp, and Adam, including their update information.
- Discuss how initialization, feature scaling, and activation functions affect gradient flow.
- Explain how learning-rate schedules and validation monitoring can improve training reliability.
Analytical / Application Questions- A network’s training loss decreases but its validation loss increases after several epochs. Diagnose the likely problem and propose two technically justified responses.
- Design an optimization plan for a multiclass image classifier, specifying output activation, loss, initialization, optimizer, schedule, and evaluation checks.
- A credit-risk model has strong overall AUC but poor calibration and substantially different error rates across groups. Explain why deployment should be delayed and identify governance and technical analyses required before use.
Multiple Choice Questions with AnswersQuestion 1Which expression represents a standard gradient-descent update? A. θ_{t+1} = θ_t + η∇_θJ B. θ_{t+1} = θ_t - η∇_θJ C. θ_{t+1} = ηθ_t + ∇_θJ D. θ_{t+1} = θ_t ÷ η Correct Answer: B. θ_{t+1} = θ_t - η∇_θJ Question 2For multiclass classification with one-hot targets, which output and loss pairing is standard? A. Linear output with mean absolute error B. ReLU output with squared error C. Softmax output with cross-entropy D. Tanh output with hinge loss only Correct Answer: C. Softmax output with cross-entropy Question 3What does backpropagation primarily compute? A. The number of training examples B. The model’s accuracy without predictions C. Parameter gradients using the chain rule D. A new activation function Correct Answer: C. Parameter gradients using the chain rule Question 4For a softmax output combined with cross-entropy, the gradient with respect to logits is: A. p + y B. p − y C. p × y D. y ÷ p Correct Answer: B. p − y Question 5What is the main purpose of bias-correction terms in Adam? A. To remove all regularization B. To correct the initialization of moment estimates at zero C. To convert classification into regression D. To guarantee zero training loss Correct Answer: B. To correct the initialization of moment estimates at zero Question 6Which initialization is commonly associated with ReLU-like hidden units? A. He initialization B. All weights equal to zero C. A distribution with variance 1/n_out only D. One fixed positive constant Correct Answer: A. He initialization Question 7If the learning rate is excessively large, which behavior is most plausible? A. The loss may oscillate or diverge B. All gradients become exactly zero by definition C. The test set becomes larger D. The model automatically becomes calibrated Correct Answer: A. The loss may oscillate or diverge Question 8Which method uses a moving average of squared gradients to scale updates? A. RMSProp B. Plain batch gradient descent C. One-hot encoding D. Early stopping Correct Answer: A. RMSProp Question 9For binary cross-entropy, if y = 1, which term remains in the loss? A. −log(p) B. −log(1−p) only C. p² D. 1−p² Correct Answer: A. −log(p) Question 10Why should validation and test features use training-set scaling statistics? A. To prevent information from evaluation data influencing preprocessing B. To increase the number of labels C. To guarantee perfect accuracy D. To make every feature binary Correct Answer: A. To prevent information from evaluation data influencing preprocessing
|