Lecture Introduction and Logistics
🧠 Supervised Learning & Linear Regression
📚 Course Logistics & Overview
This block of three lectures builds increasingly sophisticated machine learning models, all closely related to linear regression. The progression is:
Linear regression – fitting a line (today's lecture)
Classification – generalizing regression to discrete outputs
Exponential family of models – a unified abstraction for many statistical models
The key workhorse algorithm is stochastic gradient descent, which underlies modern applications in image detection, search, NLP, and translation. The approach uses a parallel structure across all three topics: model definition → cost function → optimization via gradient descent.
Important: The same optimization techniques used for linear regression carry over to deep learning and large-scale machine learning. The notation is chosen carefully to generalize.
📐 Supervised Learning: Basic Definitions
What is Supervised Learning?
Given a training set of input-output pairs, the goal is to find a prediction function h:X→Y that performs well on new (unseen) data.
Input space X: images, text, house features, etc.
Output space Y: labels (cat/dog), prices, etc.
Training set: {(x(1),y(1)),(x(2),y(2)),…,(x(n),y(n))}, where x(i)∈X, y(i)∈Y.
Notation
h: hypothesis function (often called hθ when parameterized)
Errors vs. predictions: we care about generalization – performance on new data, not just training data.
Types of Problems
Classification: Y is discrete (e.g., {cat, dog})
Binary: yes/no
Multi-class: multiple categories
Regression: Y is continuous (e.g., house prices)
Key Idea
The training set is assumed to be drawn from a larger population. We want to do well on randomly chosen new examples from that same population. This is why we need generalization, not just interpolation.
📈 Linear Regression: The Workhorse Model
Model Representation
We use linear models (technically affine, but we add a constant term). For a single feature:
Where:
θ0: intercept (bias term)
θ1: slope (weight for feature x1)
To make this notation linear, we set x0=1 for every example, so:
where d is the number of original features. This allows vector notation.
Example: House Price Prediction
Given features like lot area, number of bedrooms, etc., we predict sale price. Data from the Ames, Iowa dataset (available on Kaggle).

The line represents:
High-Dimensional Linear Models
For many features, we use:
with x0=1. This is linear in the parameters θ, but the features xj can be arbitrary (e.g., embedding representations). High-dimensional linear models are powerful – they underpin many modern systems.
Training Data as a Matrix
Define the design matrix X∈Rn×(d+1), where row i is the feature vector for the i-th example (including x0(i)=1). The i-th output is y(i).
🎯 Cost Function: Measuring Goodness
We need a way to measure how good a hypothesis hθ is. The most common for regression is squared error:
The 21 simplifies derivatives (cancels the 2 from the power).
This is the least squares cost.
The error is squared so that positive and negative errors don't cancel, and large errors are penalized more.
Cost function (loss function): A function that quantifies how bad the model's predictions are. Minimizing it gives the best θ.
Goal
Find θ that minimizes J(θ):
⬇️ Gradient Descent: How to Find the Best θ
Intuition
Imagine J(θ) as a landscape (for linear regression, it's bowl-shaped, but not always). Starting from an initial guess, we repeatedly take steps downhill.
Gradient: direction of steepest ascent. We go in the opposite direction.
Learning rate α: step size. Too large → overshoot; too small → slow progress.
Algorithm
Initialize θ(0) (e.g., all zeros or random).
For each iteration t=0,1,2,…: $$ \theta^{(t+1)}_j = \theta^{(t)}_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta) $foreachparameterj$.
Stop when the update becomes small (convergence).
Gradient for Linear Regression
Compute the partial derivative:
(Notice the 21 cancels the 2 from the derivative of the square.)
So the update rule becomes:
This is batch gradient descent – it uses the entire dataset to compute one update.
Vectorized Form
In vector form (for all j at once):
Important Properties
For linear regression, J(θ) is convex (bowl-shaped). Every local minimum is a global minimum.
Gradient descent is guaranteed to converge to the global minimum (with appropriate α).
The learning rate α is usually constant or decays over time. Adaptive methods (like Adam) adjust per-parameter learning rates.
Convergence Criteria
Check if the change in θ or J(θ) is below a threshold.
In practice, for convex problems, we can prove convergence. For modern deep learning, we often run until validation loss stops improving.
🧩 Batch vs. Stochastic vs. Mini-Batch Gradient Descent
Problem with Batch
For large datasets (n millions or billions), computing the full gradient for each step is too expensive.
Solution: Mini-Batch Gradient Descent
Instead of using all n examples, we use a random mini-batch of B examples (B≪n).
Shuffle the data.
Randomly select B indices B.
Update: $$ \theta^{(t+1)} = \theta^{(t)} - \alpha \cdot \frac{n}{B} \sum_{i \in \mathcal{B}} \left( h_\theta(x^{(i)}) - y^{(i)} \right) x^{(i)} $(Sometimesthescalingfactorisomitted;it′sabsorbedinto\alpha$.)
Special Cases
Stochastic gradient descent (SGD): B=1 (one example per update).
Batch gradient descent: B=n (full dataset).
Trade-offs
Aspect | Stochastic | Mini-batch | Batch |
|---|---|---|---|
Speed per update | Very fast | Medium | Slow (for large n) |
Gradient quality | Noisy (high variance) | Moderate | Exact |
Convergence behavior | Can bounce around, but often generalizes better | Good balance | Smooth, deterministic |
Mini-batch size is typically chosen to fit hardware (e.g., 32, 64, 128). Modern GPUs process batches in parallel, so a batch of 128 may be nearly as fast as 1.
Hogwild! results show that even with very noisy updates (asynchronous SGD), convergence is robust.
Why Mini-Batch Works
Real datasets often contain redundancy (e.g., many similar images). A mini-batch gives a good enough gradient estimate.
The noise from mini-batches can actually help escape poor local minima in non-convex problems.
🧮 Normal Equations (Briefly)
For linear regression, we can directly solve for θ in closed form using calculus:
Set the gradient to zero:
Solving:
This is the normal equation. It's useful for small-to-medium datasets but becomes expensive for large d (inverting XTX is O(d3)). Gradient descent is preferred for large-scale problems.
🔍 Summary & Key Takeaways
Supervised learning maps inputs to outputs using a hypothesis function.
Linear regression with squared error is a fundamental model.
Gradient descent (batch, mini-batch, stochastic) optimizes the cost.
The error's gradient has the form: error × derivative of hypothesis – this pattern generalizes to other models (classification, exponential families).
Mini-batch SGD is the workhorse of modern machine learning, balancing speed and gradient quality.
In the next lecture, these ideas extend to classification using the same optimization framework.

Ask anything
We've read this whole study set, so questions about it get the best answers.