BeginnerCoder

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.

Want it another way?
Task

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.

← 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.