Module 1
Mathematical Foundations
Linear algebra, calculus, and gradient descent. Implement a matrix library from scratch in Haskell.
Learning Objectives
- Refresh linear algebra fundamentals
- Understand calculus in the context of optimisation
- Implement gradient descent from scratch
- Grasp vectorisation and computational efficiency
1.1 Linear Algebra Essentials
Key Concepts
- Vectors, matrices, and tensors
- Matrix multiplication and broadcasting
- Eigenvalues, eigenvectors, and diagonalisation
- Singular Value Decomposition (SVD)
Neural networks are matrix operations. All forward passes and backpropagation reduce to linear transformations. Understanding how to compute these efficiently is critical.
Implementation Task 1.1 — Matrix Library
Build a matrix library in Haskell with: basic operations (add, multiply, transpose), broadcasting rules, and element-wise operations.
Haskell — matrix type
module Matrix where
newtype Matrix a = Matrix [[a]] -- Simple representation to start
-- For production, use more efficient arrays
import Data.Array.Unboxed (UArray, array, bounds, (!))
data DenseMatrix = DenseMatrix
{ rows :: Int
, cols :: Int
, data_ :: UArray (Int, Int) Double
}
matmul :: DenseMatrix -> DenseMatrix -> DenseMatrix
matmul m1 m2 = -- Implementation using dot products
1.2 Calculus for Machine Learning
Key Concepts
- Partial derivatives and gradients
- Chain rule (critical for backpropagation)
- Optimisation and critical points
- Convexity and local minima
Backpropagation is mechanically applying the chain rule. Understanding this connection deeply prevents confusion about what's happening during training.
Implementation Task 1.2 — Automatic Differentiation
Haskell — forward-mode AD
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- Simple forward-mode AD
data Dual a = Dual { primal :: a, tangent :: a }
deriving (Show, Eq)
instance Num a => Num (Dual a) where
Dual p1 t1 + Dual p2 t2 = Dual (p1 + p2) (t1 + t2)
Dual p1 t1 * Dual p2 t2 = Dual (p1 * p2) (p1 * t2 + t1 * p2)
-- ... etc
-- Reverse-mode AD (more useful for neural networks) is more complex.
-- Consider implementing a tape-based system.
1.3 Gradient Descent and Optimisation
Key Concepts
- Batch gradient descent, SGD, mini-batch GD
- Learning rates and scheduling
- Momentum and Nesterov acceleration
- Adam optimiser and adaptive learning rates
Most of training time is spent optimising. Understanding optimisers deeply helps you diagnose training failures.
Implementation Task 1.3 — Multiple Optimisers
Haskell — SGD with momentum
data OptimizerState = SGDState
{ learningRate :: Double
, momentum :: Double
, velocity :: [Double] -- Or Matrix for full network
}
-- Update parameters given gradients
updateSGD :: OptimizerState -> [Double] -> [Double] -> (OptimizerState, [Double])
updateSGD state params grads =
let newVel = zipWith (\v g -> momentum state * v - learningRate state * g)
(velocity state) grads
newParams = zipWith (+) params newVel
in (state { velocity = newVel }, newParams)
Adam formulas
m_t = β₁·m_{t-1} + (1-β₁)·∇L(θ)
v_t = β₂·v_{t-1} + (1-β₂)·∇L(θ)²
θ = θ - α · m̂_t / (√v̂_t + ε)
Typical values: β₁=0.9 β₂=0.999 ε=1e-8
Reading & Viewing
Essential reading
Introduction to Linear Algebra (5th ed., 2016)
Chapters 1–3 essential; Chapter 6 on eigenvalues particularly useful.
math.mit.edu/~gs/linearalgebra/
Automatic Differentiation in Machine Learning: A Survey (2018)
arxiv.org/abs/1502.05767
An Overview of Gradient Descent Optimisation Algorithms (2016)
arxiv.org/abs/1609.04747
Adam: A Method for Stochastic Optimisation (2014)
arxiv.org/abs/1412.6980
Video
Backpropagation — distill.pub
Interactive visual explanation of backpropagation.
distill.pub/2020/backprop/
3Blue1Brown — Neural Networks series (episodes 3–4)
YouTube playlist
Blog
Sebastian Ruder's blog — Optimising Gradient Descent
ruder.io/optimizing-gradient-descent/