中文版:LLM基础原理解析
If you want to understand AI, you first need some grasp of how it actually works underneath. Modern large models are built on neural networks and deep learning, or more precisely on multi-layer neural networks.
1. The Basic Mathematical Model of a Neural Network
At the very beginning, a model is made up of a few things:
- How many layers the neural network has
- How many neurons in each layer
- Which activation function to use
Actually, the model already has parameters at this point. Those parameters are usually:
- Randomly initialized
- Initialized according to some rule
So when people say “training a large model”, what they really mean is training the parameters inside the model. This parameter-training process generally runs through the backpropagation algorithm, which I’ll get into in detail later.
Also, the parameters here don’t refer to the neurons. A single neuron usually contains a lot of parameters.
The simplest neuron can be written in the following form
It can also be written as , where represents the set of parameters
Here refers to the activation function, and along with are the model’s parameters.
As you can see, together with the bias parameter at the end form a linear function, while the wrapped around the outside performs a nonlinear transformation.
Of course, a neural network usually has multiple layers of neurons
Suppose the input vector is:
Note that what goes in here is a vector, which is also related, in its mathematical form, to RAG later on.
Suppose this layer has neurons. The whole layer can then be expressed in aggregate as:
Where:
The here is a parameter matrix, the collection of all the parameters in this entire layer.
In the end, an -dimensional input produces an -dimensional output.
Why is the output -dimensional? There’s actually an even simpler way to understand it
Each neuron is a function that operates on the input vector. The parameter here can be understood as a kind of weight, and the parameter is the bias. In the end, the vector is computed down to a single value.
where
neurons give you values, and stacked together they form an -dimensional vector.
2. How Input Enters the Neural Network
I described some of this in the earlier RAG article. First, the text entered by the user needs to be cut into tokens by a tokenizer
For instance, didn’t GPT 5.5 recently become more token-efficient precisely because it changed the way it handles tokens?
2.1 tokenizer
A tokenizer generally consists of two steps:
- Text splitting
For example, given the input:
how are you
The output might become:
"how","are","you"
Of course this is the idealized case. As far as I remember, GPT’s tokenizer is open source, so if you’re interested you can try it out yourself
- Mapping to token_id
["how", "are", "you"] → [101, 57, 302]
2.2 embedding
A token_id itself is nothing but an ID number. You need to look up the corresponding vector in the embedding table, and this number is exactly what you use to do the lookup, to find which row of the table it belongs to
Here represents the vocabulary size, which is the height of the table, and is the vector dimension, which is the width of the table
Of course, a sentence usually has multiple tokens. For example, a sentence is tokenized into 4 token_ids
When multiple sentences are fed in at once, what you look up is a three-dimensional tensor
In real situations, a model usually processes one batch at a time, that is, a group of samples sent into the model together for processing
2.3 Adding positional encoding
As I wrote in that RAG article, a token_id itself carries no positional information, so you need something like a positional encoding to mark the position it sits at
3. How a Neural Network Learns
Roughly, it’s divided into the following steps:
- Input data: such as a piece of text, or an image
- Forward propagation: that is, the model, based on the current parameters, computes the output from the neurons
- Compute the loss: compare the prediction against the correct answer to get the loss
- Backpropagation: compute the effect of each parameter on the loss
- Gradient descent: update the model’s parameters in the direction that makes the loss smaller
We’ve already roughly covered the input data and forward propagation, so let’s first talk about how the loss is computed
3.1 Computing the loss
The loss here is computed through a function:
The notation is as follows:
: the loss function
: the model’s prediction
: the true answer
LLMs generally compute their loss using cross-entropy loss
- logits
First, we all know that a model’s output is generally a sequence of tokens
Such as
token1,token2,token3...
An LLM’s training objective is to predict the next token based on the preceding tokens
The model does not output tokens directly. The model itself has a vocabulary, which to be precise is a token table. At each prediction position, the model scores every token in the entire vocabulary. Suppose the vocabulary size is , then the model outputs
This can also be represented as a vector
Here represents the raw score for the -th token to be the next token. These scores are called logits. They are not probabilities yet; they can be positive or negative, and they don’t necessarily add up to 1
- softmax
What needs to be done next is to turn these raw scores into a set of probabilities
Here each is between 0 and 1
And they add up to 1
If ordinary normalization were used here, such as:
then several problems would arise
-
The values have both positive and negative signs
-
The differences between scores are not very pronounced, so large scores don’t stand out enough
Here we can solve this problem by using the exponential function
First, the result of the exponential function is always positive, and second, the exponential function grows very fast, so differences become quite pronounced
- Cross-entropy loss computation
softmax is responsible for turning logits into probabilities, and cross-entropy loss is responsible for checking how much probability the model gives to the correct answer
If the probability that the model gives to the -th token after softmax is
then the cross-entropy loss is
The reason here is that ** is greater than 0 and less than 1, so is less than 0, hence the need to add a minus sign**
| Probability the model gives the correct token (pk) | Loss (L=-log pk) |
|---|---|
| 1.0 | 0 |
| 0.9 | 0.105 |
| 0.5 | 0.693 |
| 0.1 | 2.303 |
| 0.01 | 4.605 |
| 0.001 | 6.908 |
As you can see
- If the model is very confident and correct, the loss is close to 0
- If the model only gives a 0.1 probability to the correct answer, the loss grows noticeably
- If the model almost doesn’t believe the correct answer, say 0.001, the loss becomes very large
And why use log here? You can see from the table that if the probability of the correct answer goes from 0.9 to 0.8 the loss changes little, but if it goes from 0.1 down to 0.001, the gap grows noticeably larger, making it more sensitive than a simple 1-p
There’s another very important point. We all know that when a model outputs, it outputs a sequence of tokens. If it were 1-p, the probabilities would multiply together and get smaller and smaller, which is very hard to compute
if you take a log, you can turn multiplication into addition
Then the loss for the whole sequence can be written as
Taking the average gives (the loss over the whole sequence is usually averaged):
3.2 Backpropagation
Backpropagation, i.e. Backpropagation, is essentially:
Use the chain rule to work backward from the loss function, computing each parameter’s effect on the loss
We already know from before that a neural network is not a single formula, but a layer of nested functions
Here is at the very front and , that is the loss, is at the very end. If you want to know how should be changed, then you need to know
the effect of on the final loss along this entire chain. This requires the chain rule, and the chain rule is the core of backpropagation
3.2.1 The chain rule
The simplest chain rule is: if
then
In plain language:
If x affects y, and y affects z, then the effect of x on z = the effect of x on y × the effect of y on z
Of course, when I first saw this formula, I had a vague impression of it, but I still forgot what these d’s were, so let me fill that in here
The here is the differential symbol from calculus, and it can also represent an infinitesimally small change
Here is the derivative of with respect to . I remember the high school textbook describing it as the rate at which changes as changes
A more straightforward way of saying it:
For every tiny change in x, roughly how much does y change? You can also think of it as an approximation of the tangent line
However, is normally used for single-variable functions, such as:
There is only one input variable, so we write:
For multi-variable functions we have to switch to a different symbol, which is also something you’ll see often when computing gradients below
If a function depends on many variables at the same time, such as:
at this point, if you want to ask
If only changes and everything else stays fixed, how does change?
After all, we all know that a neural network has more than one parameter, and what we want here is to compute each parameter’s individual effect on the overall loss
So at this point we write:
This is called a partial derivative. By “partial” we mean: look only at the effect of one variable, and temporarily treat the other variables as constants
For example:
. As you can see, depends on both and . If we only look at the effect of on , can be treated as a constant, and the result is
3.2.2 Computing parameter gradients
In the network from before:
If you want to update the parameter at the very front, , you need to know
It represents how the final loss will change if undergoes a very small change
And exerts its influence through a single chain:
By the chain rule, we know:
-> -> …, and from this you can see the way of “reverse” in backpropagation
3.2.3 Gradient descent for updating parameters
We already know from before that each parameter’s effect on the loss function is:
. Next, we can update the parameters based on the gradient. The most basic way of updating parameters is gradient descent:
: the current parameter
: update the left side to become the new value on the right
: the learning rate, controlling how big a step to take each time
: the gradient of the loss with respect to the parameter
-: move in the opposite direction of the gradient, because the gradient is the direction in which the loss increases fastest
So if you want the loss to decrease, you need to move in the opposite direction
New parameter = old parameter - a small step * gradient
The learning rate here is also very important. Without a learning rate, the steps could become too large and shoot straight past the lowest point
Back to the essence of . In fact is essentially a parameter matrix: if the input has 3 numbers and the hidden layer has 4 neurons, then is a 4×3 matrix holding 12 numbers, where each number represents “how much influence a given input item has on a given neuron”. So-called training a model is, at bottom, adjusting those 12 numbers (and all the other parameters in the network) again and again according to the gradient, until the loss is small enough.
At this point, a complete loop is closed:
Input → the parameter matrix performs a linear transformation → the activation function introduces nonlinearity → produce the output → the loss function measures the gap → backpropagation uses the chain rule to compute gradients → gradient descent updates the parameters → feed in the next batch of data …
Large language models look mysterious, but once you take them apart, every layer is doing exactly this one thing. The only difference is that the scale of the matrices has gone from 4×3 to a few thousand by a few thousand, and the parameter count has gone from a dozen or so to hundreds of billions. Scale brings emergence, but the principle hasn’t changed. Once you understand this layer, looking back at terms like “temperature”, “context window”, and “fine-tuning”, they all come back down to matrices and gradients.