Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 1: AI and Machine Learning Fundamentals

Week 01 / Lesson 3

How a model learns from error

Calculate prediction error and improve one weight with a small gradient step.

Section 3.1 1 of 7

Overview

Focus

Calculate prediction error and improve one weight with a small gradient step.

Your route through the lesson

  • 3.2 A prediction, a parameter, and a loss
  • 3.3 Slope, learning rate, and a weight update
  • 3.4 Training curves, baselines, and errors that matter
  • 3.5 Put it into practice
  • 3.6 Guided comparison
  • 3.7 An imbalanced alert classifier

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 3.2 2 of 7

A prediction, a parameter, and a loss

Focus

Calculate a prediction and loss while distinguishing a parameter from a chosen setting.

A simple numerical predictor multiplies one input x by a learned weight w: prediction=w×x. The weight is a parameter because fitting changes it. A learning rate is a hyperparameter: a setting chosen to control fitting. These roles differ even though both may be numbers stored in code.

Take x=2, target=6, and weight=1. The prediction is 2. Define signed error as prediction minus target, so error=−4. A loss summarizes mismatch; half squared error is error×error/2. Squaring makes positive and negative misses contribute positively, and the half simplifies the derivative. The loss here is 16/2=8. It is not an accuracy percentage or a probability.

If the weight rises to 2, prediction becomes 4 and loss becomes 2. At weight 3, prediction is 6 and loss is zero for this example. This does not prove weight 3 will fit every dataset. With many records, calculate a loss for each and combine them, commonly with their mean, so training balances the observed examples.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. For input 2 and weight 1, what is the prediction of prediction=weight×input?
2. A prediction is 2 and its target is 6. What is half squared error?
3. A fitting loop changes weight w but a developer sets learning rate 0.1. Which is the learned parameter?

Section 3.3 3 of 7

Slope, learning rate, and a weight update

Focus

Use the slope of loss to calculate a gradient-descent update.

A slope tells how a function changes as its input changes. For the loss viewed as a function of weight, a negative slope means a small increase in weight decreases loss. A positive slope means a small decrease is promising. A gradient is the collection of such slopes for the model's parameters; one parameter gives one number.

For half squared error with prediction=w×x, the weight's slope is (w×x−target)×x. This follows because changing w changes prediction by x times that change. At x=2,target=6,w=1, the slope is (2−6)×2=−8. Gradient descent subtracts learning_rate×slope. At rate 0.1, the new weight is 1−0.1×(−8)=1.8. Its prediction is 3.6 and its loss is (3.6−6)²/2=2.88, below 8.

The rate controls distance, not direction. At rate 1, weight becomes 9 and loss becomes 72: the update overshoots. A local downhill direction does not guarantee that an arbitrarily long step remains downhill.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. For x=2,target=6,w=1, the slope (w×x−target)×x equals what?
2. Weight is 1, slope is −8, and learning rate is 0.1. What is the updated weight?
3. A downhill slope is used with a very large step and loss rises. What explains this?

Section 3.4 4 of 7

Training curves, baselines, and errors that matter

Focus

Separate fitting improvement from validation evidence and interpret class errors.

A training curve records loss over repeated updates. If training loss falls, the model is fitting those records better. Generalization means useful performance on new records from the intended deployment setting. Validation performance provides evidence about that separate question. A flexible model can memorize training details while becoming worse on validation data; this pattern is called overfitting.

Compare models with a baseline and metrics suited to the task. Accuracy is correct predictions divided by all predictions. Precision for the positive class is true positives divided by all predicted positives. Recall is true positives divided by all actual positives. Suppose an alert model produces 8 true positives, 2 false positives, and 12 false negatives. Precision is 8/(8+2)=0.8, while recall is 8/(8+12)=0.4. Its alerts are often correct but it misses many positives.

Choose a model or threshold using validation evidence and the costs of misses and false alarms. After choosing, evaluate the untouched test set. Do not select the best model based on repeatedly observed final test scores.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Training loss decreases while validation loss increases over several epochs. What does this suggest?
2. An alert model has 8 true positives and 2 false positives. What is positive precision?
3. A classifier has 8 true positives and 12 false negatives. What is recall?

Section 3.5 5 of 7

Put it into practice

Focus

Run and verify numerical updates before comparing learning-rate behavior.

NumPy arrays support elementwise arithmetic. Python's ** operator raises a number to a power; an expression such as error**2 squares it. A for loop repeats indented statements. print displays a result. Use these operations to expose each stage of fitting rather than hiding it behind a model.fit call.

x, target, weight, rate = 2.0, 6.0, 1.0, 0.1
prediction = weight * x
loss = (prediction - target) ** 2 / 2
gradient = (prediction - target) * x
updated = weight - rate * gradient
new_loss = (updated * x - target) ** 2 / 2
print(prediction, loss, gradient, updated, new_loss)

The expected values are approximately 2,8,−8,1.8,2.88. Floating-point arithmetic represents many decimals approximately, so compare with a tolerance rather than exact equality. NumPy's allclose performs that comparison for several values.

Download week01.py and run ./.venv/Scripts/python.exe week01.py --lesson 3. It checks those intermediate values and repeats updates using rates 0.01,0.1,1.0. Before inspecting the output, predict which rate takes a gentle step and which first increases loss. The printed sequences are measured computations for this scalar function, not evidence that one rate works for every neural network.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Python evaluates (2−6)**2/2. What does **2 do?
2. A calculated weight prints 1.8000000000000003. How should it be checked against expected 1.8?
3. A scalar update starts at w=1 with gradient −8. Which learning rate first produces w=9?

Section 3.6 6 of 7

Guided comparison

Focus

Check the signs and intermediate values of a complete gradient step.

Start with input 2, target 6, weight 1. Prediction 2 is too small, so a useful first change increases the weight. Error=2−6=−4; gradient=−4×2=−8. Subtracting a negative gradient increases the weight: at rate 0.1 it rises to 1.8. This sign check catches the common mistake of adding the gradient and moving to 0.2.

Recompute the result after updating; do not reuse the old prediction. Weight 1.8 predicts 3.6. New error=−2.4 and new loss=2.88. The loss dropped by 5.12, but that improvement concerns this example only. At the next iteration the gradient is −2.4×2=−4.8, not the original −8. The learning rate can stay fixed while the gradient changes with the weight.

Rate 0.01 instead produces weight 1.08 and loss 7.3728 after one update, a smaller improvement. Rate 1 gives weight 9, prediction 18, and loss 72. Comparing these complete paths explains the outcome better than judging only how far a parameter moved. The lab asserts the first-step values and prints repeated losses so an unexpected sign or stale-gradient error becomes visible.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. For prediction=w×x and loss=(prediction−target)²/2, what is the weight gradient at w=1.8, x=2, target=6?
2. Use prediction=w×x, loss=(prediction−target)²/2, and w_new=w−rate×gradient. For x=2,target=6,w=1,rate=0.01, what is the new weight?
3. An update reports a new weight but evaluates loss using the old prediction. What is wrong?

Section 3.7 7 of 7

An imbalanced alert classifier

Focus

Recognize when a high accuracy conceals a useless positive-class detector.

A network alarm dataset contains 990 normal events and 10 genuine incidents. An always-normal model is correct on 990/1000 events, giving 99% accuracy. It detects zero incidents. Its positive recall is 0/10=0. A high aggregate score therefore conceals the failure that matters operationally.

Now compare a model that detects eight incidents, misses two, and falsely flags twenty normal events. Its positive precision is 8/(8+20), approximately 0.286, and recall is 8/10=0.8. It detects more incidents but creates more investigation work. The choice depends on the cost of missed incidents and the available review capacity, not a slogan that one metric is always best.

Use the training partition to fit, validation to compare choices, and test once the choice is fixed. Report counts alongside fractions so a reader sees the denominator. Ten incidents are a small sample; eight detected incidents do not guarantee exactly 80% recall on all future traffic. The model comparison is evidence for this evaluated setting.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. An always-normal model sees 990 normal events and 10 incidents. What are accuracy and incident recall?
2. An alarm model detects 8 incidents and produces 20 false alarms. What is its positive precision?
3. A comparison reports eight detected incidents out of ten. What should accompany “80% recall”?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Tensors and a neural network forward pass →