2017 was a historical year in machine learning. Researchers from the Google Brain team introduced **Transformer** which rapidly outperformed most of the existing approaches in deep learning. The famous **attention** mechanism became the key component in the future models derived from Transformer. The amazing fact about Transformer’s architecture is its vaste flexibility: it can be efficiently used for a variety of machine learning task types including NLP, image and video processing problems.

The original Transformer can be decomposed into two parts which are called **encoder** and **decoder**. As the name suggests, the goal of the encoder is to encode an input sequence in the form of a vector of numbers — a low-level format that is understood by machines. On the other hand, the decoder takes the encoded sequence and by applying a language modeling task, it generates a new sequence.

Encoders and decoders can be used individually for specific tasks. The two most famous models deriving their parts from the original Transformer are called **BERT** (Bidirectional Encoder Representations from Transformer) consisting of encoder blocks and **GPT** (Generative Pre-Trained Transformer) composed of decoder blocks.

In this article, we will talk about GPT and understand how it works. From the high-level perspective, it is necessary to understand that GPT architecture consists of a set of Transformer blocks as illustrated in the diagram above except for the fact that it does not have any input encoders.

As for most LLMs, GPT’s framework consists of two stages: pre-training and fine-tuning. Let us study how they are organised.

## 1. Pre-training

**Loss function**

As the paper states, “W*e use a standard language modeling objective to maximize the following likelihood*”:

In this formula, at each step, the model outputs the probability distribution of all possible tokens being the next token *i* for the sequence consisting of the last *k* context tokens. Then, the logarithm of the probability for the real token is calculated and used as one of several values in the sum above for the loss function.

The parameter k is called the

context window size.The mentioned loss function is also known as

log-likelihood.

Encoder models (e.g. BERT) predict tokens based on the context from both sides while decoder models (e.g. GPT) only use the previous context, otherwise they would not be able to learn to generate text.

**The intuition behind the loss function**

Since the expression for the log-likelihood might not be easy to comprehend, this section will explain in detail how it works.

As the name suggests, GPT is a generative model indicating that its ultimate goal is to generate a new sequence during inference. To achieve it, during training an input sequence is embedded and split by several substrings of equal size *k*. After that, for each substring, the model is asked to predict the next token by generating the output probability distribution (by using the final softmax layer) built for all vocabulary tokens. Each token in this distribution is mapped to the probability that exactly this token is the true next token in the subsequence.

To make the things more clear, let us look at the example below in which we are given the following string:

We split this string into substrings of length *k = 3*. For each of these substrings, the model outputs a probability distribution for the language modeling task. The predicted distrubitons are shown in the table below:

In each distribution, the probability corresponding to the true token in the sequence is taken (*highlighted in yellow*) and used for loss calculation. The final loss equals the sum of logarithms of true token probabilities.

GPT tries to maximise its loss, thus higher loss values correspond to better algorithm performance.

From the example distributions above, it is clear that high predicted probabilities corresponding to true tokens add up larger values to the loss function demonstrating better performance of the algorithm.

**Subtlety behind the loss function**

We have understood the intuition behind the GPT’s pre-training loss function. Nevertheless, the expression for the log-likelihood was originally derived from another formula and could be much easier to interpret!

Let us assume that the model performs the same language modeling task. However, this time, the loss function will maximize the product of all predicted probabilities. It is a reasonable choice as all of the output predicted probabilities for different subsequences are independent.

Since probability is defined in the range [0, 1], this loss function will also take values in that range. The highest value of 1 indicates that the model with 100% confidence predicted all the corrected tokens, thus it can fully restore the whole sequence. Therefore,

Product of probabilities as the loss function for a language modeling task, maximizes the probability of correctly restoring the whole sequence(-s).

*If this loss function is so simple and seems to have such a nice interpretation, why it is not used in GPT and other LLMs*? The problem comes up with computation limits:

- In the formula, a set of probabilities is multiplied. The values they represent are usually very low and close to 0, especially when during the beginning of the pre-training step when the algoroithm has not learned anything yet, thus assigning random probabilities to its tokens.
- In real life, models are trained in batches and not on single examples. This means that the total number of probabilities in the loss expression can be very high.

As a consequence, a lot of tiny values are multiplied. Unfortunately, computer machines with their floating-point arithmetics are not good enough to precisely compute such expressions. That is why the loss function is slightly transformed by inserting a logarithm behind the whole product. The reasoning behind doing it is two useful logarithm properties:

**Logarithm is monotonic**. This means that higher loss will still correspond to better performance and lower loss will correspond to worse performance. Therefore, maximizing*L*or*log(L)*does not require modifications in the algorithm.

**The logarithm of a product is equal to the sum of the logarithms of its factors, i.e. log(ab) = log(a) + log(b)**. This rule can be used to decompose the product of probabilities into the sum of logarithms:

We can notice that just by introducing the logarithmic transformation we have obtained the same formula used for the original loss function in GPT! Given that and the above observations, we can conclude an important fact:

The log-likelihood loss function in GPT maximizes the logarithm of the probability of correctly predicting all the tokens in the input sequence.

**Text generation**

Once GPT is pre-trained, it can already be used for text generation. GPT is an **autoregressive** model meaning that it uses previously predicted tokens as input for prediction of next tokens.

On each iteration, GPT takes an initial sequence and predicts the next most probable token for it. After that, the sequence and the predicted token are concatenated and passed as input to again predict the next token, etc. The process lasts until the [end] token is predicted or the maximum input size is reached.

## 2. Fine-tuning

After pre-training, GPT can capture linguistic knowledge of input sequences. However, to make it better perform on downstream tasks, it needs to be fine-tuned on a *supervised problem*.

For fine-tuning, GPT accepts a labelled dataset where each example contains an input sequence *x* with a corresponding label *y* which needs to be predicted. Every example is passed through the model which outputs their hidden representations *h* on the last layer. The resulting vectors are then passed to an added linear layer with learnable parameters *W* and then through the softmax layer.

The loss function used for fine-tuning is very similar to the one mentioned in the pre-training phase but this time, it evaluates the probability of observing the target value *y* instead of predicting the next token. Ultimately, the evaluation is done for several examples in the batch for which the log-likelihood is then calculated.

Additionally, the authors of the paper found it useful to include an auxiliary objective used for pre-training in the fine-tuning loss function as well. According to them, it:

- improves the model’s generalization;
- accelerates convergence.

Finally, the fine-tuning loss function takes the following form (α is a weight):

There exist a lot of approaches in NLP for fine-tuning a model. Some of them require changes in the model’s architecture. The obvious downside of this methodology is that it becomes much harder to use *transfer learning*. Furthermore, such a technique also requires a lot of customizations to be made for the model which is not practical at all.

On the other hand, **GPT uses a traversal-style approach: for different downstream tasks, GPT does not require changes in its architecture but only in the input format**. The original paper demonstrates visualised examples of input formats accepted by GPT on various downstream problems. Let us separately go through them.

## Classification

This is the simplest downstream task. The input sequence is wrapped with *[start]* and *[end]* tokens (which are trainable) and then passed to GPT.

## Textual entailment

**Textual entailment** or **natural language inference (NLI) **is a problem of determining whether the first sentence (*premise*) is logically followed by the second (*hypothesis*) or not. For modeling that task, premise and hypothesis are concatenated and separated by a delimiter token ($).

## Semantic similarity

The goal of similarity tasks is to understand how semantically close a pair of sentences are to each other. Normally, compared pairs sentences do not have any order. Taking that into account, the authors propose concatenating pairs of sentences in both possible orders and feeding the resulting sequences to GPT. The both hidden output Transformer layers are then added element-wise and passed to the final linear layer.

## Question answering & Multiple choice answering

Multiple choice answering is a task of correctly choosing one or several answers to a given question based on the provided context information.

For GPT, each possible answer is concatenated with the context and the question. All the concatenated strings are then independently passed to Transformer whose outputs from the Linear layer are then aggregated and final predictions are chosen based on the resulting answer probability distribution.

GPT is pre-trained on the BookCorpus dataset containing 7k books. This dataset was chosen on purpose since it mostly consists of long stretches of text allowing the model to better capture language information on a long distance. Speaking of architecture and training details, the model has the following parameters:

*Number of Transformer blocks*: 12*Embedding size*: 768*Number of attention heads*: 12*FFN hidden state size*: 3072*Optimizator*: Adam (learning rate is set to 2.5e-4)*Activation function*: GELU*Byte-pair encoding*with a vocabulary size of 40k is used*Total number of parameters*: 120M

Finally, GPT is pre-trained on 100 epochs tokens with a batch size of 64 on continuous sequences of 512 tokens.

Most of hyperparameters used for fine-tuning are the same as those used during pre-training. Nevertheless, for fine-tuning, the learning rate is decreased to 6.25e-5 with the batch size set to 32. In most cases, 3 fine-tuning epochs were enough for the model to produce strong performance.

Byte-pair encodinghelps deal with unknown tokens: it iteratively constructs vocabulary on a subword level meaning that any unknown token can be then split into a combination of learned subword representations.

Combination of the power of Transformer blocks and elegant architecture design, GPT has become one of the most fundamental models in machine learning. It has established 9 out of 12 new state-of-the-art results on top benchmarks and has become a crucial foundation for its future gigantic successors: GPT-2, GPT-3, GPT-4, ChatGPT, etc.

*All images are by the author unless noted otherwise*