Skip to content

Introduction: Five Phases, One Loop

Before you write a line of code, it helps to know where the thing you are about to build sits. Artificial intelligence came in five waves, and each wave kept what worked and changed one idea. The model in this book belongs to the fourth wave, and it inherits almost everything from the second.

The five phases of artificial intelligence on a timeline Figure I.1: Five waves of artificial intelligence. From the second wave onward, every one of them learns the same way.

Where this book sits

There are about four phases that I'll break down. Phase one was 1950 to roughly 1985. You wrote down a set of rules by hand. If you want to do X you will do Y. There were thousands of these rules that were being written down. ELIZA came out in 1966. It mimicked being a therapist by finding key words in your statement and asking you a question. It knew nothing but humans spoke to it as if it did. We still assume something understands what it's saying if it can speak in proper sentences.

Phase two was 1985 to 2011. We no longer wrote down the rule. By giving a computer 10,000 examples of emails and whether they were spam or not, we can allow it to figure it out on its own. This is where our math from the book comes into play. Computers only understand numbers. So you make a guess and see how far off you are and update your numbers. Later models use this technique.

Phase three was 2012-2016. More layers of numbers with more data to compute it on. An image classifier using this technique beat its competition by such a large margin that no one could dispute it in 2012.

Phase four was 2017-2023. Learning what comes next. Instead of classifying what something was it predicted what was next. Two things made this possible. One is taking all words and creating vectors. This allows us to group words together that have similar meanings to them in the vector space. Two is attention. Attention was introduced in 2017 and allows you to look at all previous words and weight their importance when predicting what will come next. The rest of this book will walk you through these concepts on a smaller scale.

The fifth phase is 2023 to today. Not much changed in the model itself. Give it some objectives and it can call functions and create a task list that it'll complete step by step. It's still predicting the next word, and that's what you're going to make in this book.

Table I.1: The five phases, and what changed at each one.

Phase Roughly What changed What the machine does
Rules 1950 to 1985 People write every rule Matches patterns, follows conditions
Machine learning 1985 to 2011 Learn the rule from examples Guess, measure the error, adjust
Deep learning 2012 to 2016 Far more layers and data The same loop, much bigger
Generative AI 2017 to 2023 Produce the next piece, not a label Embeddings and attention, same loop
Agentic AI 2023 onward Give the model tools and a goal The same model, inside a system

One loop, running for forty years

Every phase from the second onward runs the same three steps.

The training loop: guess, measure the error, adjust the weights Figure I.2: The loop behind every model from the second phase onward.

It makes a prediction, then we measure how incorrect the prediction was. Then another process changes the weights to hopefully get a better prediction. And it does this over and over again millions of times. You will build all three parts. Predictions in Chapter 10, measuring error in Chapter 11 and changing weights in Chapter 13.

There is no step that asks if what is produced is truthful. The model is rewarded for producing text that fits the pattern of its training data. A made-up answer fits that pattern just as well as a true one. So you will see language models produce false statements with confidence. You cannot engineer your way out of this, since the loop only cares about fitting the pattern, and you will notice it happen when your own model writes made-up Shakespeare in Chapter 17.

A layer is a stack of small regressions

Once you draw a line of best fit on a scatter plot you've trained a model. You've found a slope and intercept, calculated how far off each point was and then found the slope and intercept that minimize that error. It's as simple as a guess, a measuring stick and finding the closest fit.

Neural networks just repeat this process over and over. Every node in a neural network will take the input into that layer multiply it by its own set of weights and sum them together. It'll also add a bias which acts as the intercept. This means we now have a slope for every input and an intercept for each unit. This means every node in a neural network is a regression. By multiplying the inputs with our set of weights we're doing this for hundreds of nodes at a time in one matrix multiplication. We can then chain these layers together to make a network. Nothing in this book will be harder than that but there is a lot more to it. In Chapter 3 we'll dive deeper into the matrices and in Chapter 8 we'll create a layer.

The four parts you are about to build

Follow one guess from a chatbot and you pass through four pieces, in this order.

  • The dictionary. Words are turned into lists of numbers, positioned so that words used alike sit near each other. This is the embedding, covered in Chapter 5.
  • The structure. Layers of numbers, each doing its small job and handing the result to the next. Attention lives here, in Chapters 6 through 10.
  • The ruler. A single number saying how far the guess landed from the right answer. This is the loss, covered in Chapter 11.
  • The messenger. The error is carried back through every layer, telling each weight which way to move. This is backpropagation, and it runs the training in Chapter 13.

Why build it by hand

Having a calculator is great if you know math. If you don't know how to do math then you won't know when the calculator returns an incorrect number. Because you would have no way of knowing that it was incorrect.

You can ask a language model to generate code for you, write you a memo or even tell you about itself. If you have not made one yourself, you won't be able to tell when it is incorrect or describe why it's wrong. At the end of this you will have created each component of a language model and been able to train it on your computer. Watch it decrease in error and see what it has to say for itself. It will no longer look like some fancy paragraph on your screen.

Further Reading

Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2017). ImageNet classification with deep convolutional neural networks. Communications of the ACM, 60(6), 84–90. https://doi.org/10.1145/3065386

Rosenblatt, F. (1958). The perceptron: A probabilistic model for information storage and organization in the brain. Psychological Review, 65(6), 386–408. https://doi.org/10.1037/h0042519

Rumelhart, D. E., Hinton, G. E., & Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323, 533–536. https://doi.org/10.1038/323533a0

Weizenbaum, J. (1966). ELIZA: A computer program for the study of natural language communication between man and machine. Communications of the ACM, 9(1), 36–45. https://doi.org/10.1145/365153.365168

Next up: Chapter 1, Setting Up Your Environment