AI Fundamentals · Guide 2 of 35

How AI Models Learn: Training and Inference

Follow data through training, evaluation and inference, and learn why a model can perform well in training yet fail in use.

BeginnerAbout 6 min readPractical exercise

Before you start: AI, machine learning and deep learning. Reading time is an estimate; allow extra time for practice.

What you’ll learn

  • Distinguish learned parameters from inputs and application settings.
  • Fit and evaluate a small prediction rule using separate data.
  • Choose data splits that avoid duplicates and information from the future.
  • Explain why changing conditions require monitoring after deployment.
On this page

Training changes the model

Suppose a dispatch screen needs to estimate delivery hours before a parcel leaves the depot. You have past distances and actual delivery times. The task is to find a useful relationship in those past examples, then apply it to a new parcel whose arrival time is still unknown.

During training, an algorithm adjusts model parameters to improve an objective. In supervised learning, examples include inputs and desired outputs. A delivery-time model might take distance and parcel size as inputs and learn from actual delivery times. A loss function measures how far its predictions are from the targets; an optimizer uses that feedback to update parameters.

Training data, parameters and application settings are different things. Changing an instruction sent to a hosted language model usually changes the input for that request, not its weights. Fine-tuning is a separate training process. Whether a provider retains or uses submitted data is a separate service-policy question; do not infer it from the word “inference”.

Keep an honest evaluation set

A training set supplies examples for fitting parameters. A validation set helps choose model settings. A final test set estimates performance after those choices are made. If you repeatedly choose a model because it performs well on the test set, the test set is effectively becoming another validation set.

Split data along the boundaries of the real problem. Randomly dividing nearly identical tickets from the same conversation can put a copy in both training and testing. For forecasting, training on future records makes an evaluation unrealistic. Fit preprocessing, such as scaling, using training data only, and apply the learned transformation to later sets.

A time-based evaluation plan
Training:   deliveries completed in January–March
Validation: deliveries completed in April
Test:       deliveries completed in May

Inputs: facts available when the parcel is dispatched
Target: actual arrival time, known only afterwards

Do not use “time delivered” or a later tracking event as an input. That information would not exist when the prediction is needed. Dates here illustrate the split, not a required schedule.

Inference applies what was learned

At inference time, the deployed model receives an input and produces an output using its current parameters. The application still has work to do: validate the input, use the same preprocessing as training, interpret the result and decide whether it is safe to act on it. A model file alone is not a working product.

An API may return a prediction quickly even though training took a long time. Conversely, a large generative model can require substantial computation for each response. Track the latency and cost of the complete request, including retrieval, generation and validation, rather than treating “inference” as free.

A good training score is not the goal

Overfitting means the model captures details of the training examples that do not help on new examples. Compare training and validation results, inspect errors and start with a simple baseline. More training is not always better. Regularization, better data and a simpler model may help, but the evaluation must show that they do.

Even a well-tested model can degrade when conditions change. A new delivery region or a different ticket template can change the input distribution. Monitor outcomes and review the data before automatically retraining: new records can contain mistakes, private information or malicious examples.

Fit one parameter, then leave it fixed

We can make the distinction tangible without an ML library. Assume predicted delivery hours equal distance multiplied by one learned rate. The rate is a parameter; distance is an input; observed hours are the target. The example tries three possible rates and chooses the one with the smallest mean absolute error on the training rows. Mean absolute error is the average distance between predictions and targets, measured here in hours.

Run the complete example in Node.js or a browser console. This tiny grid search really fits a parameter, but it is not a realistic delivery service: the fictional data has a perfect linear relationship, no traffic and no uncertainty. Larger models usually search their parameter space with different optimization methods. The important boundary is that the held-out rows never choose the rate.

JavaScript: training followed by inference
const training = [[10, 2], [20, 4], [30, 6]];
const heldOut = [[15, 3], [25, 5]];
const candidates = [0.1, 0.2, 0.3];
function meanAbsoluteError(rows, rate) {
  return rows.reduce((sum, [km, hours]) =>
    sum + Math.abs(km * rate - hours), 0) / rows.length;
}
const rate = candidates.reduce((best, candidate) =>
  meanAbsoluteError(training, candidate) < meanAbsoluteError(training, best)
    ? candidate : best);
console.log('Learned rate:', rate);
console.log('Held-out error:', meanAbsoluteError(heldOut, rate));
console.log('Prediction for 40 km:', 40 * rate);
console.log('Error after conditions change:',
  meanAbsoluteError([[15, 6], [25, 8]], rate));

Expected values: learned rate 0.2, held-out error 0, prediction 8, changed-condition error 3. Applying the rate to 40 km is inference; evaluating changed conditions does not automatically update it. Zero error on two invented rows provides no evidence about real deliveries. A real workflow needs validation data for design choices and an independent final test.

Put it into practice

Try it yourself

Run the rate example, explain why the changed-condition error is 3 hours, then design a split for predicting ticket escalation at ticket creation. List three available inputs and two that would leak the outcome.

Show hint

The unchanged rate predicts 3 and 5 hours while the new targets are 6 and 8. For tickets, consider the initial message, product area and creation time; final resolution and escalation flags reveal the outcome. Keep each conversation in one split. Success means explaining what is learned, what is held out and what is unavailable at prediction time, not merely obtaining a low score.

Optional practice

Check your understanding

5 questions to revisit the ideas. No score is saved, and you can retry. This is separate from Skill Quiz; it does not award XP or unlock guides.

1. In the delivery example, which value is learned during training?
2. You keep choosing model settings based on the final test score. What changes?
3. Which feature leaks information when predicting escalation at ticket creation?
4. The deployed model receives a new parcel distance and calculates an estimate. What has happened?
5. The held-out error was zero, but a new region produces larger errors. What is the best next step?

Sources & further reading

Primary references for the concepts in this guide. Examples and exercises are illustrative; verify provider-specific behaviour against the version you use.