Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Position embedding

In the previous chapter, we built a runnable basic skeleton of the GPT model, and also pointed out its problems. In this chapter, we start to solve the first one: there is no position information.

The embedding layer can currently only tell the model what each token is, not where each token is. For the model, “the mouse loves rice” and “rice loves the mouse” are the same sentence: wherever a token appears, the same token always gets exactly the same word vector.

But word order is the soul of language. The same word at the beginning or at the end of a sentence can mean something completely different. Compare: an RNN processes tokens one by one in order, so position information is naturally contained in the processing order and needs no extra encoding. A GPT model, in contrast, processes all the tokens in parallel in one go, which is efficient, but at the cost of losing the word order. So we must add the word order information to the encoding explicitly.

Position encoding

How do we represent word order? The most direct idea is to give each position a number in turn: the 1st token is 1, the 2nd token is 2, and so on. We call this position encoding.

Does it look familiar? Quite like the index encoding of tokens? The difference is that the index encoding is the position of a token in the vocabulary, while the position encoding is the position of a token in the text.

Position embedding

Obviously, we can borrow the idea of token embedding: if we can replace the index encoding with a trainable word vector, why not the position encoding?

Position embedding replaces the position encoding with a learnable position vector. Providing both the word vector and the position vector of each token to the GPT model solves the problem that a GPT model naturally ignores word order.

Dataset

Character-encoding dataset

Activation functions

Gaussian error linear unit (GELU)

Loss function

GPT cross-entropy loss function

Model

GPT embedding layer

We add the position embedding positional_embedding to the GPT embedding layer, forming the combination “token embedding + position embedding”:

  • embedding (token embedding): looks up the table with the index encodings of the tokens and produces word vectors, with the shape (batch size, context size, embedding size);

  • positional_embedding (position embedding): looks up the table with the position encodings of the tokens and produces position vectors, with the shape (context size, embedding size).

The embedding layer needs a new initialization parameter: the context size context_size, which determines the size of the weight matrix of the position embedding.

The final output is “word vector + position vector”.

Note that this is an addition of two tensors. It uses the tensor addition that we extended in our training framework, which keeps the computational graph and backpropagation going, instead of simply adding the numbers of two vectors together.

Also, the word vector (3 dimensions) and the position vector (2 dimensions) have different shapes, yet they can be added directly. This uses the broadcasting mechanism supported by the tensor: the position vector is automatically expanded to the same dimensions as the word vector, and then they are added.

GPT basic modules with position embedding: the word vector and the position vector are added together before entering the Transformer block
Figure: GPT basic modules with position embedding

GPT feed-forward network layer

GPT Transformer block

GPT output layer

GPT model

Creating the GPT model also needs one extra initialization parameter: the context size context_size, used to create the embedding layer.

GPT trainer

Hyperparameters

Learning rate

Batch size

Number of epochs

Context size

Embedding size

Number of Transformer blocks

Modeling

When creating the model, we need to pass CONTEXT_SIZE in as an additional initialization parameter.

Training

Evaluation

prediction:	1742 steps, each (4, 32, 65)
loss:	2.462666325331008

Inference

ROMEO:
PELUCENone iat werachald l d:
ANMyerther, itik he
TIO:
Ife.
DUKayo in, y dld s m f s pllorislds hine try be ou 'te noungeltwiomavear whe
Opld migrarthemaxhayorthe t; llleakash lu aru g d, harenout watllllicinchiowe f s my, boive
I he y a fod, gheltl I her

In this chapter, through position embedding, we give the model “word vector + position vector”, so that it can tell both “what it is” and “where it is”.

With this, we have completed the input representation part of the GPT model.

But we cannot see obvious progress in the inference result, because a more fundamental problem remains unsolved: tokens “do not talk to each other”. The prediction at each position depends only on the current token and its position, and the model has no idea what else is in the context.

In the next chapter, we will introduce the most central mechanism of the GPT model, self-attention, which lets tokens truly “see each other”.

Exercises

  1. Try to count the parameters of the GPT model, including the weight matrices of the token embedding and the position embedding, and the weights and biases of the linear layers in the other modules. Which hyperparameters is the count mainly related to?