AoZai
AoZai

Reading Notice

These are personal study and translation notes for reference only; the original course materials belong to MIT OpenCourseWare and their respective rights holders.

ZoomNotes for Linear Algebra

Gilbert Strang, MIT 18.06 · Ch.10 / 11

Part 10: Learning from Data: Minimize Loss by Gradient Descent

10.1 Learning Function F(x,v0)F(x, v_0): Data v0v_0 and Weights xx

A neural network is a composition of LL layer functions:

vk=ReLU(Akvk1+bk)for k=1,,Lv_k = \text{ReLU}(A_k v_{k-1} + b_k) \quad \text{for } k = 1, \dots, L

where:

  • v0v_0 is the input data
  • AkA_k are weight matrices (learnable parameters, collectively called xx)
  • bkb_k are bias vectors
  • vLv_L is the output (prediction)

ReLU (Rectified Linear Unit): The nonlinear activation function:

ReLU(y)=max(y,0)\text{ReLU}(y) = \max(y, 0)

ReLU is a piecewise linear ramp function, which makes the entire network piecewise linear in the weights and inputs. This is crucial for training: the function F(x,v0)F(x, v_0) is piecewise linear.

The Learning Function: F(x,v0)F(x, v_0) maps weights xx and input v0v_0 to output vLv_L. The goal is to find weights xx that minimize a loss function over the training data.


10.2 Counting Flat Pieces in the Graph of FF

Because ReLU creates folds (the "crease" where y=0y=0), the piecewise linear function FF divides the input space into many linear regions.

Counting Formula: For NN ReLU folds in Rp\mathbb{R}^p:

r(N,p)=(N0)+(N1)++(Np)r(N, p) = \binom{N}{0} + \binom{N}{1} + \cdots + \binom{N}{p}

Recursive Formula:

r(N,p)=r(N1,p)+r(N1,p1)r(N, p) = r(N-1, p) + r(N-1, p-1)

Growth: The number of regions grows roughly as r(N,p)cNpr(N,p) \approx c N^p, showing that even modest networks can represent highly complex piecewise linear functions.

Example: For N=100N = 100 ReLU neurons in R10\mathbb{R}^{10}, the number of linear regions is approximately (10010)1.7×1013\binom{100}{10} \approx 1.7 \times 10^{13}.


10.3 Minimizing the Loss: Stochastic Gradient Descent

Gradient Descent: Iteratively update weights in the direction of steepest descent:

xk+1=xkskF(xk)x_{k+1} = x_k - s_k \nabla F(x_k)

where sks_k is the learning rate (step size) and F(xk)\nabla F(x_k) is the gradient of the loss at xkx_k.

Loss Functions:

  • Square loss: L=(ypredytrue)2L = (y_{\text{pred}} - y_{\text{true}})^2
  • Cross-entropy loss: L=yilog(y^i)L = -\sum y_i \log(\hat{y}_i) (for classification)

Stochastic Gradient Descent (SGD): Instead of computing the gradient over the entire dataset (which is expensive), use one random sample:

xk+1=xkskFik(xk)x_{k+1} = x_k - s_k \nabla F_{i_k}(x_k)

where iki_k is a randomly chosen index. This is much faster per iteration, and the randomness helps escape local minima.

Minibatch SGD: Average the gradient over a small batch of BB random samples:

xk+1=xksk1BibatchFi(xk)x_{k+1} = x_k - s_k \cdot \frac{1}{B} \sum_{i \in \text{batch}} \nabla F_i(x_k)

Typical batch sizes: 32, 64, 128, 256. Batches balance gradient accuracy with computational efficiency.


10.4 Slow Convergence with Zigzag: Add Momentum

Standard gradient descent can zigzag when the loss landscape is elongated (high condition number), leading to slow convergence.

Momentum (Heavy Ball Method): Add a fraction of the previous update:

zk+1=βzk+F(xk)xk+1=xkskzk+1\begin{aligned} z_{k+1} &= \beta z_k + \nabla F(x_k) \\ x_{k+1} &= x_k - s_k z_{k+1} \end{aligned}

The momentum term βzk\beta z_k (typically β0.9\beta \approx 0.9) smooths the trajectory, reducing oscillation and accelerating convergence.

ADAM (Adaptive Moment Estimation): Combines momentum with adaptive learning rates per parameter:

  • Maintains exponentially decaying averages of past gradients and squared gradients
  • Adapts the learning rate for each parameter individually
  • Handles sparse gradients effectively
  • Typically the default optimizer for deep learning

10.5 Convolutional Neural Nets: CNN in 1D and 2D

Convolutional Neural Networks use shared weights applied via sliding windows, making them shift-invariant (translation equivariant).

1D Convolution: A filter (kernel) ww slides over the input, computing dot products at each position:

(fw)[i]=jf[i+j]w[j](f * w)[i] = \sum_{j} f[i+j] \, w[j]

2D Convolution (images): A small kernel (e.g., 3×33 \times 3) slides across the image, computing the same weighted sum at each patch:

(IK)[i,j]=p,qI[i+p,j+q]K[p,q](I * K)[i,j] = \sum_{p,q} I[i+p, j+q] \, K[p,q]

Key properties:

  • Weight sharing: same filter weights used at every position (dramatically reduces parameters)
  • Locality: each output pixel only depends on a local neighborhood
  • Hierarchy: early layers detect edges, later layers detect shapes and objects

Pooling Layers: Reduce spatial dimensions:

  • Max-pooling: take the maximum value in each 2×22 \times 2 (or other size) block
  • Average-pooling: take the average

Additional components:

  • Softmax: softmax(z)i=ezi/jezj\text{softmax}(z)_i = e^{z_i} / \sum_j e^{z_j} converts outputs to probabilities
  • Batch Normalization: rescales and recenters activations at each layer to maintain stable gradients
  • Residual Networks (ResNet): skip connections xf(x)+xx \to f(x) + x allow training very deep networks by mitigating the vanishing gradient problem

10.6 Backpropagation: Chain Rule for FF

Backpropagation is the algorithm for computing gradients in neural networks. It is the chain rule applied efficiently in reverse mode.

Forward Pass: Compute v1,v2,,vLv_1, v_2, \dots, v_L and the loss LL.

Backward Pass (Reverse Mode): Compute gradients layer by layer from output back to input:

Lvk1=Lvkvkvk1\frac{\partial L}{\partial v_{k-1}} = \frac{\partial L}{\partial v_k} \cdot \frac{\partial v_k}{\partial v_{k-1}}

For each layer vk=ReLU(Akvk1+bk)v_k = \text{ReLU}(A_k v_{k-1} + b_k), the gradients with respect to AkA_k and bkb_k are computed via local derivatives:

LAk=Lvkvk1TLbk=Lvk\begin{aligned} \frac{\partial L}{\partial A_k} &= \frac{\partial L}{\partial v_k} \cdot v_{k-1}^T \\ \frac{\partial L}{\partial b_k} &= \frac{\partial L}{\partial v_k} \end{aligned}

Why automatic differentiation is efficient: The forward pass costs one function evaluation. The backward pass computes all partial derivatives in roughly the same time as one forward pass -- a vast improvement over O(n)O(n) forward passes for nn parameters via finite differences.

The key insight: never form the full Jacobian matrix (which would be enormous). Instead, propagate gradient information backward through the computational graph, storing and reusing intermediate results.

Previous Next
© MIT OpenCourseWare  |  18.06 Linear Algebra  |  Gilbert Strang  |  Spring 2010
ocw.mit.edu  ·  CC BY-NC-SA 4.0