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.
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.
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.
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.
3. Build a response. The model repeats the process, adding tokens one at a time. Step through this simplified example:
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.
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.
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.
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:
- Hands-On Large Language Models by Jay Alammar and Maarten Grootendorst uses illustrations and code to explain language models, from the basics through using and adapting them. Start here if you'd like a visual introduction and know some Python.
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow by Aurélien Géron covers machine learning more broadly, with practical Python examples. Pick this if you want to understand models beyond language.
- Build a Large Language Model (From Scratch) by Sebastian Raschka takes you through building and training a small GPT-style model. It's a deeper next step for readers comfortable with Python and some machine learning.