DL Foundations
The Math Behind Deep Learning - Linear Algebra, Calculus, Probability
Every neural network operation - from matrix multiplications to gradient updates - is built on mathematical principles. Mastering these foundations lets you understand why architectures work and debug training failures at the mathematical level.
- Linear Algebra - Vectors, matrices, eigendecomposition, and SVD underpin all neural network operations
- Calculus - The chain rule is the backbone of backpropagation, enabling efficient gradient computation
- Probability - Gaussian distributions, cross-entropy, and KL divergence shape loss functions and generative models
Math Foundations for Deep Learning
What You'll Learn
- 1Multiply matrices and norms to describe a dense layer as z = Wx + b
- 2Apply the chain rule through a computational graph and derive the backprop gradient of a weight
- 3Decompose matrices with eigendecomposition and SVD and say what each reveals about a layer
- 4Derive cross-entropy from maximum likelihood and stabilize log-sum-exp in code
Deep learning is built on linear algebra, calculus, and probability. This tutorial covers the essential math you need to understand how neural networks learn.
Linear Algebra
Vectors and Matrices
The same product written with shapes is the unit you should reason in. A dense layer maps an input vector to an output vector and adds a bias:
so a batch of samples stacks into one matrix multiply, with . That single line is the entire forward pass of a fully connected layer, and the FLOPs it costs are .
Matrix Operations in Neural Networks
Eigenvalues and Eigenvectors
Singular Value Decomposition (SVD)
Calculus for Deep Learning
Gradients
The Chain Rule
Unwound through a two layer network the chain rule becomes the gradient you actually store on each weight:
Two facts make this cheap. First the same term appears in every layer, so one routine serves all of them. Second, the outer product is a rank one update whose cost matches the forward pass, which is why training a layer costs about twice inference, not more.
The gradient then drives the parameter update that every optimizer in the next lessons generalizes:
Jacobian and Hessian
Probability Theory
Distributions
Information Theory
Practical Applications in Deep Learning
Summary
- Linear Algebra: Matrix multiplication, eigendecomposition, and SVD are the building blocks of all neural network operations
- Calculus: The chain rule enables efficient gradient computation through backpropagation
- Probability: Gaussian distributions, cross-entropy, and KL divergence shape loss functions and generative models
- Numerical Stability: Implementing math operations requires care to avoid overflow, underflow, and division by zero
Next: Backpropagation Algorithm
Common Mistakes
Ignoring matrix shapes until an error appears at runtime
Fix: Annotate every tensor with (batch, features) and check that W is (out, in) before the first forward pass.
Computing softmax or log directly in floating point
Fix: Subtract the max first: log sum exp(x) = m + log sum exp(x - m), otherwise exp(710) overflows FP32.
Treating the Hessian as something you must materialize
Fix: Use its eigenvalues only as a concept; in practice estimate curvature with the Gauss-Newton vector products or adaptive optimizers.
Best Practices
- Write shapes next to every symbol in your head before you write the code
- Derive the gradient of a loss on paper once, then compare it against autograd on a two element tensor
- Use float64 only for validation runs and mixed precision for throughput, never the reverse
Key Takeaways
- A dense layer is z = W x + b costing 2 B m n FLOPs for batch B
- Backprop is the chain rule applied layer by layer: dL/dW(1) = W(2)^T delta(2) outer h(0)
- Eigendecomposition and SVD explain curvature, pruning, and low-rank weight compression
- Cross-entropy with softmax gradients to y_hat minus y, and log-sum-exp keeps it numerically finite
Frequently Asked Questions
Why is the chain rule the whole of backpropagation?
Because a network is a composition of differentiable functions, and the derivative of a composition is the product of the derivatives along the path. Backprop just organizes that product so each intermediate derivative is computed once and reused, which turns an exponential enumeration of paths into one forward and one backward sweep.
What does the Hessian tell me that the gradient does not?
The gradient gives slope, the Hessian gives curvature. Its eigenvalues say how fast the slope itself changes: large positive eigenvalues mean a sharp valley where big steps overshoot, near-zero eigenvalues mean a flat plateau where steps barely move, and negative eigenvalues mark a saddle where the gradient points along a direction that is actually climbing.
Why do we use log-probabilities instead of probabilities?
Products of thousands of small probabilities underflow, while sums of log-probabilities do not. Maximum likelihood maximizes a product, which is the same as maximizing a sum of logs, and the log-sum-exp identity lets you evaluate that sum with one subtraction of the maximum.
Interview Questions
Q1: Write the gradient of a single weight in a two layer network and explain each factor.
dL/dW(1) = W(2)^T delta(2) outer h(0) with delta(2) = grad h(2) L elementwise sigma prime z(2). The transpose routes the error back, the elementwise term is the local nonlinearity, and the outer product pairs the error with the activation that entered the layer.
Q2: How do eigendecomposition and SVD show up in practice deep learning?
Hessian eigenvalues describe loss curvature and saddle points, so optimizers avoid the sharp directions; SVD gives the singular values of a weight matrix, and truncating small ones is exactly low-rank pruning, which is how a 4,096 x 4,096 layer can drop to rank 256 with 16 times fewer weights.
Q3: Derive the cross-entropy gradient with respect to the logits.
L = -z_y + log sum_j exp z_j, so dL/dz_i = softmax(z)_i - y_i. One subtraction, no exponentials in the backward pass, which is why the softmax plus cross-entropy pair is fused into a single kernel in practice.