[ad_1]

## Diving deeply into the working structure of the first version of gigantic GPT-models

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.

[ad_2]

Source link