AI Run Lab

All chapters

From a line of code to a learned rule

A shop receives the question “Can I return my order?” We can write down the return policy, search for a delivery record, learn to recognise the intent and generate a reply. Each operation needs different information.

Open this chapter with experiments and audio

What happens when a computer runs code?

Suppose a shop allows returns up to and including 30 days after delivery. A person writes down an instruction the computer can execute. The instruction days <= 30 compares two numbers and produces one of two values, true or false. Humans decide that delivery day is day zero and that day 30 still counts. The comparison implements those decisions.

InputCalculationValue the program returns
29 days29 ≤ 30True
30 days30 ≤ 30True
31 days31 ≤ 30False

A variable gives a value a name such as days. A function packages the comparison so several parts of the shop can call it. An if instruction chooses what runs next based on the result. A loop can apply the same function to each item in a basket. These four ideas also appear in spreadsheets, booking systems and model-training programs.

What does a change of one character do?

Replace ≤ with <. The results for 29 and 31 stay the same. Day 30 changes to false. Those three test cases let you detect a boundary error without reading the whole program.

Real software also moves data. A browser sends the order number to an application. The application looks up the delivery date in a database, calculates the elapsed days and calls the function. If the database has no delivery date, the program needs a branch for “unknown”. Treating a missing date as zero would make an undelivered order appear eligible.

In a learning system a programmer still writes the data-reading code, the model operations, the measure of error and the update routine. Training supplies values for adjustable numbers in those operations. The same program can load different saved values and make a different prediction. The next section tests this difference with a room whose true area we can calculate.

Python tutorial · Conditions, loops and functions

Optional calculation · count the adjustable numbers in a network

A program can multiply two room dimensions using a rule supplied by its developer. A neural network can learn relationships by adjusting numbers called parameters. Where do those numbers sit, and how many are there?

Each line below carries one weight. Each hidden or output unit also has one bias, a number added to its weighted inputs. Change a layer size and watch which connections appear.

Here is a separate design problem. Suppose a survey records four measurements per building and the model predicts heating demand and electricity use. The designer chose eight intermediate units. These widths let us count the weights and biases before training. To estimate their storage in bytes we also need the number format.

Set the layers to 4 inputs, 8 hidden units and 2 outputs. Each of the 8 hidden units receives 4 weights, giving 32. The outputs add 8 × 2 = 16 weights. Add 8 + 2 biases and the total is 58 trainable numbers. Increase the hidden layer to 9. One extra hidden unit needs four incoming weights, two outgoing weights and one bias. The new total is 65.

Changing the layer sizes redesigns the model. During ordinary training those sizes stay fixed while the numerical values change. The diagram counts the places where learning can happen. It tells us nothing yet about which values training will find.

Who supplies the rule, and which kinds of AI use it?

A rectangular floor has area length × width. Once the two lengths are known, a programmer can write one multiplication. In a learned system, the developer can instead supply examples of rooms and their areas, choose a family of possible functions and let training search for useful settings. Both systems perform calculations. The difference lies in how their rules are obtained.

For example, a model might assign weight 2 to the length and weight 3 to the width. With a 5 by 5 room, that gives 2 × 5 + 3 × 5 = 25. For a 10 by 10 room it gives 50, although the floor covers 100 m². The first correct answer left the rule undetermined. The second room helps distinguish two explanations.

AI is a field that studies how machines can perceive, predict, search, reason and act. An LLM is one family in that field. To compare systems, first name the job and its output. Then ask how the system acquires the procedure that produces it.

JobOne workable mechanismWhat can you test?
Plan a delivery routeSearch candidate paths using travel times and a goal.Does the path reach the address, and how long is it?
Route a customer messageFit a classifier to messages labelled “return”, “delivery” or “other”.Can it route unfamiliar wording?
Group similar purchasesCluster item vectors by a chosen distance without supplying group labels.Do the resulting groups remain meaningful for new items?
Identify a damaged item in a pictureTrain a vision model on images and annotations.Does accuracy hold under a different camera or light?
Learn to select actionsA reinforcement-learning policy uses experience with actions and rewards.Does following the policy improve the outcome across new episodes?
Draft a responseA generative language model estimates possible next tokens given the text so far.Does the reply fit the request and the recorded facts?

These categories overlap. “Vision” describes what goes in. “Classification” describes what comes out. “Supervised learning” describes how examples guide the update. A single model can be all three. A route planner can run a hand-designed search with a travel-time estimate learned from data. An LLM can receive reward-based training and later be used by an agent.

Russell & Norvig · Search, planning, learning and perception · scikit-learn · Supervised learning methods

What happens to the words in the question?

The input to a calculator could be the two numbers 5 and 5. A language model receives a sentence. A tokenizer first maps pieces of the sentence to integer IDs. Learned tables turn IDs into vectors. Successive layers combine information across positions, producing scores for possible next tokens. The application can present the generated tokens as an answer.

1. Question

How many square metres is a room five metres by five metres?

2. Representation

Token IDs become vectors whose values are transformed by the network.

3. Possible answer

The model can generate “25 m²” or propose a calculator call.

The displayed answer alone does not tell you whether a model recalled a common pattern, performed several internal steps or received a tool result. You can inspect the application’s tool record to establish whether an external calculation actually ran. Follow a question through a Transformer or inspect a tool call.

Test an explanation with another room

Run this Python 3 example with the standard library. Both proposed rules see exactly the same inputs. Predict the last row before looking at the output. This small comparison illustrates why evaluation uses examples that were held back during development.

for length, width in [(5, 5), (10, 10), (3, 8)]:
    area = length * width
    prediction = 2 * length + 3 * width
    print(length, width, area, prediction)
What changes when the room measures 3 by 8 metres?

The area is 24. The weighted sum gives 30. Swapping the dimensions keeps the area constant but changes the weighted sum to 25. Testing symmetry can reveal a wrong rule.

PyTorch · Learn the Basics