Lesson 23 — Gradient descent with early stopping
Train until good enough: early stopping
A training loop rarely runs a fixed number of steps. It watches the loss and stops early once the loss is small enough (or stops improving). This saves time and is how real training scripts work. Python's break leaves a loop immediately.
for i in range(500):
...
if error_now < 0.01:
break
Why it matters: training big models costs real money per step. Knowing when to stop is part of the job.
Inside the loop, compute current_loss and break when it is below 0.0001. Expected: it stops at step 12 with w very close to 3.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based AI / ML Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for AI / ML Foundations books — check the publication year so the examples match current versions.
Search Amazon →