Before transformer models, Recurrent Neural Networks (RNNs) were used to generate text in the field of NLP. RNNs predict output by assessing the relationship of a word with its immediate neighbors

image.png

But RNNs had 2 major flaws

  1. Difficulty in handling long sequences RNNs struggle to learn from things that happened a long time ago in the sequence i.e. with long-range dependencies in text. This means: They find it difficult to comprehend a very long sentence or paragraph where the meaning depends heavily on something mentioned at the very beginning.
  2. Computational complexity Training RNNs can be quite resourceintensive, particularly with extended sequences. This is attributed to the sequential processing of each input by the network, a method that can be time-consuming.

But the Transformer architecture solves all these problems The strength of the transformer model is in its ability to understand the significance and context of every word in a sentence.

image.png

Transformer The Transformer architecture was introduced by Vaswani et al. in 2017 as a groundbreaking neural network design widely used in natural language processing tasks like text categorization, language modeling, and machine translation.

At its core, the Transformer architecture resembles an encoder-decoder model. The process begins with the encoder, which takes the input sequence and generates a hidden representation of it. This hidden representation contains essential information about the input sequence and serves as a contextualized representation. The hidden representation is then passed to the decoder, which utilizes it to generate the output sequence. Both the encoder and decoder consist of multiple layers of self-attention and feed-forward neural networks. The self-attention layer computes attention weights between all pairs of input components, allowing the model to focus on different parts of the input sequence as needed. The attention weights are used to compute a weighted sum of the input elements, providing the model with a way to selectively incorporate relevant information from the entire input sequence. The feed-forward layer further processes the output of the self-attention layer with nonlinear transformations, enhancing the model’s ability to capture complex patterns and relationships in the data. The Transformer design offers several advantages over prior neural network architectures:

  1. Efficiency: It enables parallel processing of the input sequence, making it faster and more computationally efficient compared to traditional sequential models.
  2. Interpretability: The attention weights can be visualized, allowing us to see which parts of the input sequence the model focuses on during processing, making it easier to understand and interpret the model’s behavior.
  3. Global Context: The Transformer can consider the entire input sequence simultaneously, allowing it to capture long-range dependencies and improve performance on tasks like machine translation, where the context from the entire sentence is crucial. The Transformer architecture has become a dominant approach in natural language processing and has significantly advanced the state of the art in various language-related tasks, thanks to its efficiency, interpretability, and ability to capture global context in the data. Chapter 2 Eevolution of neural networks to large language Models

How does the Transformer model work?

image.png

  1. Tokenization Tokenization is the process of breaking down text into smaller units, such as words or phrases, for easier processing and analysis.

    image.png

  2. Embeddings Embedding is the process where each token is then transformed into a vector in a high-dimensional space. This embedding captures the meaning and context of each word.

    image.png

  3. Positional encoding Since transformers do not process text sequentially like RNNs, they need a way to understand the order of words. Positional encoding is the process of adding information to a model about the position of elements in a sequence.

    image.png

  4. Self-attention The model calculates attention scores for each word, determining how much focus it should put on other words in the sentence when trying to understand a particular word. This helps the model capture relationships and context within the text.

    image.png

  5. Multi-Headed attention Multi-headed attention is a mechanism in transformers that runs several self-attention processes in parallel, allowing the model to focus on different parts of the input sequence from different perspectives at the same time.

    image.png

  6. Output The final layers of the transformer convert the processed data into an output format suitable for the task at hand, such as classifying the text or generating new text.

    image.png