Understanding AI Models

I use AI tools to help me understand ideas and write code. But I wanted a clearer picture of what an AI model actually is and what happens when I ask it a question.

Lee Robinson's introduction to AI helped me explore the topic. This is my simpler explanation of the ideas that helped me understand a model, without going too far into the mathematics.

If you're a student, each section ends with the campus units that build that part of the skill. The unit codes come from the University of Nairobi's BSc Computer Science course structure. Other universities use different names and codes, so match units by topic. Where a topic usually isn't taught as its own undergraduate unit, I've added a free course from a university that teaches it.

What is an AI model?

An AI model is a system trained on examples to recognize patterns and use them to produce an output. Depending on how it was built and trained, it might classify an image, predict a number, or generate text.

A language model works with text. Give it a question or an instruction, and it generates a response based on patterns it learned during training and the context you provide.

In traditional software, a developer writes rules for the program to follow. With a model, developers also write the software, but the model learns many of its patterns from data instead of having every response written by hand.

Rules: written vs. learnedIllustrative
Traditional software compared with machine learningTop row: a person writes a rule, FREE in the subject means spam. The program applies it to a new email and outputs spam. Bottom row: 10,000 example emails and their labels go into a repeated training loop, and the output is a learned vector of parameters, which plays the role of the rule."FREE" → spam"FREE trip!"spam× 10,000spam | ok | …w

How does a model learn?

Training starts with data. For a language model, that can include text and code from a variety of sources. The quality and makeup of that data affect what the model can learn.

The model uses a mathematical structure called a neural network. During training, it makes predictions, measures how far those predictions are from the expected answers, and adjusts internal numbers called parameters. This process is repeated across many examples.

Training loopIllustrative
1/4
The training loop for one exampleInput: The sky is. With parameters 0.25, 0.7, 0.45, the model predicts blue 20%, green 45%, cold 35%. The target is blue, so the loss is 1.61. The gradient adjusts the parameters, and over four steps the loss falls.The sky isw0.250.700.45pblue20%green45%cold35%blueL = 1.61−∇L

Those parameters hold the patterns the model has learned. They are not a neatly organized set of facts, and learning a pattern does not guarantee that the model will always apply it correctly.

The illustration follows one example through a few rounds of that loop: the prediction for “blue” improves as the parameters are adjusted, and the error falls. Google's neural network glossary includes another diagram if you want to see how the layers connect.

What happens when you ask a question?

Once a model has been trained, using it is called inference. For a language model, the process looks roughly like this:

1. Break the question into tokens. A token might be a word, part of a word, or punctuation.

Text → tokens → IDs → vectorsIllustrative
Tokenizing: Why is the sky blue?The text "Why is the sky blue?" (20 characters) splits into 6 tokens: "Why", " is", " the", " sky", " blue", "?". Each token maps to an ID, 10445, 382, 290, 17307, 9861, 30, and each ID to a vector of numbers.Why is the sky blue?Why10445·is382·the290·sky17307·blue9861?302066 × 4

2. Predict the next token. The model processes the tokens and scores possible next tokens based on the context. Those scores become probabilities, and one token is picked from them. It isn't always the most likely one: a setting called temperature controls how adventurous the pick is, which is one reason the same question can get different answers.

Next-token probabilitiesIllustrative
Next-token probabilities for The sky isAt temperature 1, the scores blue 4, clear 2.9, gray 2.4, dark 1.6, falling 0.6 become probabilities blue 60%, clear 20%, gray 12%, dark 5%, falling 2%.The sky iszsoftmax(z / T)T = 1.0blue4.060%clear2.920%gray2.412%dark1.65%falling0.62%

3. Build a response. The model repeats the process, adding tokens one at a time. Step through this simplified example:

Generation loopIllustrative
Generating a response one token at a timeStarting from the prompt The sky is, the model predicts blue. The sky is blue becomes the next input and the model predicts today, then a full stop, then an end token that stops generation. 1 of 4 steps are shown.1Theskyisblue

Many language models use a structure called a transformer. It uses attention to help the model consider relationships between different parts of the text. This helps it use the surrounding context when producing a response. Choose a word below to see which earlier words it draws on.

AttentionIllustrative
Attention from the word itIn the sentence The cat sat on the mat because it was tired, the word it pays the most attention to cat (52%) and mat (22%). Words after it are not used.The3%cat52%sat6%on2%the3%mat22%because12%itwas–tired–

A coding example

Imagine a language model trained on examples that include source code. During training, it can learn patterns in programming languages: how a function is written, how a loop is structured, and how code relates to a description of a task.

Later, you might ask it, “Write a Python function that sorts a list.” The model uses your request and the patterns it learned to generate code. It may produce a useful function, but it can also make mistakes, so you still need to read and test the result.

Can a model keep improving?

After the initial training, a model can be trained further for particular tasks or behaviors. This is called fine-tuning. For example, a team might use additional examples to help a model follow instructions or work better on a specific kind of task.

Teams also evaluate models by testing them on examples and checking the results. These evaluations help reveal where a model does well and where it makes mistakes. They matter because an answer can sound confident and still be wrong.

Pretrain → fine-tune → evaluateIllustrative
Pretraining, fine-tuning and evaluationPretraining on about 10 to the 13 tokens, fine-tuning on about 10 to the 5 examples, then evaluation, where 4 of 5 test cases pass and failures loop back to fine-tuning. Asked What is the capital of France, the pretrained model continues with more quiz questions.Pretrain10¹³ tokensFine-tune10⁵ examplesEvaluate4/5What is the capital of France?What is the capital of Germany? What is thecapital of Spain? What is the capital of Italy?

A model and an application are different

The model is the system that processes input and produces output. An AI application is the software built around it: the interface you use, the instructions sent to the model, and any tools or information the application provides.

Inside an AI applicationIllustrative
How an application uses a modelInside an application, the context is assembled from a system instruction, a retrieved document, a tool definition, earlier messages and the user's message, about 1,850 tokens. 1: the context goes to the model. 2: the model requests the lookup_order tool for order A123. 3: the application runs the tool and adds the result, status delivered, to the context. 4: the model writes the answer shown to the user.system: internal supportreturns-policy.mdlookup_order(id)messages × 2"Return order A123?"≈ 1,850 tokenslookup_order("A123"){ status: "delivered" }"Yes, eligible."1234

For example, GPT refers to a family of models, while ChatGPT is an application that lets people interact with models. An application can use a model without having trained that model itself.

The distinction helps me think about the tools I use every day. The model provides a capability; the application decides how to put it to work. Understanding that much is a useful place to start, even if you never train a model yourself.

Where to go next

If you want to see these ideas in action, start with a short sentence in OpenAI's tokenizer. Change a word or add punctuation and see how the tokens change. Then ask a language model to explain the sentence and compare its answer with your own understanding.

If you'd rather learn from a book, these are useful places to continue: