✖

Neural network: what it is and how it computes

What is a neural network? In fact, one enormous formula. Numbers go into it, are multiplied by weights and added up, and negative results are turned into zero. The same thing is repeated layer after layer, and the answer is whatever comes out of the last one. Everything the network can do is written in the weights, that is, in ordinary numbers. Do not look for anything mysterious behind the word “neuron”: the calculation was only loosely inspired by brain cells long ago.

For a language model such as ChatGPT, what goes into the network is a list of numbers standing for a piece of text, say a word, and what comes out are numbers from which the next word is chosen. How text is turned into numbers is described in the first part of the series How artificial intelligence computes. Now let’s look inside the network. We can get by with almost nothing but adding and multiplying; matrices appear only in the second-to-last section, and if you do not know them, feel free to skip it.

The neuron, the building block of a neural network

A neuron receives several numbers, the inputs. It multiplies each input by its own weight, adds up the results and adds one more number, the bias. The bias shifts the threshold at which the neuron “kicks in”: with a negative bias, the inputs together have to give more before anything shows. The result is called the weighted sum:

z=w1⋅x1+w2⋅x2+w3⋅x3+b

A large weight means “this input matters a lot”, a weight close to zero means “almost ignore this input”, and a negative weight means “the larger the input, the smaller the result”. The weights and biases together make up the network’s parameters, the numbers that are set during learning.

Three inputs 3, 1 and 2 in circles on the left, blue arrows with weights 2, −1 and 1 leading into a circle with a plus sign, and from below a green arrow labelled bias −4 leading into it. From the sum comes the number 3 into a red box with a graph of ReLU, and from it the output 3 in a red circle Three inputs 3, 1 and 2 in circles on the left, blue arrows with weights 2, −1 and 1 leading into a circle with a plus sign, and from below a green arrow labelled bias −4 leading into it. From the sum comes the number 3 into a red box with a graph of ReLU, and from it the output 3 in a red circle

Inputs can be numbers from a token vector, the brightness of the pixels of an image, or anything else that can be written as numbers. We will simply make them up and recalculate the neuron from the picture: inputs 3, 1 and 2, weights 2, −1 and 1, bias −4. We substitute:

z=2⋅3+(−1)⋅1+1⋅2+(−4)

After multiplying and adding:

z=6−1+2−4=3

But different inputs are enough, say 1, 3 and 0, and we get z = 2 − 3 + 0 − 4 = −5. What to do with such a negative number is decided by the last step of the neuron.

The ReLU activation function

The neuron does not hand over the weighted sum yet; first it runs it through an activation function. It is called that because it decides whether the neuron “activates”, that is, passes on something non-zero, and how strongly. The simplest and a very widespread one is ReLU (short for rectified linear unit). It works almost laughably simply: it lets a positive number through unchanged and turns a negative one into zero.

ReLU(z)=max(0,z)

Graph of the function ReLU: for negative z the red line lies on the horizontal axis, it bends at zero and then rises diagonally as the line y = z; the vertical axis is ReLU(z). An orange point shows that for z = 3, ReLU(z) = 3 Graph of the function ReLU: for negative z the red line lies on the horizontal axis, it bends at zero and then rises diagonally as the line y = z; the vertical axis is ReLU(z). An orange point shows that for z = 3, ReLU(z) = 3

So our neuron sends out ReLU(3) = 3, and in the second case ReLU(−5) = 0. GPT-3 used a smoothed relative of this function called GELU, which does not break sharply at zero but in a rounded way, and newer models use similar functions. The principle is the same.

What is that kink good for? Without it, the network would only multiply and add. If one neuron computes 3x + 1 from the input x and the next neuron multiplies the result by two and adds 5, we get

2⋅(3x+1)+5=6x+7,

so again a single weighted sum. However many steps you put one after another, you always end up with something like a linear function of many variables, and further layers add nothing. But if the first neuron passed on ReLU(3x + 1), the graph would have a kink and no a · x + b could describe it any more. Only the kink allows the network to “bend”.

Why one layer is not enough

Let’s give a neuron a simple task. It receives two numbers x1 and x2, each 0 or 1, and has to answer 1 when exactly one of them is 1, and 0 otherwise. This is the exclusive or, “either tea or coffee, but not both”, called XOR in computer science:

x1 x2 XOR
0 0 0
0 1 1
1 0 1
1 1 0

Each row of the table is a point in the plane: x1 is the first coordinate, x2 the second. The points with the answer 1 are red in the picture, those with the answer 0 blue.

At the top a legend: a filled red dot means XOR = 1, an empty blue one XOR = 0. Below it two panels with four points in the plane. The points where one coordinate is 0 and the other 1 are filled red, the origin and the point with coordinates 1 and 1 are empty blue. On the left three grey dashed lines, none of which separates the red points from the blue ones. On the right a green shaded band between two parallel green lines, containing both red points and no blue one At the top a legend: a filled red dot means XOR = 1, an empty blue one XOR = 0. Below it two panels with four points in the plane. The points where one coordinate is 0 and the other 1 are filled red, the origin and the point with coordinates 1 and 1 are empty blue. On the left three grey dashed lines, none of which separates the red points from the blue ones. On the right a green shaded band between two parallel green lines, containing both red points and no blue one

One neuron is not enough for this. Its answer depends only on the weighted sum w1 · x1 + w2 · x2 + b, and the points where that sum is exactly zero form a line. On one side of it the sum is positive, and after ReLU the neuron passes on a positive number, which we take as the answer “yes”, that is, 1. On the other side the sum is negative, ReLU turns it into zero, and that is the answer “no”, that is, 0. A neuron can do no more than that: it cuts the plane with one line into two halves and says “yes here, no there”. But the red points lie in opposite corners of the square and the blue ones in the other two, so no line separates the red points from the blue ones. Try it yourself.

For the doubtful (the others can skip this paragraph): if such a line existed and the red points lay on the positive side (otherwise we flip the signs of both the weights and the bias), then for the red points (0, 1) and (1, 0) we would have w2 + b>0 and w1 + b>0, and adding them gives w1 + w2 + 2b>0. For the blue points (0, 0) and (1, 1) we would have b ≤ 0 and w1 + w2 + b ≤ 0, and adding them gives w1 + w2 + 2b ≤ 0. Both cannot hold at once.

The weakness of networks with a single layer was already described in 1969 by Marvin Minsky and Seymour Papert in their book Perceptrons.

A network that can do XOR

The solution is another layer. Between the inputs and the output we insert two neurons h1 and h2, a so-called hidden layer, both with ReLU. It is hidden because its numbers cannot be seen either at the input or at the output of the network. The output neuron y then simply adds up whatever reaches it:

A network in three columns: inputs x₁ and x₂, hidden neurons h₁ and h₂ in red circles with a small graph of ReLU, output y. All four arrows from the inputs to the hidden layer have weight 1, h₁ has bias 0, h₂ bias −1. The arrow from h₁ to y has weight 1, the one from h₂ weight −2. Orange numbers by the neurons show the pass for the input x₁ = 1, x₂ = 1: h₁ = 2, h₂ = 1, y = 0 A network in three columns: inputs x₁ and x₂, hidden neurons h₁ and h₂ in red circles with a small graph of ReLU, output y. All four arrows from the inputs to the hidden layer have weight 1, h₁ has bias 0, h₂ bias −1. The arrow from h₁ to y has weight 1, the one from h₂ weight −2. Orange numbers by the neurons show the pass for the input x₁ = 1, x₂ = 1: h₁ = 2, h₂ = 1, y = 0

Written as formulas:

h1=ReLU(x1+x2)

h2=ReLU(x1+x2−1)

y=h1−2⋅h2

The whole trick: h1 counts how many ones arrived, whereas h2 wakes up only when both arrive, and it knocks the result back down to zero. We start right away with the trickiest input, two ones. First the hidden layer:

h1=ReLU(1+1)=2

h2=ReLU(1+1−1)=1

Then the output:

y=2−2⋅1=0

Zero, which is right. The orange numbers in the picture show the same calculation. The remaining inputs are summarised in the table, and all four come out right:

x1 x2 h1 h2 y
0 0 0 0 0
0 1 1 0 1
1 0 1 0 1
1 1 2 1 0

The column y is exactly XOR. And here ReLU shows what it is for: without it, two zeros would give h2 = −1 and y = 0 − 2 · (−1) = 2, which is wrong.

For our four inputs, y always comes out as exactly 0 or 1. But the network can also compute a result for points in between, for example for (0.25, 0) we get y = 0.25. So we agree that the answer “yes” means y greater than 12. Where that holds is shown by the right-hand panel of the XOR picture: in the green band between the lines x1 + x2 = 0.5 and x1 + x2 = 1.5. The lower line belongs to the neuron h1: below it h1, and with it y, is less than 12. The upper line belongs to the neuron h2: above it h2 pushes the result below 12. Two neurons, two lines.

We borrowed these weights from the textbook Deep Learning (Goodfellow, Bengio, Courville, 2016). A real network finds them itself: it starts with small random numbers and gradually fine-tunes them.

A layer as multiplying a matrix by a vector

Both hidden neurons add up the same inputs, each with its own weights. If we write the weights of each neuron into one row of a matrix, that is, a table of numbers, and the inputs one below another into a vector, a single multiplication computes the whole layer:

(1111)⋅(x1x2)+(0−1)=(x1+x2x1+x2−1)

The matrix on the left, with 2 rows and 2 columns, is called a 2 × 2 matrix. The first row belongs to the neuron h1, the second to the neuron h2. Here both rows are the same, because both hidden neurons have the same weights 1 and 1 and differ only in the bias. This is ordinary matrix multiplication, only the second matrix has a single column. Each row is multiplied by the vector of inputs as a scalar product (dot product), giving the weighted sum of one neuron. For two ones:

(1111)⋅(11)=(1⋅1+1⋅11⋅1+1⋅1)=(22)

We add the biases:

(22)+(0−1)=(21)

Each component then passes through ReLU. The output layer is a matrix with a single row:

(1−2)⋅(21)=1⋅2+(−2)⋅1=0

The whole layer can therefore be written briefly as

h→=ReLU(W⋅x→+b→),

where W is the matrix of weights, x→ the vector that came from the previous layer, and b→ the vector of biases. So what is multiplied by what in the network? The vector of numbers from the previous layer by the matrix of weights. The output of one layer is the input of the next. In a language model, the vector at the start is the token vector from the first part, and instead of a 2 × 2 matrix there are matrices with thousands of rows and columns. And because graphics cards multiply matrices incredibly fast, artificial intelligence runs mainly on them.

How much a large model computes

GPT-3 from 2020 had 96 layers, and the vector of each token in it had 12,288 numbers. Each layer contains several matrices. The vector is first expanded to four times the size, 49,152 numbers, and then narrowed back again, so the two matrices have 49,152 × 12,288 entries, each over 600 million weights. On top of that, a layer has smaller matrices for so-called attention, which is covered in the part on transformer and attention. Altogether that is about 1.8 billion weights per layer, and 96 layers give roughly 175 billion parameters. The open model Llama 3.1 from Meta (2024) has 405 billion in its largest version; for the models that ChatGPT runs on, OpenAI does not disclose the numbers.

And how many calculations fall on one token? Each parameter is used roughly once for it, for one multiplication and one addition. So it comes to approximately

2⋅175 billion=350 billion

operations for every generated token, a little more for a long text (this estimate is common in the scientific literature, for example Kaplan et al., 2020). By hand, at one operation per second without sleep or breaks, you would spend about 11,000 years on a single token. An answer of five hundred tokens needs roughly 175 trillion operations, and graphics cards manage it within a few seconds.

But where do the 175 billion correct numbers come from? Nobody picks them by hand as we did for XOR. The network starts with random weights and learns from its mistakes, which the next part explains, gradient descent. How the numbers at the output of the last layer become probabilities of the next word is shown in the part on the softmax function.

Chapters: How ChatGPT and artificial intelligence work: the maths inside, Neural network: what it is and how it computes, Gradient descent: how a neural network learns, Softmax: how numbers become probabilities and what temperature is, Transformer and attention: how a model knows what a word refers to, AI hallucination: why artificial intelligence confidently writes falsehoods