Recommended resources
The freely available resources this lesson will direct you to.
- Confused which Transformer Architecture to use? BERT, GPT-3, T5, Chat GPT? Encoder Decoder Explainedwww.youtube.com
- Which transformer architecture is best? Encoder-only vs Encoder-decoder vs Decoder-only modelswww.youtube.com
- Understanding Encoder And Decoder LLMs - Ahead of AImagazine.sebastianraschka.com
- Transformer Architectures - Hugging Face LLM Coursehuggingface.co
Hello! Welcome back.
In our last few lessons, we've done a thorough exploration of the original Transformer architecture, focusing on how its encoder and decoder stacks work together for sequence-to-sequence tasks like machine translation. You learned about the crucial role of cross-attention and the different operational flows during training versus inference.
However, the complete encoder-decoder structure is just one way to build with Transformer blocks. The real power and flexibility of this architecture became apparent when researchers started using just one half of it. Today, we'll explore these variations. Our goal is to differentiate between encoder-only (like BERT), decoder-only (like GPT), and encoder-decoder (like T5) model architectures. We'll discover what makes each one unique, the types of problems they excel at, and which famous models belong to each family.
This lesson concludes our deep dive into Transformer theory. Understanding these architectural families is key to navigating the modern landscape of Large Language Models (LLMs) and choosing the right tool for a given task.
The Three Families of Transformer Models
The original Transformer was designed for machine translation, a task that requires mapping one sequence (input) to another (output). But not all NLP tasks fit this mold. Some require understanding a sentence's meaning (classification), while others focus purely on generating new text (creative writing). This led to the development of three distinct architectural families.
To get a high-level overview of these three families, let's start with a short and clear video.
Which transformer architecture is best? Encoder-only vs Encoder-decoder vs Decoder-only models
This video from the Efficient NLP channel provides an excellent introduction to the three main types of Transformer architectures and their primary purposes.
Please watch the introduction from 00:00:00 to 00:00:51. The speaker will lay out the three categories we are about to discuss: encoder-only, encoder-decoder, and decoder-only models.
Now, let's look at a visual comparison. The image below illustrates the core structural differences between models representing each of these families.

As we go through each architecture, keep this diagram in mind. Let's now examine each one in detail.
1. Encoder-Only Models: The Masters of Understanding
Encoder-only models use just the encoder stack from the original Transformer. Their defining feature is the bi-directional self-attention mechanism.
- How they work: When processing an input sentence, the representation for each token is built by attending to all other tokens in the sentence, both to the left and to the right. This allows the model to build a deep, contextual understanding of the entire input.
- What they're used for: They excel at tasks that require Natural Language Understanding (NLU), where a comprehensive grasp of the full sentence context is crucial.
- Text Classification: Sentiment analysis, topic categorization.
- Named Entity Recognition (NER): Identifying people, places, and organizations in text.
- Extractive Question Answering: Finding the answer to a question within a given passage of text.
- How they're trained: A common pre-training objective is Masked Language Modeling (MLM). The model is given a sentence with some words randomly masked (e.g., "The [MASK] jumped over the lazy dog."), and its task is to predict the original masked words. This forces it to learn the relationships between words in context.
- Famous Examples: BERT (Bidirectional Encoder Representations from Transformers), RoBERTa, and DistilBERT.
The main limitation of encoder-only models is that they are not designed for generating new, free-flowing text in an autoregressive manner. They are primarily analyzers, not creators.
Transformer Architectures - Hugging Face LLM Course
To solidify your understanding, please read the following section from the Hugging Face LLM Course. It provides a concise explanation of encoder models and their best use cases.
Read the section titled 'Encoder models'. Focus on the description of 'bi-directional' attention and the types of tasks these models are best suited for.
2. Decoder-Only Models: The Fluent Generators
Decoder-only models use just the decoder stack. Their defining characteristic is the causal (or masked) self-attention mechanism.
- How they work: When predicting the next token in a sequence, the model can only attend to the tokens that came before it. This autoregressive behavior is exactly what's needed for generating text one word at a time, as we saw in the previous lesson's discussion on inference.
- What they're used for: They are the backbone of modern Natural Language Generation (NLG) and are at the heart of most famous LLMs.
- Text Generation: Writing stories, poems, or articles.
- Conversational AI: Powering chatbots like ChatGPT.
- Code Generation: Writing code from natural language prompts.
- How they're trained: Their pre-training objective is simple but powerful: next-token prediction. Given a sequence of text, the model is trained to predict the very next word. By doing this on a massive corpus of internet text, these models learn grammar, facts, reasoning abilities, and style.
- Famous Examples: The GPT series (Generative Pre-trained Transformer), LLaMA, and Gemma.
Which transformer architecture is best? Encoder-only vs Encoder-decoder vs Decoder-only models
Let's return to the 'Efficient NLP' video to see the explanation of both encoder-only and decoder-only models.
Please watch the segment on encoder-only models (00:00:51 - 00:02:30) and the segment on decoder-only models (00:04:32 - 00:07:19). Notice the clear distinction made between 'understanding' text (encoder) and 'generating' text (decoder).
3. Encoder-Decoder Models: The Transformers
These models, also known as sequence-to-sequence (seq2seq) models, use the full architecture from the "Attention Is All You Need" paper. They combine the strengths of the other two families.
- How they work:
- The encoder uses bi-directional attention to create a rich contextual representation of the entire input sequence.
- The decoder generates the output sequence autoregressively, but at each step, it uses cross-attention to look at the encoder's output. This allows it to base its generation on the meaning of the input sequence.
- What they're used for: They are ideal for tasks that require transforming an input sequence into a different output sequence.
- Machine Translation: Translating from one language to another.
- Text Summarization: Condensing a long document into a short summary.
- Generative Question Answering: Generating an answer in natural language based on a context.
- How they're trained: The training objectives often involve reconstructing corrupted data. For example, the T5 model is pre-trained by replacing random spans of text with a mask and tasking the model to predict the missing text.
- Famous Examples: The original Transformer, T5 (Text-to-Text Transfer Transformer), and BART.
Which transformer architecture is best? Encoder-only vs Encoder-decoder vs Decoder-only models
Finally, let's watch the segment from the 'Efficient NLP' video on encoder-decoder models to complete the picture.
Watch the section from 00:02:30 to 00:04:32. Pay attention to how it describes the combination of an encoder to process the input and a decoder to generate the output.
Choosing the Right Architecture
So, given a task, how do you choose? The Hugging Face course provides a very helpful guide. The key is to think about the nature of your input and desired output.
Transformer Architectures - Hugging Face LLM Course
The following resource provides a clear guide and a summary table to help you decide which architecture is best for a given task. This synthesizes everything we've just discussed.
Please read the sections 'Sequence-to-sequence models' and 'Choosing the right architecture'. The table in the second section is a particularly useful summary.
For a deeper technical perspective on the differences, especially regarding their pre-training objectives and the nuances of terminology, the following article by Sebastian Raschka is an excellent, though optional, read.
Understanding Encoder And Decoder LLMs
This article provides more technical depth, clarifying the pre-training objectives like Masked Language Modeling (MLM) and explaining why even 'encoder-only' models perform a type of decoding.
This is an optional reading. Skim through the sections on 'Encoders', 'Decoders', 'Encoder-decoder hybrids', and the 'Conclusion'. It offers a great reinforcement of the concepts we've covered.
Conclusion
You have now completed your tour of the fundamental Transformer architectures. You've moved from understanding the individual blocks to seeing how they form the complete encoder-decoder model, and now, how they give rise to a whole family of powerful architectural variants.
Key Takeaways:
- Encoder-Only (e.g., BERT): Uses bi-directional attention to create deep contextual understanding. Best for analysis tasks like classification and NER. Cannot generate text autoregressively.
- Decoder-Only (e.g., GPT): Uses masked, autoregressive attention to generate new text fluently. The foundation for most modern LLMs. Best for creative generation and chatbots.
- Encoder-Decoder (e.g., T5): Combines the strengths of both. The encoder understands the input, and the decoder generates a transformed output. Best for sequence-to-sequence tasks like translation and summarization.
- The choice of architecture depends directly on the task: understanding (encoder), generation (decoder), or transformation (encoder-decoder).
Preview of the Next Lesson
With this solid theoretical foundation, we are now ready to transition to the practical side of things. In our next module, we'll begin our journey with the HuggingFace transformers library. The very first step in any NLP pipeline is turning raw text into a format the model can process. In the next lesson, we will dive into tokenization and explore the differences between word, character, and subword tokenization methods like BPE, which are crucial for the models we've just discussed.