Understanding AI · lesson 20 of 60 · about 6 minutes
The Training Loop
Guess, measure, adjust, repeat. Watch a model train itself, step by step.
What you'll learn
- Describe the training loop
- Follow a few rounds of training
- Explain when training stops
Round and round
Training is one loop, repeated many times: guess with the current weights, measure the error, nudge the weights to shrink it.
It stops when the error is small enough, or stops improving, or after a set number of rounds. Big AI models go round this loop millions of times.
A one-weight model learning the price of a 10 square metre room (50). Each round it nudges w by 0.5 towards the answer.
size = 10
price = 50
w = 2
for round_number in range(8):
guess = w * size
if guess < price:
w = w + 0.5
elif guess > price:
w = w - 0.5
print(round_number, w, abs(w * size - price))Output
0 2.5 25.0
1 3.0 20.0
2 3.5 15.0
3 4.0 10.0
4 4.5 5.0
5 5.0 0.0
6 5.0 0.0
7 5.0 0.0Tip
elif means else if: a second check that only happens when the first one wasn't true.
Now try it yourself
This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.