BeginnerCoder

Lesson 20 — Gradient descent

Gradient descent with two weights

Now the model has two weights: pred = w * x + b, where b is the bias (the intercept). Training updates both. The gradient for w is error × x; the gradient for b is just error. Going through the data example by example and updating after each one is called stochastic gradient descent (SGD). One pass over all data is an epoch.

miss = (k * h + c) - p
k -= rate * miss * h
c -= rate * miss

Why it matters: real models have millions of weights, but each one is updated exactly like this.

Want it another way?
Task

Write the two update lines. After 200 epochs the model should find y = 2x + 1.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with AI / ML Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.