Training Models: Loss & Optimization

</>Coding Tamilan.← AI course & curriculum

AI · LESSON 10 / 25 · English

Training Models: Loss & Optimization

Measure the error, then adjust with care.

Beginner-friendly explanationsAI method + worked scenarioPractice + answer

Content reviewed: 22 October 2026 · தமிழில் படிக்க ↗

01 / THE IDEA

Understand it in plain language

A loss function turns prediction errors into a quantity to optimize. An optimizer changes model parameters; the learning rate controls the update size. Training loss, validation quality, and the real product goal are related but different. A lower loss on familiar examples does not by itself prove useful generalization.

How the AI approach works

Training is an experiment with a defined objective. Choose a model, initialize parameters, make predictions, compute loss, calculate gradients, and update parameters. An epoch is a pass through the training set; a batch is the subset used for one update. Stochastic or mini-batch gradients estimate the full-data gradient, so the loss need not decrease on every individual step.

For squared-error regression, large residuals receive a larger penalty. Cross-entropy is commonly used for classification and next-token prediction. The loss must match the output representation and task. Lower next-token loss measures a prediction objective; it does not directly certify truthfulness, helpfulness, or successful tool execution.

Gradient descent updates θ ← θ − η∇L. The gradient points toward local increase of loss; subtracting a suitably sized step aims to reduce it. The learning rate η is crucial: a large step may overshoot or diverge; a tiny step may learn too slowly. Optimizers such as SGD and Adam differ in how they form updates. Neither eliminates the need for validation.

Track training and validation curves, save the best validated checkpoint, and record data versions, random seeds, and configuration. Keep the final test out of tuning. If training fails, check shapes, labels, input scales, and whether a tiny batch can be fit before launching a costly run.

02 / STEP BY STEP

Break the idea into steps

  1. Run a forward calculation and compute the loss.
  2. Calculate gradients and update parameters using the chosen optimizer.
  3. Track held-out performance and save enough configuration to reproduce a run.

Worked example: connect the method to the task

Use three training pairs: x = 1, 2, 3 and y = 2, 4, 6. For ŷ = wx and mean squared loss, w = 0 gives loss 56/3 ≈ 18.67. The gradient is the mean of 2x(wx−y), which is −56/3 at this point. With learning rate 0.1, the first updated weight is about 1.867, much closer to 2.

The executable experiment repeats the update and tests x = 4, which was not in training. This clean synthetic relationship demonstrates fitting and held-out prediction; real data needs noise analysis, stronger splits, and comparison against baselines.

Algorithm experiment: predict, change, explain

This fits a regression weight from three data pairs with an actual loss gradient. Try a learning rate of 0.3 and observe instability. The held-out synthetic point is a learning illustration, not a real forecasting benchmark.

main.py · focused experimentPYTHON
data = [(1.0, 2.0), (2.0, 4.0), (3.0, 6.0)]
w, learning_rate = 0.0, 0.1
for epoch in range(20):
    loss = sum((w*x-y)**2 for x, y in data) / len(data)
    gradient = sum(2*x*(w*x-y) for x, y in data) / len(data)
    if epoch in (0, 1, 19):
        print("epoch:", epoch, "weight:", round(w, 4),
              "loss:", round(loss, 4))
    w -= learning_rate * gradient
print("held-out x=4 prediction:", round(w * 4, 4))
Predict first, then reveal the checked output
epoch: 0 weight: 0.0 loss: 18.6667
epoch: 1 weight: 1.8667 loss: 0.083
epoch: 19 weight: 2.0 loss: 0.0
held-out x=4 prediction: 8.0
04 / TRY IT

Change one thing. Run it again.

Run this small, self-contained experiment in standard Python. No package installation or paid API is required. Change the suggested variable and explain the effect on the method before applying it to real data.

Open Python editor ↗

Let the editor finish loading, load this example, then press Run.

The free editor uses OneCompiler, an external service. Code you run is sent there. Do not enter passwords, API keys, or private student data. If an example is sent before the editor is ready, wait and load it again.

Apply the idea: check your reasoning

Think before revealing the answer

Why should we not select an epoch using the final test score?

Reveal the answer and reasoning

That turns the test into a tuning set and biases the final estimate. Select the checkpoint with validation data; use the test for a final independent assessment.

A common mistake: Do not tune repeatedly on the final test set; it must remain an honest final check.

Before you move on

  • Explain the main idea without repeating the technical label.
  • Identify the approach, its assumptions, and the evidence needed to trust the result.
  • Complete the challenge, then compare your reasoning with the answer.

Sources & a deeper next step

The refresh uses primary papers and official documentation. These optional links are in English; you can understand this lesson without reading them first. Product versions and availability can change.

Coding Tamilan · Understand the idea. Test the claim. Build responsibly.