1. Home
  2. Explore
  3. Other
  4. Lecture Introduction and Logistics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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:

  1. Linear regression – fitting a line (today's lecture)

  2. Classification – generalizing regression to discrete outputs

  3. 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→Yh: \mathcal{X} \to \mathcal{Y}h:X→Y that performs well on new (unseen) data.

  • Input space X\mathcal{X}X: images, text, house features, etc.

  • Output space Y\mathcal{Y}Y: labels (cat/dog), prices, etc.

  • Training set: {(x(1),y(1)),(x(2),y(2)),…,(x(n),y(n))}\{(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), \dots, (x^{(n)}, y^{(n)})\}{(x(1),y(1)),(x(2),y(2)),…,(x(n),y(n))}, where x(i)∈Xx^{(i)} \in \mathcal{X}x(i)∈X, y(i)∈Yy^{(i)} \in \mathcal{Y}y(i)∈Y.

Notation

  • hhh: hypothesis function (often called hθh_\thetahθ​ when parameterized)

  • Errors vs. predictions: we care about generalization – performance on new data, not just training data.

Types of Problems

  • Classification: Y\mathcal{Y}Y is discrete (e.g., {cat, dog})

    • Binary: yes/no

    • Multi-class: multiple categories

  • Regression: Y\mathcal{Y}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:

hθ(x)=θ0+θ1x1h_\theta(x) = \theta_0 + \theta_1 x_1hθ​(x)=θ0​+θ1​x1​

Where:

  • θ0\theta_0θ0​: intercept (bias term)

  • θ1\theta_1θ1​: slope (weight for feature x1x_1x1​)

To make this notation linear, we set x0=1x_0 = 1x0​=1 for every example, so:

hθ(x)=∑j=0dθjxjh_\theta(x) = \sum_{j=0}^{d} \theta_j x_jhθ​(x)=∑j=0d​θj​xj​

where ddd 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).

![Scatter plot of lot area vs. sale price, showing a roughly linear upward trend](Think of the image showing points with a line through them)

The line represents:

hθ(x)=θ0+θ1⋅lot areah_\theta(x) = \theta_0 + \theta_1 \cdot \text{lot area}hθ​(x)=θ0​+θ1​⋅lot area

High-Dimensional Linear Models

For many features, we use:

hθ(x)=θ0x0+θ1x1+⋯+θdxdh_\theta(x) = \theta_0 x_0 + \theta_1 x_1 + \cdots + \theta_d x_dhθ​(x)=θ0​x0​+θ1​x1​+⋯+θd​xd​

with x0=1x_0 = 1x0​=1. This is linear in the parameters θ\thetaθ, but the features xjx_jxj​ 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)X \in \mathbb{R}^{n \times (d+1)}X∈Rn×(d+1), where row iii is the feature vector for the iii-th example (including x0(i)=1x_0^{(i)} = 1x0(i)​=1). The iii-th output is y(i)y^{(i)}y(i).


🎯 Cost Function: Measuring Goodness

We need a way to measure how good a hypothesis hθh_\thetahθ​ is. The most common for regression is squared error:

J(θ)=12∑i=1n(hθ(x(i))−y(i))2J(\theta) = \frac{1}{2} \sum_{i=1}^{n} \left( h_\theta(x^{(i)}) - y^{(i)} \right)^2J(θ)=21​∑i=1n​(hθ​(x(i))−y(i))2
  • The 12\frac{1}{2}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 θ\thetaθ.

Goal

Find θ\thetaθ that minimizes J(θ)J(\theta)J(θ):

θ∗=arg⁡min⁡θJ(θ)\theta^* = \arg\min_{\theta} J(\theta)θ∗=argminθ​J(θ)

⬇️ Gradient Descent: How to Find the Best θ\thetaθ

Intuition

Imagine J(θ)J(\theta)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 α\alphaα: step size. Too large → overshoot; too small → slow progress.

Algorithm

  1. Initialize θ(0)\theta^{(0)}θ(0) (e.g., all zeros or random).

  2. For each iteration t=0,1,2,…t = 0, 1, 2, \dotst=0,1,2,…: $$ \theta^{(t+1)}_j = \theta^{(t)}_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta) $foreachparameterfor each parameterforeachparameterj$.

  3. Stop when the update becomes small (convergence).

Gradient for Linear Regression

Compute the partial derivative:

∂∂θjJ(θ)=∑i=1n(hθ(x(i))−y(i))⋅xj(i)\frac{\partial}{\partial \theta_j} J(\theta) = \sum_{i=1}^{n} \left( h_\theta(x^{(i)}) - y^{(i)} \right) \cdot x_j^{(i)}∂θj​∂​J(θ)=∑i=1n​(hθ​(x(i))−y(i))⋅xj(i)​

(Notice the 12\frac{1}{2}21​ cancels the 2 from the derivative of the square.)

So the update rule becomes:

θj(t+1)=θj(t)−α∑i=1n(hθ(x(i))−y(i))⋅xj(i)\theta^{(t+1)}_j = \theta^{(t)}_j - \alpha \sum_{i=1}^{n} \left( h_\theta(x^{(i)}) - y^{(i)} \right) \cdot x_j^{(i)}θj(t+1)​=θj(t)​−α∑i=1n​(hθ​(x(i))−y(i))⋅xj(i)​

This is batch gradient descent – it uses the entire dataset to compute one update.

Vectorized Form

In vector form (for all jjj at once):

θ(t+1)=θ(t)−α∑i=1n(hθ(x(i))−y(i))x(i)\theta^{(t+1)} = \theta^{(t)} - \alpha \sum_{i=1}^{n} \left( h_\theta(x^{(i)}) - y^{(i)} \right) x^{(i)}θ(t+1)=θ(t)−α∑i=1n​(hθ​(x(i))−y(i))x(i)

Important Properties

  • For linear regression, J(θ)J(\theta)J(θ) is convex (bowl-shaped). Every local minimum is a global minimum.

  • Gradient descent is guaranteed to converge to the global minimum (with appropriate α\alphaα).

  • The learning rate α\alphaα is usually constant or decays over time. Adaptive methods (like Adam) adjust per-parameter learning rates.

Convergence Criteria

  • Check if the change in θ\thetaθ or J(θ)J(\theta)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 (nnn millions or billions), computing the full gradient for each step is too expensive.

Solution: Mini-Batch Gradient Descent

Instead of using all nnn examples, we use a random mini-batch of BBB examples (B≪nB \ll nB≪n).

  1. Shuffle the data.

  2. Randomly select BBB indices B\mathcal{B}B.

  3. 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(Sometimes the scaling factor is omitted; it's absorbed into(Sometimesthescalingfactorisomitted;it′sabsorbedinto\alpha$.)

Special Cases

  • Stochastic gradient descent (SGD): B=1B = 1B=1 (one example per update).

  • Batch gradient descent: B=nB = nB=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 θ\thetaθ in closed form using calculus:

Set the gradient to zero:

∇θJ(θ)=0⇒XTXθ=XTy\nabla_\theta J(\theta) = 0 \quad \Rightarrow \quad X^T X \theta = X^T y∇θ​J(θ)=0⇒XTXθ=XTy

Solving:

θ∗=(XTX)−1XTy\theta^* = (X^T X)^{-1} X^T yθ∗=(XTX)−1XTy

This is the normal equation. It's useful for small-to-medium datasets but becomes expensive for large ddd (inverting XTXX^T XXTX is O(d3)O(d^3)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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent