iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🕌

Understanding GPT: Generative Pre-Training for Language Understanding

に公開
2025/04/28

This is an article for understanding GPT.

GPT is a model based on the Transformer architecture. You can find an explanatory article on the Transformer here:

https://zenn.dev/sunbluesome/articles/078ac9a9afca6a

I will summarize it based on the original paper "Improving Language Understanding by Generative Pre-Training".

Key Highlights

  • It can perform various tasks with almost no changes to the model structure. Furthermore, it achieved State-of-the-Art (SOTA) results in 9 out of 12 tasks.

GPT Structure

Transformer Decoder (T-D)

GPT is a model that uses the Transformer Decoder (T-D) proposed in [2], and it has the following structure (shown on the left side of the figure below).


Quoted from [1] Figure 1

Similar to the initial Transformer, encoder-decoder networks were the mainstream for language models at the time. For example, in a translation task from English to German, the English word sequence (input) is fed into the encoder, and the past German word sequence (past output) is fed into the decoder to perform next-token prediction.

Specific Example

For instance, consider a task of translating the English sentence "Hello world" into the German sentence "Hallo Welt."

  1. Encoder:

    • First, the Encoder receives the entire English sentence "Hello world" as input.
    • The Encoder processes this and generates information representing the meaning of the entire sentence (often called a context vector).
  2. Decoder:

    • The Decoder receives the context vector generated by the Encoder.
    • Generating the 1st word: The Decoder takes the context vector and a special token indicating the start of translation (e.g., <start>) as input to predict and generate the first German word "Hallo."
    • Generating the 2nd word: Next, the Decoder takes the context vector and the previously generated German word "Hallo" as input to predict and generate the next German word "Welt."
    • Finishing the translation: Similarly, using the context vector and the German word sequence generated so far "Hallo Welt" as input, it generates a special token indicating the end of the sentence (e.g., <end>), completing the translation.

In other words, "feeding the past German word sequence (past output) into the decoder" means that the Decoder uses the German word sequence it has generated so far to predict the next word. It follows a flow of generating the next word while referring to the previous translation results (past outputs) at each step.

In Decoder-only models like GPT, there is no such division of roles between an Encoder and a Decoder; instead, the input and output are treated as a single sequence for training.

The Transformer Decoder can halve the parameters but only accepts inputs from the decoder side. Therefore, it is trained by creating a sequence that combines the input sequence and the past output sequence. In other words, when there is a sequence conversion mapping like (m^1, \ldots, m^n) \mapsto (y^1, \ldots, y^n), these are concatenated into a single sequence like (u^1, \ldots, u^{n+\eta+1}) = (m^1, \ldots, m^n, \delta, y^1, \ldots, y^\eta) for training. \delta is a delimiter token.

Specific Example

Taking an English-to-German translation task as an example:

  • m = (m^1, ..., m^n) is the input English word sequence (e.g., Hello, world)
  • y = (y^1, ..., y^η) is the output German word sequence (e.g., Hallo, Welt)
  • δ is a delimiter token (e.g., <DELIMITER>)

When performing a translation task (or a similar sequence conversion task) with a Decoder-only model like GPT, the training data is prepared as follows.

Specific Example:

For training data to translate English "Hello world" into German "Hallo Welt":

  1. Input sequence m: (Hello, world)
  2. Output sequence y: (Hallo, Welt)
  3. Delimiter δ: <DELIMITER>
  4. Combined sequence u: (Hello, world, <DELIMITER>, Hallo, Welt)

Learning Process:

The model receives this combined sequence u as a single input. Then, it learns to predict the next word.

  • Seeing Hello, it predicts world
  • Seeing Hello, world, it predicts <DELIMITER>
  • Seeing Hello, world, <DELIMITER>, it predicts Hallo
  • Seeing Hello, world, <DELIMITER>, Hallo, it predicts Welt
  • ... (Continues until it predicts a token like <end> indicating the end of the sentence)

Due to the Masked Self-Attention mechanism of the Transformer Decoder, when predicting each word, it can only refer to the words before it. This allows the model to learn the next German word to output using the input (English) and the portion already output (German).

Inference (Actually performing translation):

  1. Input (Hello, world, <DELIMITER>) to the model.
  2. The model predicts/outputs Hallo as the next word.
  3. Next, input (Hello, world, <DELIMITER>, Hallo) to the model.
  4. The model predicts/outputs Welt as the next word.
  5. Repeat this until a token indicating the end of the sentence is output, then finish the translation.

In this way, instead of explicitly separating input and output like an Encoder-Decoder model, the method described here treats them as one long sequence and realizes sequence conversion tasks within the framework of next-word prediction.

Unsupervised Pre-training

When a set of tokens is given as \mathcal{U} = \left\{u_1, \ldots, u_n\right\}, we consider maximizing the log-likelihood L_1 of a neural network defined by parameters \Theta.

\begin{align} L_1 (\mathcal{U}) = \sum_i \log P(u_i | u_{i-k}, \ldots, u_{i-1};\Theta) \end{align}

Here, k is the context window size.

GPT utilizes the Transformer Decoder structure mentioned earlier* and is defined as follows:

\begin{align} h_0 &= UW_e + W_p \\ h_i &= \text{transformer\_block}(h_{i-1}) \quad \forall i \in [1, n]\\ P(u) &= \text{softmax}(h_nW_e^T) \end{align}

Where \mathcal{U} = (u_{i-k}, \ldots, u_{i-1}) is the context vector of tokens, n is the number of layers, W_e is the token embedding matrix (vectors embedded into the hidden state), and W_p is the position encoding (refer to here for position encoding).

*Note that the transformer block uses multi-headed self-attention.

Supervised Fine-Tuning

After training to maximize Equation (1), we perform supervised learning on the target task.

We prepare a target task dataset (X, y) \in \mathcal{C}, where \left\{x^1, \ldots, x^m\right\} \in X is the input token sequence and y is the label corresponding to the input token sequence. We then perform fine-tuning to maximize the following likelihood function:

\begin{align} L_2 (\mathcal{C}) = \sum_{(x, y)} \log P (y|x^1, \ldots, x^m) \end{align}

In what is called fine-tuning in GPT, only the parameters of the final layer of the pre-trained model are updated. That is, the input token sequence is passed through the pre-trained model to obtain the final transformer block output h_l^m. Then, h_l^m is fed into a linear layer to learn the linear layer parameters W_y that predict y.

\begin{align} P(y|x^1, \ldots, x^m) = \text{softmax}(h_l^m W_y) \end{align}

auxiliary objective

The authors state that performing fine-tuning including an auxiliary objective improves generalization performance and convergence. Specifically, they introduce a new objective function:

\begin{align} L_3(\mathcal{C}) = L_2(\mathcal{C}) + \lambda * L_1(\mathcal{C}) \end{align}

where \lambda is an arbitrary constant.

Task-Specific Input Transformations

For classification tasks, we can simply perform the fine-tuning as described above. However, for tasks like answering questions based on text or evaluating similarity between sentences, we need to adapt the input. This is because the GPT model only supports learning with a single sequence, while sentence similarity tasks require two sequences of input and question-answering tasks require three sequences (text, question, and answers), making them impossible to learn as-is.

In [1], examples for Textual Entailment, Similarity, and Question Answering and Commonsense Reasoning are introduced.


Re-posted: Quoted from [1] Figure 1

There is a schematic diagram of the learning method for each task on the right side of the figure above, so checking it as needed will make the following explanation easier to understand.

Textual Entailment

This is the task of determining whether a premise token p entails a hypothesis token h when both are given. In short, it is a task to determine whether h can be inferred from p. It can be handled by simply connecting p and h with a delimiter token ($).\n

Similarity

This evaluates the similarity between two sentences. Inserting a delimiter token between the sentences is the same as in Textual Entailment. However, since there is no inherent order between the two sentences, two input sequences with different orders are prepared and passed independently to the model to obtain the hidden states. The element-wise sum of the two obtained hidden states is taken and finally passed to a linear layer.

Question Answering and Commonsense Reasoning

This is a task to predict the answer \{a_k\} from a text z and a question q. As before, all elements are concatenated using delimiter tokens () as $[z; q; $; a_k]. Each answer is passed independently to the model, and finally, a softmax is applied to obtain the output distribution for the answers.

GPT Performance

Finally, let's look at the performance of GPT on various tasks.

Natural Language Inference Task Performance


Quoted from [1] Table 2

A performance comparison with the SOTA models of the time was conducted on six natural language inference tasks. The fine-tuned GPT model achieved SOTA in five out of the six tasks.

Question Answering and Commonsense Reasoning Performance

This was the task of obtaining an answer to a question. It achieved SOTA in all tasks.


Quoted from [1] Table 3

Semantic Similarity and Classification Performance

It achieved SOTA in four out of six tasks.


Quoted from [1] Table 4

Summary

  • GPT is a model that modifies the Decoder side of the Transformer.
  • It achieved SOTA on various tasks with almost no changes to the model structure or objective function.

While its power is almost hard to believe, its effectiveness has been sufficiently demonstrated by its successor models, collectively known as GPT-n, which are capable of generating text of nearly the same quality as humans.

References

  1. Radford, A., Narasimhan, K., Salimans, T. & Sutskever, I. (2018). Improving Language Understanding by Generative Pre-Training.
  2. Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L. & Polosukhin, I. (2017). Attention Is All You Need. arXiv. https://doi.org/10.48550/arxiv.1706.03762

Discussion