vrishank

← Blog

Lecture 2: Linear Regression and Gradient Descent

Stanford CS229 lecture 2 notes: Linear Regression and Gradient Descent.

· 2 min read

  • cs229
  • machine-learning
  • course-notes
On this page

Setting up#

Notation we'll use for the whole course:

  • mm = number of training examples
  • nn = number of features
  • x(i)x^{(i)} = input features of the ii-th example
  • y(i)y^{(i)} = output of the ii-th example
  • (x(i),y(i))(x^{(i)}, y^{(i)}) = one training example

The job of a supervised learning algorithm is to learn a function h:X→Yh: X \rightarrow Y. This hh is called the hypothesis.

For linear regression we choose:

hθ(x)=θ0+θ1x1+⋯+θnxn=∑j=0nθjxj=θTxh_\theta(x) = \theta_0 + \theta_1 x_1 + \dots + \theta_n x_n = \sum_{j=0}^{n} \theta_j x_j = \theta^T x

(We set x0=1x_0 = 1 so that the intercept term fits neatly into the sum.)

Picking the parameters#

We want hθ(x)h_\theta(x) to be close to yy on the training examples. So we define a cost function, the sum of squared errors:

J(θ)=12∑i=1m(hθ(x(i))−y(i))2J(\theta) = \frac{1}{2} \sum_{i=1}^{m} \left(h_\theta(x^{(i)}) - y^{(i)}\right)^2

and choose the θ\theta that minimizes J(θ)J(\theta). The 12\frac{1}{2} is there only to make the derivative cleaner.

Gradient Descent#

Start with some random θ\theta (say all zeros), and keep updating:

θj:=θj−α∂∂θjJ(θ)\theta_j := \theta_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta)

α\alpha is the learning rate.

Intuition: imagine standing on a hill, looking around 360 degrees, and taking a small step in the steepest downhill direction. Keep doing that and you end up at the bottom.

Working out the derivative for one training example:

∂∂θj12(hθ(x)−y)2=(hθ(x)−y)xj\frac{\partial}{\partial \theta_j} \frac{1}{2}(h_\theta(x) - y)^2 = (h_\theta(x) - y) x_j

So for all examples, the update is:

θj:=θj+α∑i=1m(y(i)−hθ(x(i)))xj(i)\theta_j := \theta_j + \alpha \sum_{i=1}^{m} \left(y^{(i)} - h_\theta(x^{(i)})\right) x_j^{(i)}

Why is there only one minimum?#

For linear regression, J(θ)J(\theta) is a convex quadratic function, so it looks like a bowl. There are no local minima, only one global minimum. That's why gradient descent works so well here.

Batch vs Stochastic Gradient Descent#

  • Batch gradient descent: use all mm examples for each single step. Gets very slow when mm is huge (millions).
  • Stochastic gradient descent (SGD): use only one example for each step, and sweep through the data again and again. It doesn't converge perfectly (it keeps wiggling around the minimum), but it gets close really fast.

For big datasets, SGD is almost always what people use. (Mini-batch is the happy middle ground.)

Picking the learning rate#

  • Too big: you overshoot, bounce around, and might even diverge.
  • Too small: it works, but takes forever.

A good habit is to plot J(θ)J(\theta) against the number of iterations. It should go down smoothly. If it doesn't, change α\alpha.

The Normal Equation#

For linear regression, we can skip gradient descent entirely and just solve for θ\theta directly.

Stack the examples as rows in a matrix XX (size m×(n+1)m \times (n+1)) and the labels into a vector yy. Then J(θ)=12(Xθ−y)T(Xθ−y)J(\theta) = \frac{1}{2}(X\theta - y)^T(X\theta - y).

Set the gradient to zero:

∇θJ(θ)=XTXθ−XTy=0\nabla_\theta J(\theta) = X^T X \theta - X^T y = 0
θ=(XTX)−1XTy\theta = (X^T X)^{-1} X^T y

One line, no learning rate, no iterations.

The catch: inverting XTXX^T X costs roughly O(n3)O(n^3), so it's slow when you have a lot of features (tens of thousands). Then gradient descent wins.