Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed
Deep Learning Systems · Lecture 2 of 25 · 1:20:00
Lecture 2: ML Refresher and Softmax Regression
Study guide
What this lecture covers
This lecture reframes machine learning as three interchangeable ingredients: a hypothesis class, a loss function, and an optimization procedure. It argues that essentially every supervised learning algorithm, from linear classifiers to deep networks, is just a different choice of these three pieces solving the same core optimization problem.
The lecture then works through softmax regression (multi-class logistic regression) as a concrete instance of that framework, building up matrix batch notation, the cross-entropy loss, gradient descent, and stochastic gradient descent, before manually deriving the gradient of the loss with respect to the parameters. This sets up the next lecture on neural networks, which reuses the same loss and optimization procedure with a fancier hypothesis class, and previews automatic differentiation, which will let you skip manual derivations like the one done here.
Key ideas
- Three ingredients of ML: every algorithm consists of a hypothesis class (the parameterized model), a loss function (what makes a prediction good), and an optimization procedure (how to find good parameters).
- Linear hypothesis class: for k-class classification with n-dimensional inputs,
h_theta(x) = theta^T x, where theta is an n-by-k parameter matrix mapping inputs to a k-dimensional vector of per-class scores. - Matrix batch notation: stacking examples into a design matrix
X(m by n) lets the hypothesis and gradient be computed as efficient matrix operations instead of loops, which matters for performance on CPUs and GPUs. - Cross-entropy (softmax) loss: converts raw hypothesis outputs (logits) into a probability distribution via exponentiation and normalization, then takes the negative log probability of the true class; preferred over the zero-one error loss because it is differentiable.
- Gradient descent: repeatedly moves parameters against the gradient,
theta = theta - alpha * gradient, where alpha is the step size; the lecture stresses that step size choice strongly affects convergence. - Stochastic gradient descent (SGD): computing the gradient over the full dataset is too slow and memory-heavy, so training splits data into minibatches of size B and takes a gradient step per batch, which is what actually drives deep learning training.
- The "cheat" derivation trick: to compute matrix-valued gradients without full matrix calculus, the lecture treats terms as scalars, applies the ordinary chain rule, then transposes and reorders the resulting matrices so the dimensions match, verifying the result numerically afterward.
- Result: applying the derived gradient rule to MNIST with softmax regression alone achieves roughly 8% error, using only a handful of lines of code.
Walkthrough
Why supervised learning replaces hand-coded logic (1:01)
The lecture opens with digit classification as a motivating example: hand-coding rules for what makes an image a "3" is hard, but collecting labeled training data and letting an algorithm learn a mapping is comparatively easy. This motivates the general supervised learning setup and introduces the three-ingredient framework that structures the rest of the lecture.
Setting up multi-class classification and the linear hypothesis (8:12)
The lecture defines notation used throughout the course: n-dimensional inputs x^(i), k-class targets y^(i), and m training examples, illustrated with MNIST (n=784, k=10, m=60000). It then defines a general hypothesis function mapping R^n to R^k and specializes it to the linear hypothesis h_theta(x) = theta^T x, followed by matrix batch notation for applying the hypothesis to an entire dataset at once.
Loss functions: from classification error to cross-entropy (22:30)
The zero-one error loss is introduced first as the intuitive way to measure classifier quality, then rejected because it is non-differentiable and hard to optimize. The lecture builds the softmax operator (exponentiate, then normalize) to convert hypothesis outputs into a probability distribution, and defines the cross-entropy loss as the negative log probability of the true class. It notes that in practice you compute this loss directly from the linear logits rather than explicitly forming the softmax, for numerical stability.
Gradient descent and stochastic gradient descent (38:55)
The lecture defines the gradient as a matrix of partial derivatives that points in the direction of steepest increase, and derives the gradient descent update rule. It discusses how step size (alpha) affects convergence, from slow-but-steady to divergence, and explains why full-batch gradient descent is impractical at scale, motivating minibatch stochastic gradient descent as the actual training algorithm behind deep learning.
Manually deriving the softmax regression gradient (55:16)
The lecture computes the gradient of the cross-entropy loss with respect to its vector input, getting z - e_y (the softmax output minus a one-hot vector at the true class). It then shows the informal but widely used technique of treating matrix terms as scalars, applying the chain rule, and fixing up transposes to match dimensions, arriving at the batch gradient X^T (Z - I_y) for softmax regression's parameters.
Putting it together and looking ahead (1:16:52)
Despite the complex derivation, the resulting training algorithm is only a few lines of code: iterate over minibatches and apply the gradient update. The lecture closes by previewing that the next lecture repeats this exact process with a neural network hypothesis class instead of a linear one, and that automatic differentiation will soon remove the need for manual gradient derivations like the one just shown.
Before you watch
- Watch Lecture 1 first for the course's structure and goals.
- Be comfortable with vector and matrix notation, partial derivatives, and the chain rule; this lecture assumes you've seen basic supervised machine learning before.
- Familiarity with logistic regression or classification loss functions helps, though the lecture defines cross-entropy loss from scratch.
Check your understanding
- What are the three ingredients that the lecture claims define every machine learning algorithm?
- Why is the zero-one error loss a poor choice for optimization, even though it directly measures classification accuracy?
- What problem does minibatch stochastic gradient descent solve compared to computing the gradient over the full training set?
- In the "treat everything as a scalar" gradient derivation trick, how do you determine the final matrix's shape and transpose order?
- What changes between softmax regression and the neural network model covered in the next lecture, and what stays the same?
Chapters
- 0:00 Introduction
- 1:08 Machine learning and data-driven programming
- 5:34 Three ingredients of a machine learning algorithm
- 8:40 Multi-class classification setting
- 12:04 Linear hypothesis function
- 16:52 Matrix batch notation
- 22:34 Loss function #1: classification error
- 26:44 Loss function #2: softmax / cross-entropy loss
- 35:28 The softmax regression optimization problem
- 39:16 Optimization: gradient descent
- 50:35 Stochastic gradient descent
- 55:26 The gradient of the softmax objective
- 1:08:16 The slide I'm embarrassed to include...
- 1:16:49 Putting it all together
From the YouTube description
Lecture 2 of the online course Deep Learning Systems: Algorithms and Implementation.
This lecture covers a refresher of the basic principles of (supervised) machine learning, as exemplified by the softmax regression algorithm. We will go through the derivation of the softmax regression method and stochastic gradient descent applied to train this class of model.
Sign up for the course for free at http://dlsyscourse.org.
Errata:
1:14:24 - X should be m x n, not m x k. The final gradient expression is correct (using the right size), but this was a typo in annotating.
Contents:
00:00 - Introduction
01:08 - Machine learning and data-driven programming
05:34 - Three ingredients of a machine learning algorithm
08:40 - Multi-class classification setting
12:04 - Linear hypothesis function
16:52 - Matrix batch notation
22:34 - Loss function #1: classification error
26:44 - Loss function #2: softmax / cross-entropy loss
35:28 - The softmax regression optimization problem
39:16 - Optimization: gradient descent
50:35 - Stochastic gradient descent
55:26 - The gradient of the softmax objective
1:08:16 - The slide I'm embarrassed to include...
1:16:49 - Putting it all together
← Lecture 1: Introduction and Logistics · Lecture 3 (Part I): Manual Neural Networks →
