Exposé - Transformers - Attention Is All You Need
written by Christoph Geske
Table of Contents
Introduction
1.1 The Impact of Transformers on the Field of Conversational AI
Overview of the Transformer Architecture
Word Representations and Embeddings
3.1 Tokenization
The Transformer Architecture
4.1 Positional Encoding
4.2 Self-Attention Mechanism
4.3 Multi-Head Attention
4.3.1 Cross Attention
4.4 Feed-Forward Neural Networks (Multi-Layer Perceptron Blocks)
4.4 Encoder-Decoder Structure
4.4.1 Encoder
4.4.2 Decoder
4.5 Masked Self-Attention
Training
Inference
Model Zoo
7.1 Decoder-Only Models
7.2 Encoder-Only Models
7.3 Multi-Modal Transformers
Conclusion
Introduction
Teaching computers to understand language is an active field in computer science that began long before the development of what we know today as large language models (LLMs) and ChatGPT. Practically all current state-of-the-art LLMs and chatbots run on an architecture called transformers.
Transformers were first described as an architecture in 2017 in the paper "Attention is All You Need" by researchers at Google [1, Vaswani 2017]. This architecture revolutionized the field, not by being the first to show how one can teach computers to capture the meaning of text, but by enabling the processing of large amounts of text data efficiently and in parallel using graphics processing units (GPUs).
A key property in the transformer giving the paper its catchy name is the attention mechanism. Attention allows the system to capture the relationships words have to their neighbours even over long distances. Previous architectures like Long Short-Term Memory (LSTM) networks, see Fig.1, already showed promise in translating text and were able to remember the dependencies of words on each other, but not over such long distances as transformers because they used a sequential processing scheme where one block only received information from the previous block. Because of their inability to be run in a highly parallelized way, they could not keep up with the superior transformer architecture, which nowadays is so well optimised that it is said to be trained on all text data available online.
Figure 1: History of different transformer architectures. [2, Google]
Research into enhancing architectures like LSTMs (e.g., xLSTMs) continues to this day with researchers incorporating lessons learned from transformers and their many variants, though transformers remain the dominant architecture.
1.1 The Impact of Transformers on the Field of Conversational AI
With transformers being relatively new, the field of conversational AI is just beginning to explore all the possibilities that transformers trained on ever-growing datasets bring to the field. Before the use of deep learning methods, NLP relied heavily on rule-based approaches and semantic parsing, which converts a sentence into a machine-understandable format.
[3, Stanford] While these approaches allow for a much more transparent analysis, they struggle to capture the complexity, ambiguity, and nuances of natural languages.
Assistant
User
Self-attention has a quadratic computational complexity wher in my expose could i put this information
Assistant
User
Backpropagation and Optimization: The learning is done through a process called backpropagation. The model makes predictions based on the current embeddings, and then the error between these predictions and the actual values is calculated. This error is then used to adjust the embeddings to bring the predictions closer to the actual values.
where to put this in my text
Assistant
User
During training the model predicts the best embedding and an error is calculated
what is the model predicting and what its it compared against?
Assistant
User
but in the original tanfformer what is used there by the model what ois it predicting
Assistant
In fields like image processing and protein folding, it became apparent that using large amounts of unstructured but labelled data and training a neural network on them led to significant improvements compared to hand-crafted rule-based systems, starting with the successes of ImageNet and its ability to label images better than state-of-the-art models at that time.
Something similar can be observed in the field of language understanding, where transformers' performance improves by scaling the amount of high-quality training data without the need for rule-based additions, even though the incorporation of rule-based methods can be used to improve the results and explainability. The current transformer architecture acts much like a black box when it comes to interpreting the process that leads to an output prediction because of the high number of parameters and dimensions it is hard to interpret the reasoning.
[3, Stanford] Explainable AI is its own branch becoming increasingly important as transformers get used in more and more areas.
2. Overview of the Transformer Architecture
In this chapter, all relevant parts of the transformer architecture will be listed. Some of them play an especially important role for the topic of conversational AI, and we will be looking at them in some more detail.
Figure 2: The transformer architecture. [1 Vaswani 2017]
Inputs: The input text is transformed into tokens often 1 token is smaller than a word.
Input Embedding: Tokens are converted into vectors containing the meaning of the words in the input text.
Positional Encoding: Since transformers don’t have a built-in representation of order as found in the sequence to sequence models like RNNs. Positional encodings are baked into the embedding using a clever combination of sine and cosine waves.
Information about the position of each token in the sequence is stored in a positional encoding matrix. This allows the model to capture the information about the order the tokens appear in the input text.
Encoder: The decoder is a part of the transformer that is able to capture the meaning and relationship of words given to it in the input sequence. The idea of encoding words into vectors is not unique to transformers and can be implemented in different architectures like RNNs and LSTMs. [2, Google].
The encoder consists of multiple layers and parts:
Multi-Head Self-Attention Mechanism: Allows the model to focus on different parts of the input sequence simultaneously, capturing various relationships and dependencies.
Feed-Forward Neural Network (FFN): Transformation performed on the vector for each word after each attention layer. Helps capture additional meanings beside the relationship information captured by the attention mechanism. In this step the words don't use the neighbouring words directly and only use the single vector each word was assigned to. A ReLU activation function is used to put negative values to 0 and keep only the positive values. [4, 3Blue1Brown]
Add & Norm (Addition and Normalization): Helps stabilise and speed up training.
Decoder: The decoder uses many parts in common with the encoder. It uses some of the outputs of the encoder together with a start token and returns a probability prediction for the next possible token. Some of its parts differing from the encoder are:
Encoder-Decoder Attention / Cross Attention: Allows the decoder to also focus on relevant parts of the input sequence given only to the encoder, by using the encodings generated by the encoder.
Masked-Multi-Head-Attention Mechanism: Masking hides words that appear after the word that the model needs to predict. Doing this helps the model not to use these words in calculating the probabilities for the next token. Only words before the token that should be predicted are used by the decoder for making a prediction.
Output Embedding and Softmax Layer: Converts the decoder’s output into a probability for each word in the vocabulary, allowing the model to generate the most likely next token in the sequence.
Word Representations and Embeddings
To represent words, we use a technique called word embeddings. The brilliant idea here is that words are not simply represented by symbols but rather as vectors. By using a high-dimensional vector space, the meaning and relations of words towards each other can be represented and computation can be performed on these representations.
The higher the dimension of the vector, the higher is the number of representations and nuanced meanings the word can take on. It turns the discrete representation of a word into a continuous one via the vector. [3 Stanford]
Words are first given a unique number and then they are transformed into a vector (see Figure 3). This vector can be of any size and dimension and set randomly at the start. It then points to a point in the vector space with dimensions of the vector size. Larger dimensions give each word more degrees of freedom in the vector space, whereby each dimension could encode some meaning or relationship to other words. During training, the position in the vector space becomes meaningful.
Figure 3: Translation of words into unique ids and then to high dimensional vectors. [5, Jamil]
A famous example to explain how this representation as vectors can be used to capture the meaning and relationships with other words is the following: The words king, man, queen, and woman get represented as vectors. Then the following vector operation can be performed: king - man + woman ≈ queen. The match is very close but not exact since the word queen also finds use in different contexts that are not exactly the same as female ruler (see Figure 4).
Figure 4: Visual example of how vector calculations are possible on word vectors via addition and subtraction. [6, 3Blue1Brown]
Using these vectors, we can measure their relatedness towards one another using the dot product. The dot product returns a negative number when they point in opposite directions and a positive number when they point in the same direction. In other words, not just the meaning of words is captured by the vectors, but it also becomes possible to make vector calculations on these representations to generate meaningful insights into the relationships between the words.
These static embeddings allow machines to discern contextual relationships. However, transformers go beyond this by using contextual embeddings, which adjust a word's vector based on its context in the sentence. The transformer takes dot products of whole matrices instead of single vectors, but matrices are just collections of vectors, so it is basically the same process.
3.1 Tokenization
Transformers tokenize words into smaller chunks called tokens, providing a uniform size for easier processing. The embeddings, along with positional information about where each token appears in a sentence, are used to understand the meaning behind words. In the following, words and tokens will be used without distinguishing between them.
But not just words can be tokenized; all kinds of data like images or speech can also be represented as tokens, and next token prediction can be performed on them by the transformer. LLMs are special transformers that process and predict only word tokens. When image or sound tokens are added in addition to text tokens, the term multimodal LLMs is used.
Since transformers work on sequences of data, tokenizing 2D objects is already introducing inefficiencies, which grow when working with spatial data like objects in 3D, which hints at possible limits of the transformer architecture to be used on spatial data [11, Fei-Fei Li]. However, for images, systems already exist that accept text and images with the idea that the input of an image triggers similar activations as words describing the images, allowing the model to understand the combined meaning of images and text.
4. The Transformer Architecture
The original architecture is divided into two main components: the encoder and the decoder, both use the same self-attention mechanism and have multiple layers in common (See figure 5). In the following, we take a closer look at some of these parts making up the transformer.
Figure 5: Encoder and decoder blocks in the transformer architecture. [13, The AI Hacker]
4.1 Positional Encoding
When given an input sequence, each word in the sequence can be given a unique position label. In the original transformer paper, a technique using cosine and sine was used to generate such a unique label. When the label is generated, it can be added to the original embedding vector simply by addition to end up with the embedding that is passed on to the transformer (see Fig. 6).
Figure 6. Generation of the encoder input matrics using the addition of word ambedding and positional embedding. [5, Jamil]
To make the positional encoding using cosine and sine clear, consider a simple sentence with just 3 words and 1 dimension. Imagine the sentence is "I love cats" with "I" in position 1, "love" in position 2, and "cats" in position 3. If we had just 1 dimension, the vector would look like this [1, 2, 3], and we could put one sine and cosine wave in this 1 dimension (See Figure 7).
Figure. 7: One dimensional positional encoding using sine and cosine waves of the same frequency.
For each position, we get 2 new numbers which we use as a description of the position. In Fig. 7 at position 1, we have the values 5.1 and 8.0. These are now simply added to the vector representing the word "I" in the sentence. [1 + (5.1 + 8.0), 2, 3]. This is also done for the other two words, baking the positional information into the vectors that describe each word in the sentence.
By allowing for a second dimension [[1, _], [2, _], [3, _]] which is filled with a random number in this example, we get an embedding for the words that looks like this: [[1, 22], [2, 41], [3, 58]]. We again add the values the sine and cosine waves provide at this location (See Fig. 8). For position 1, it would look like this: [[1 + (5.1 + 8.0), 22 + (1.2 + (-1))], [2, 41], [3, 58]]. The same is done for positions 2 and 3.
Figure. 8: Two dimensional positional encoding using sine and cosine waves of different frequency for the different dimensions.
Because sine and cosine repeat, it is possible that the same value pair is observed multiple times. Therefore, we allow each dimension to use a different frequency for the sine and cosine waves, making the combinations unique when enough frequencies are used. The different frequencies also allow the model to capture positional information of words close together by using high frequencies and words far apart by using low frequencies, resulting in stretched-out sine and cosine waves (see Fig. 9).
Figure 9: Different frequencies allow the model to capture positional information for words close together or far apart. [14, BrainDrain]
Using these cosine and sine waves has the following advantages:
Uniqueness of position encoding. More frequencies lead to very unique combinations that can describe a position in a unique way.
Capturing different patterns in the sentence structure by using low and high frequencies
Works with very long sequences since sine and cosine are infinite
Allows to estimate the distance of two token positions when they are located on one period of a wave
Deterministic property. The position is defined by the sine and cosine waves which are both clearly defined mathematically
Unambiguous. Each position has only one value (combination of values) [14, BrainDrain]
4.2 Self-Attention Mechanism
Self-attention allows the model to understand how each word in a sentence relates to every other word. It allows the model to focus its attention on one aspect in the input text. Using multiple self-attention blocks allows the model to pay attention to not just one but multiple aspects of the input text. [3, Stanford].
In the training step, weights are computed that allow the model to learn how much attention is given to each word in the input at each position. Attention weights are computed using dot products between vectors represented in the Query (Q), Key (K), and Value (V) matrices and a final softmax to end up with values that can be interpreted as probabilities. [3, Stanford]
There are geometrical ways to interpret Query, Key, and Value, but here the following higher-level interpretation is used:
Q stores the question “what is the information I am looking for” each word can ask other words or to itself. For example, “Am I a subject? Do I appear beside the word ‘the’ or am I often found at the beginning of a sentence?” [3, Stanford]
K can be expressed as the question “What are the things that I have?” For example a book has a summary on its back and the key/summary helps find the information quickly. [3, Stanford]
V represents “What is it that I communicate?”. When accessing the book the detailed information that is returned is the value. [3, Stanford]
The attention for all the words is then calculated using the following formula:
Q, K, and V are matrices that contain all vectors given to each word and their positions. They get updated during training to capture the relationships and meanings of the words towards each other. When plugged into the formula, they return the attention matrix, which contains the information of which words are most important to each other. The size of Q, K, and V can be set freely, and it determines how much information the model can capture from the training data. ( d_k ) is the size or dimension of each word vector, which was defined at the very beginning of the word embedding.
The softmax function results in values between 0 and 1, so we avoid very large or negative numbers and can calculate probabilities from it.
Softmax then helps to choose from all the retrieved information which one is the most relevant by increasing the values for good matches and pushing values for less ideal matches down to zero. The outputs can also be interpreted as probabilities, which is useful when the question is which next word has the highest probability to be chosen as the next word in the sequence.
Since each word is also related to itself, the attention matrix has most entries on its diagonal since most words in a sentence are not related to all the others and might only be related to themselves (see Fig. 10).
Figure 10: Attention matrix with highest relatedness between words themself on the diagonal. [5, Jamil]
Other than previous architectures, the distance between the words plays no role for a transformer if it gets a reasonable amount of compute power, unlike LSTMs and RNNs which were limited by their sequential nature. Attention in transformers allows the capture of long-range dependencies between all words (see Fig. 11). Furthermore, self-attention is able to run in parallel and process all words simultaneously. This allows transformers to run highly parallelized and with fewer training steps, speeding up training [3, Stanford].
Figure 11. Attention can focus on words everywhere in the sequence. [13, The AI Hacker]
However, even in transformers, allowing for longer-range dependencies comes at a cost. The more relations with other words are allowed and the more positions exist, the more entries are required in the different embedding matrices. When the vocabulary grows, more relations between words are possible and need to be stored, and when the context length grows, more possibilities of how words are positioned relative to each other can be generated and also need to be stored. Changes in vocabulary and context length, therefore, result in a non-linear increase of compute complexity, limiting the size of the context window even in the latest LLMs.
4.3 Multi-Head Attention
The attention mechanism determines the importance of other words towards a given word and can be applied multiple times independently of each other to focus on different parts of the input sequence. [12, SuperDataScienceWithJonKrohn]. The different runs can then be combined again via a concatenation operation (see Fig. 12).
This process is called the multi-head attention mechanism and allows for the capture of more diverse patterns in the input. The context for a word can look very different, and allowing for multiple heads also allows for multiple diverse contexts.
Figure 12: Multi Head Attention with their outputs getting concatenated. [1, Vaswani]
In Fig. 13, each head is labelled by a different color, and when looking at only one word and asking each of the heads which words are most related to it, different colors indicate that each head focuses on different words and marks them as most important.
Figure 13: Multiple heads and their prediction for the most relevant words related to “making”. For each head a different colour is used. [5, Jamil]
4.3.1 Cross Attention
In the encoder-decoder architecture, the encoder and decoder have different inputs, and for these different inputs, both calculate distinct Q, V, and K matrices.
With both encoder and decoder using similar but different Q, K, and V matrices, it becomes possible to recombine the matrices from the encoder and decoder, which is known as cross attention because information crosses from the encoder into the decoder, helping the decoder make a prediction using the learned weights from the encoder (see Fig. 14).
Cross attention allows the encoder to send information to the decoder. K and V come from the encoder, and Q is used from the decoder.
Figure 15: Source: Showing the flow of information required for a translation task from Encoder (left) to Decoder (right) [3, Stanford]
Via the connection to the encoder, the decoder knows what needs to be translated in this example (Fig. 15). Otherwise, it would need to make a prediction based on the knowledge stored in its own Q, V, and K matrices without knowing exactly what to translate. The decoder without the encoder would still be able to make a prediction for the most likely next word, but that would not be a translation anymore but a basic next token prediction.
4.4 Feed-Forward Neural Networks (Multi-Layer Perceptron Blocks)
After the attention mechanism, each word goes through its own feed-forward neural network. This transformation is performed separately for each word vector after each attention step. This step helps capture additional information not captured by the attention mechanism alone.
In the feed-forward step, the words don’t use the neighboring words directly and only work with the single vector which describes each word and its position and relationships. An intuitive way to understand what information is learned in this feed-forward network is not so easy, but one can think of it as if each token vector is asked questions about its role in a sentence like “Is the word related to an animal?” and if it is, this weight is strengthened, giving the network the ability to learn what different token vectors represent and stand for (see Fig. 16).[6, 3Blue1Brown]
Figure 16: Given one token vector the feed forward network learns what meaning this particular token vector carries. Source: [6, 3Blue1Brown]
4.4 Encoder-Decoder Structure
Both the encoder and decoder share several common components and mechanisms, such as input embeddings that convert tokens into vectors, the addition of positional encodings to these embeddings, performing self-attention to capture relationships between words within the input sequence, applying feed-forward transformations to further process the embeddings, and using normalisation after certain layers.
4.4.1 Encoder
The encoder processes the input sentence, using self-attention to capture the relationships between words. Its main task is to encode the input data into a set of vectors that represent the relationships and meanings of the words. These encoded representations are then made available to the decoder for generating predictions.
4.4.2 Decoder
The decoder is responsible for generating the next token in the output sequence. It receives as input a start token and predicts the most likely next token. For each prediction, the decoder uses parts of the encoder's output (specifically the Value (V) and Key (K) matrices) and the tokens generated so far. This process is known as inference and involves generating one word at a time in a loop until an end-of-sequence token is produced.
4.5 Masked Self-Attention
Masked self-attention is used by the decoder to prevent it from looking at future tokens during the generation process. This is also called an autoregressive property, meaning each token is generated one at a time.
The decoder uses this method to only look back at the tokens already generated and not at future tokens. This guarantees that the prediction is autoregressive, meaning only one token is generated at a time and not all in parallel. After each new prediction, the newly predicted token as well as all previous tokens are considered before performing the next token prediction.
Fig. 17 shows how masking can be used to hide all upcoming words.
Figure 17. Masking used by the decoder to prevent itself from looking ahead during the prediction step, ensuring that the prediction is auto regressive one token after the other. [6, 3Blue1Brown]
In combination with cross-attention, the decoder is allowed to focus on relevant parts of the encoder's output despite masking, preventing the decoder from looking ahead in the initial step. This connection to the encoder is needed to perform translation tasks.
5. Training
Transformers are trained to predict the next word in a sentence or translate text sequences. The training process enables the model to learn relationships between words, storing this information within its network weights.
The following learning processes can be observed:
Attention Mechanism: The model learns to focus on relevant parts in the input to make a prediction for the most likely next word.
Feed-Forward Blocks: Help the model learn what the token vectors represent
Positional Encodings: The network learns the position patterns using sine and cosine.
What these mechanisms learn was already covered in the corresponding chapters. The attention mechanism learns connections between words, positional embeddings are learned only once and then applied in the inference phase, and feed-forward networks learn what each token vector represents.
6. Inference
Inference is the phase where the trained transformer model generates the next token based on a never-before-seen input sequence.
At first, the input is processed. The text is tokenized, then an embedding is created for the token input, and positional encodings are added to the vectors. The embeddings and learned parameters are used to generate the Q, V, and K matrices for the given input sequence. Q, V, and K are used by the attention mechanism to predict the scores for each token to be the next one, and the token with the highest likelihood can be chosen to be appended to the sequence the decoder started with.
Figure 18 shows how this inference step would look like for the prediction of the next token in a translation task. It starts with the encoder calculating its Q, K, and V matrices and offering them to the decoder to use. The decoder, on the other hand, starts with a single “Start of Sequence” token. Via cross-attention, the information about what to translate is combined with the information provided by the start token to come up with the likely next word, which is Ti as the Italian word for "I" and the first word in the translation.
Figure 18. Encoder generates the Q,K and V matrices and provides K and V to the decoder which uses them to make a prediction of the first word in the translation. [5, Jamil]
In the following steps, the Q, K, and V matrices of the encoder do not need to be recalculated and can be reused (see Fig. 19).
Figure 19: Prediction of the second word in the translation by the decoder reusing the already calculated matrices from the encoder. [5, Jamil]
Model Zoo
7.1 Decoder-Only Models
The most famous transformer model might not be the encoder-decoder model presented in this exposé, but instead its Decoder-Only successors, the GPT (Generative Pre-trained Transformer).
The Decoder-only model is very similar to the original transformer, simply removing the encoder and the parts required for the cross-attention in the original transformer (See Fig.20).
Instead of having both an encoder and decoder with the decoder having access to the encoder weights, a decoder-only architecture only has to train the decoder, reducing its training time and the required compute resources.
Figure 20: From original transformer to decoder only architecture [7, Andrej Karpathy]
The training of the decoder is done on data created by masking, as was shown in Fig. 17. Training data can be generated by masking all upcoming words of a sentence and training the model to predict the next tokens. Since the next tokens are known and only synthetically occluded, the true labels are known, and the loss function can be calculated and used to do backpropagation and weight tuning. [8, Hoorn]
7.2 Encoder-Only Models
As the name suggests, here only the encoder is trained and used. A prominent example of an Encoder-only model is BERT (Bidirectional Encoder Representations from Transformers).
Since the encoder has access to the complete input text during training, it understands the structure of text very well. It is therefore used in tasks like sentiment analysis, where the whole input text can be searched for clues about the emotional tone of the sentence. An encoder-only model can also be used to spot irregular patterns in the input sequence, like reversed sentence order in a paragraph.[9, Raschka] Named Entity Recognition (NER) is another common problem in the NLP field where the model needs to find important information/entities in a text like names, nouns, verbs, etc.
7.3 Multi-Modal Transformers
In previous chapters, it was mentioned that text is only one of the modalities one can give to a transformer. Transformers which are trained on multiple modalities can be used for very different tasks, like those required in visual question answering (VQA). This could be that an image is given to the transformer model in combination with a question prompt to describe the image, and the model is tasked to return an answer requiring a deeper understanding of what is shown in the image and the ability to describe it using written language.[10, Sahu]
Another multi model transformer especially relevant for conversational AI are transformers that are able to accept text and audio data and are able to not only transcribe text to speech but also to capture the meaning of tone, pitch and emotional tone in a speech example and show the ability to describe it using text.
Another multi-modal transformer especially relevant for conversational AI are transformers that can accept text and audio data and are able to not only transcribe text to speech but also to capture the meaning of tone, pitch, and emotional tone in a speech example and show the ability to describe it using text.
8. Conclusion
In this exposé, the workings of the original transformer model were put at the center of focus. The goal was to gain a deeper understanding of the underlying technology. Since this technology is still powering so many innovations happening in the field of AI in general and conversational AI in particular, having a deeper understanding helps to understand the current trends and possible future directions this field can move in.
Many topics like fine-tuning, prompt engineering, or quantization, as well as all the diverse use cases transformer-based models can be deployed for, were not covered. But the amount of subjects one could cover underlines the importance and relevance of the ideas covered here for the field of conversational AI.
Sources
Vaswani, A., Shazeer, N., Parmar, N., et al. (2017). "Attention is All You Need." Google. Link
Google. Encoder-decoder architecture: Overview. YouTube Video
Stanford University. Semantic Parsing Presentation. YouTube Video
3Blue1Brown. Deep Learning Explained. YouTube Video
Umar Jamil. Transformer From Scratch Notes. GitHub. Link
3Blue1Brown. But what is a GPT? Visual intro to transformers | Chapter 5, Deep Learning. YouTube Video
Andrej Karpathy. Stanford Lecture on Transformers. YouTube Video
Robin van Hoorn. How does the (decoder-only) transformer architecture work? StackOverflow. Link
Sebastian Raschka, PhD. Understanding Encoder and Decoder LLMs. sebastianraschka.com
Tezan Sahu. Visual Question Answering with Multimodal Transformers. Medium. Link
Fei-Fei Li. The Future of AI is Here: Unveiling the Next Frontier of AI. YouTube Video
SuperDataScienceWithJonKrohn. How Decoder-Only Transformers (like GPT) Work. YouTube Video
The AI Hacker, Illustrated guide to transformers https://youtu.be/4Bdc55j80l8?si=LxEEXiVFZwyyMtg2
BrainDrain How positional encoding works in transformers? https://youtu.be/T3OT8kqoqjc?si=07XhaPyIfJ6tfGS_
What is this expose getting wrong or are all facts correct and well explained? if something is miss explained or wrongg point it out and help me fix it. Alos i would like to add a chapter about pre training and finetuning how large modern llms are trained on lots of data and how this is used in the area of conversational Ai to adjust large models to special tasks
The exposé titled "Transformers - Attention Is All You Need" by Christoph Geske provides a thorough overview of the transformer architecture, focusing on its relevance in conversational AI. However, there are a few areas that could be improved for accuracy, clarity, and depth, along with suggestions for adding chapters on pre-training and fine-tuning. Below are observations on what might require correction and how to enhance the exposé:
Corrections and Clarifications:
Distinguishing between Attention and Self-Attention:
The term "attention mechanism" is used interchangeably with "self-attention" at times, which may confuse readers. It would be helpful to clarify that "self-attention" is a specific type of attention where the model attends to its own input, while "attention" can refer to different types, e.g., cross-attention used in encoder-decoder models.
Positional Encoding:
When explaining positional encoding, it should be made clearer how these encodings actually help the transformer attend to the order of words in input sequences. You might want to add that positional embeddings are crucial because transformers process words concurrently, unlike RNNs that inherently consider sequence order.
Training Process:
The exposé mentions that the models learn their parameters during the training process but could benefit from additional detail on the optimization algorithms used (e.g., Adam optimizer, learning rate schedules) and the role of loss functions (e.g., cross-entropy loss).
Masked Self-Attention Clarification:
The description of masked self-attention might benefit from elaboration regarding its specific implementation in models like GPT, as well as its importance in ensuring that the model can only make predictions based on previously seen tokens, thus avoiding leakage of information from future tokens.
Model Zoo Section:
The term "Model Zoo" can be slightly misleading. This section should provide more context on where to find these models (like Hugging Face's Model Hub) and how they are commonly utilized in practice.
Suggestions for Adding a Chapter on Pre-training and Fine-tuning:
8. Pre-training and Fine-tuning of Large Language Models
Introduction to Pre-training:
Pre-training is a crucial step in training large language models (LLMs) where the model is initially trained on a vast corpus of text data in an unsupervised manner. This phase aims to learn general language representations and patterns that can be utilized across various tasks.
Types of Pre-training Objectives:
Discuss the common pre-training objectives such as masked language modeling (used in BERT) and next-token prediction (used in GPT). Explain the significance of these tasks in teaching the model about syntactic, semantic, and contextual relationships.
Transfer Learning:
Introduce the concept of transfer learning—how the knowledge gained during pre-training is leveraged in downstream tasks. Explain why this process helps LLMs achieve state-of-the-art results with relatively less task-specific data.
Fine-tuning for Conversational AI:
Fine-tuning adjusts the pre-trained models to perform specific tasks in conversational AI. This could include training on datasets specific to dialogue systems, question answering, or sentiment analysis.
Elaborate on techniques such as few-shot learning, supervised fine-tuning, and prompt engineering, and how these techniques can improve a model's performance on specialized tasks.
Avoiding Overfitting:
Discuss techniques like dropout, data augmentation, and regularization that are crucial during the fine-tuning phase to prevent overfitting, especially when the size of the fine-tuning dataset is limited.
Challenges in Conversational AI:
Address common challenges in conversational AI that can arise from fine-tuning, such as maintaining conversational context, understanding user intent, and ensuring coherent and contextually relevant responses.
Conclusion for the Added Chapter:
This new chapter will provide a rounded understanding of how pre-training and fine-tuning contribute to the effectiveness of transformers in the realm of conversational AI. It emphasizes the importance of these steps in transforming a general-purpose language model into a specialized tool that can handle varied conversational tasks effectively.
By addressing these points and incorporating the new chapter, the exposé will provide a more comprehensive, accurate, and clear understanding of transformers and their impact on conversational AI.
The important information about the quadratic computational complexity of self-attention could be placed in several locations within your exposé, depending on how you want to integrate it into the flow of information. Here are some suggestions:
In Section 4.2: Self-Attention Mechanism
You can add a statement about the complexity of self-attention after the explanation of how self-attention works. For example, you could enhance this section as follows:
Revised segment in Section 4.2:
"Self-attention allows the model to understand how each word in a sentence relates to every other word. However, this capability comes with a computational cost. Specifically, the self-attention mechanism has a quadratic computational complexity, O(n²), where n is the length of the input sequence. This means that as the input length increases, the time and memory required grow significantly, which can limit the practical application of transformers with long sequences."
In Section 4.3: Multi-Head Attention
Since multi-head attention builds on the concept of self-attention, you could also mention the complexity here. You might frame it in the context of how the multi-head approach manages computational resources.
Revised segment in Section 4.3:
"The attention mechanism determines the importance of other words towards a given word and can be applied multiple times independently of each other to focus on different parts of the input sequence. However, because each self-attention layer operates with quadratic complexity, O(n²), running multiple heads in parallel can significantly increase the overall computational cost, making it necessary to balance model complexity with available resources."
In Section 4.5: Masked Self-Attention
When discussing masked self-attention, it could be useful to mention how the quadratic complexity impacts the performance of the decoder in transformer models.
Revised segment in Section 4.5:
"Masked self-attention is used by the decoder to prevent it from looking at future tokens during the generation process. However, like self-attention, it comes with quadratic computational complexity, O(n²). This can pose challenges when dealing with long sequences, as the required computation time grows significantly, making efficient memory management critical."
By adding the information about the quadratic computational complexity of self-attention in these sections, you help provide a clearer understanding of the trade-offs associated with using this powerful mechanism in the transformer architecture. This addition will underscore the challenges of scaling transformer models, especially in the context of long input sequences.
The discussion of backpropagation and optimization is crucial for understanding how transformers learn from data. The most suitable location for this information in your exposé would probably be within the Training section or as a new subsection under a relevant chapter. Here are specific recommendations on where to include this information:
Option 1: Within Section 5. Training
This section already touches on how transformers learn relationships between words, so it is a logical place to discuss backpropagation and optimization.
Revised Version of Section 5:
Add a new paragraph after the first sentence to detail the backpropagation process. For example:
Revised Segment:
"Transformers are trained to predict the next word in a sentence or translate text sequences. The training process enables the model to learn relationships between words, storing this information within its network weights. Learning occurs through a process called backpropagation. During training, the model makes predictions based on the current embeddings, and then the error between these predictions and the actual values is calculated. This error is used to adjust the weights of the embeddings to minimize the difference between predicted and actual values, effectively bringing the predictions closer to the true outcomes. Various optimization algorithms, such as Adam, are commonly employed to update these weights efficiently."
Option 2: Create a New Subsection Under Section 5
If you want to emphasize backpropagation and optimization further, you could create a dedicated subsection titled 5.1 Backpropagation and Optimization.
New Subsection 5.1 Content:
Here’s how the content might look:
5.1 Backpropagation and Optimization
"Learning in transformers is facilitated through a process called backpropagation. Initially, the model makes predictions based on the current embeddings. The error between these predictions and the actual values is computed using a loss function. This error is then backpropagated through the network to update the model weights.
The adjustments aim to minimize the error, thereby bringing the predictions closer to the actual values. Efficient optimization algorithms, such as Adam, are commonly used to perform these updates. These algorithms dynamically adjust the learning rate, allowing for faster convergence and better performance during training. By iteratively refining the weights based on the accumulated errors, the transformer learns meaningful representations and relationships in the data."
Conclusion:
By adding this information in either of the suggested locations, you create a more comprehensive understanding of how transformers are trained and the fundamental mechanism behind their learning process. Choose the option that best fits your writing style and the overall structure of your exposé.
During the training process of a transformer model, the model is typically engaged in predicting one of the following:
1. Next Token Prediction:
What is the Model Predicting? In many language models, such as GPT (Generative Pre-trained Transformer), the model predicts the next token in a sequence based on the preceding tokens. For instance, given the input "The cat is on the," the model attempts to predict the next token, which could be "mat."
What is it Compared Against? This predicted token is compared against the actual token that follows in the training data. The training data consists of sequences where the true next token is known, allowing for the calculation of the prediction error, typically using a loss function such as cross-entropy loss.
2. Masked Language Modeling (MLM):
What is the Model Predicting? In models like BERT (Bidirectional Encoder Representations from Transformers), a portion of the input tokens is masked (i.e., hidden) during training. The model is tasked with predicting these masked tokens based on the context provided by the other (unmasked) tokens in the input sequence.
What is it Compared Against? The predicted tokens are compared against the original (true) tokens that were masked out. Again, the error is calculated using a suitable loss function, measuring how well the predicted tokens match the actual masked tokens.
3. Sequence Classification:
What is the Model Predicting? In scenarios like sentiment analysis or topic classification, the model may predict a category or label for an entire input sequence (e.g., positive or negative sentiment).
What is it Compared Against? The model's predicted label is compared to the true label assigned to that input during training. The error is computed based on how accurately the model's prediction aligns with the true classification.
Error Calculation:
The error calculated during this process typically involves a loss function, which quantifies the difference between the predicted outputs and the actual target values. In the case of token prediction, for instance, the cross-entropy loss function computes how well the predicted probabilities match the true distribution of the expected targets.
Conclusion:
In summary, during training:
The model predicts either the next token (in autoregressive models) or the masked tokens (in bidirectional models) based on the current embeddings.
The predictions are compared against the actual tokens in the training data, allowing the model to learn and adjust its weights through backpropagation to minimize prediction errors.
In the original transformer architecture as introduced in the paper "Attention Is All You Need" by Vaswani et al. (2017), the model works primarily in the context of sequence-to-sequence tasks, such as machine translation. Here’s a breakdown of what the model predicts and what it compares against in this context:
Model Predictions in the Original Transformer
Encoder-Decoder Structure:
The original transformer consists of two main components: the encoder and the decoder.
Encoder: The encoder takes a source sequence (e.g., a sentence in one language) and processes it to create a set of continuous representations (or embeddings) for those tokens, capturing contextual relationships and meanings.
Decoder: The decoder generates predictions based on both the encoder's output and the previously generated tokens in the output sequence (e.g., a translated sentence).
Next Token Prediction:
What the Model Predicts: During the decoding phase, the model predicts the next token in the target sequence based on its current context and the encoder's output.
For instance, if the output sequence starts with a "Start of Sequence" token and the model has produced the tokens "Je suis" (French for "I am"), it will predict the next token based on that context.
Loss Calculation:
What it is Compared Against: The predicted token is compared against the actual token that should follow in the target sequence during training. For example, if the true target sequence is "Je suis content" (French for "I am happy"), and if the model predicts "je," the prediction will be evaluated against "suis."
The model uses a loss function (typically cross-entropy loss) to compute the error between the predicted probability distribution over the vocabulary and the actual distribution (which is one-hot encoded for the true next token).
Autoregressive Property:
Masked Attention: During training, the decoder uses masked self-attention to ensure that the prediction for a token is based only on the tokens that precede it. This autoregressive nature prevents the model from accessing future tokens when generating the output sequence.
The decoder generates one token at a time, appending each predicted token to the input for the next prediction until an "End of Sequence" token is generated or a predefined maximum length is reached.
Summary
So, in summary:
In the Original Transformer: The model predicts the next token in the output sequence (e.g., during a translation task) based on the previously generated tokens and the context provided by the input sequence encoded by the encoder.
Comparative Basis: These predicted tokens are compared against the actual tokens present in the target output sequence during training, allowing the model to learn and optimize its weights through backpropagation based on the calculated loss.
This paradigm of sequence generation via predicted tokens is foundational in many NLP tasks and has been a critical advancement in neural network architectures for language processing.