Skip to main content

Available now!

Chapter 2: How AI Learns & Makes Decisions

From Patterns to Predictions

From Patterns to Predictions

So far, we’ve explored how AI models learn patterns from data and apply that knowledge to new information. Now, we’re going to look more closely at how models use those patterns to produce an output.

In many AI systems, producing an output involves making a prediction. In AI, a prediction doesn’t always mean forecasting what will happen in the future. Depending on the task, a prediction can take different forms.

Prediction can mean recognizing what something is. Image Classification is where the model uses learned patterns to predict which category an image is most likely to belong to.

A schnauzer photo, a cat photo and a black dog photo. The black dog is highlighted and labelled "Dog, 92% confident", showing a model's prediction.

Prediction can mean recommending what you might like. Recommendation systems use patterns in previous activity to predict what someone might be interested in next.

Recommendation example: three nature-documentary posters (Frozen Kingdom, The Wild North and The Scenic Route) under "Because you watched…", and a highlighted Canadian Roots poster under "You might like this".

Prediction can mean predicting what comes next. Language Modelling uses patterns in language and the context so far to predict what is most likely to come next.

A sentence starting "Don't forget to bring an umbrella. It looks like it might" with four possible next words and their likelihoods: rain 42%, storm 28%, pour 20%, shower 10%.

Whether a model is identifying an image, recommending content, or predicting what comes next in a sentence, its prediction is based on patterns learned during training. Next, we’ll look more closely at how models identify those patterns and relationships across many examples.

Finding Patterns

During training, a model works through many examples and identifies statistical patterns and relationships within the data. These patterns help the model distinguish between different kinds of information.

For example, imagine a model being trained to recognize handwritten numbers. It might be shown many examples of each number, written in different ways.

Examples from the Training Data

No two handwritten 7’s look exactly alike. But across many examples, the model can identify patterns in the visual information that help distinguish a 7 from a 3, a 1, or another number.

Three rows of handwritten digits showing the same number written many different ways: a 7, a 3 and a 4, each with four varied examples.

The model isn’t memorizing one perfect version of each number. It is learning which statistical patterns and relationships appear across the training data. This allows it to use what it has learned when it encounters an entirely new example.

Making a Prediction

Imagine we now give our handwriting model an image it has never seen before. Rather than looking for an exact match in its training data, the model uses the patterns it learned to estimate which number the new image is most likely to represent.

A new handwritten 3 with an arrow to the model's prediction: 3 at 91%, 9 at 5%, 7 at 4% and 6 below 1%.

The model might assign the highest probability to 3, while assigning lower probabilities to other possibilities. This is what we mean by a prediction: the model is estimating the most likely output based on patterns learned during training (Zhang et al., 2023). A prediction represents what the model considers most likely. It does not mean the model is certain.

How Generative AI Predicts

When you enter a prompt, the model processes the text as smaller units called tokens. A token might be a whole word, part of a word, punctuation, or piece of text.

Four examples of what a token can be, each shown as a coloured chip: a whole word, part of a word, a punctuation mark, or another small piece of text.

For example, a short sentence might be broken into tokens like this:

Two possible tokenizations of the same sentence: in the first, each word is one token; in the second, “running” is split into two tokens, “run” and “ning”.

The exact tokens can vary depending on the model. The important idea is that a language model works with these smaller pieces of text rather than simply processing language one whole sentence at a time. The model then uses patterns learned during training to calculate probabilities for which token could come next based on context so far (Zhang et al., 2023).

Imagine beginning a sentence with:

“The students opened their books and turned to…”

What could plausibly come next?

Several possibilities could reasonably come next. Based on the context and patterns learned during training, the model determines which possibilities are more or less likely and generates the next token. Then it does this again.

Each new token becomes part of the context used to predict what comes next. Because the model has learned complex patterns and relationships across language during training, these predictions can build on the words and ideas that came before.

Why the Same Prompt Can Produce Different Answers

If you’ve used generative AI more than once, you may have noticed that entering the same prompt doesn’t always produce the same response. The reason comes back to prediction and probability.

A calculator provides a useful comparison. Enter the same calculation into a calculator, and you expect the same answer each time. A language model works differently. As we saw earlier with the handwritten numbers, a model considers multiple possibilities for what could come next, with different probabilities assigned to each.

Comparison: a calculator gives the same result every time for 2+2, while a generative AI given the same prompt three times gives a different response each time.

A language model does not always follow the same sequence of predictions. A different choice early in the response changes the context for what comes next (Zhang et al., 2023). As this process continues, those differences can lead to responses with different wording, examples, explanations, or conclusions.

Confidently Wrong

Understanding how generative AI produces a response helps explain one of its important limitations. A language model generates text based on patterns and probabilities, but a response that fits those patterns is not necessarily factually correct. This means AI can produce information that sounds convincing while still getting facts wrong, combining information incorrectly, or even inventing details. These types of errors are often referred to as hallucinations (Huang et al., 2023).

Different models and tools vary in their accuracy, but no generative AI system should automatically be assumed to be correct. When accuracy matters, important information should be checked against a reliable source.

We’ll explore hallucinations, inaccuracies, and the risks they can create in much greater detail in Chapter 3: Risks and Realities.