Skip to main content
Deep Neural Networks

Transformer Architectures and Optimization

📅 Published: 2026-07-15
🎓 Level: postgraduate
👥 Audience: Postgraduate students in Deep Neural Networks

Prerequisite Knowledge

This lecture builds on the following concepts from earlier lectures. If any feel unfamiliar, review the linked notes before proceeding.

Previously Covered in This Subject

  • Attention mechanisms (self-attention, cross-attention, scaled dot-product attention) — covered in Lectures 14 and 15
  • Encoder-decoder architecture foundations — covered in Lecture 14
  • Transformer encoder components (layer normalization, residual connections, multi-head attention) — covered in Lecture 15
  • Positional encoding and sinusoidal embeddings — covered in Lecture 15

Transformer Architectures and Optimization

16.1 Encoder and Decoder — Purpose and Intuition

Hook. How does a machine read a thousand-word article and answer.

A single question about it? And how does that same machine then write an entirely new paragraph.

in French — based on what it read? These two abilities.

understanding and generating — live in two different architectures: the encoder and the decoder.

An encoder is a component that reads input and compresses it..

A decoder is a component that takes compressed information and generates new output from it.

Together they form the backbone of every modern transformer. Let's unpack each one.

16.1.1 What an Encoder Does

Intuition + Analogy. Think of a librarian who reads every book in a library and writes one index card per book.

The card does not store every word. It stores the essence.

the main ideas, key characters, genre. Later, someone can pick up that card and answer questions without re-reading the whole book. The librarian is the encoder. The index card is the context.

a dense numerical vector that captures the compressed meaning of the input.

The analogy maps tightly: the books are input sequences (text, time-series, any sequential data). The librarian's reading process is the encoder's computation.

Each index card is a context vector. The card-holder who answers questions is any downstream task head. Where the analogy breaks.

the encoder does not select "important parts" consciously.

every token contributes to the context through learned attention weights. There is no human-like judgment.

Formalize. An encoder is a function that maps an input sequence of tokens to a fixed-size context vector :

Each token starts as a discrete symbol (a word or sub-word). It gets embedded into a vector in .

The encoder processes all vectors through stacked self-attention and feed-forward layers. The final output is either.

A sequence of context-rich token representations (one per input position) or a single aggregated context vector (like the [CLS] token in BERT).

The context is then plugged into a task head — a small output layer that solves a specific problem:

  • Classification head: for sentiment analysis, topic classification.
  • Tagging head: one softmax per token position for part-of-speech tagging, named entity recognition.
  • Span head: two pointers (start, end) for extractive question answering.

Worked Example. English-to-French Translation (encoder side).

Take the input sentence: "The cat sits."

Step What happens
1. Tokenize ["The", "cat", "sits"] — 3 tokens
2. Embed Each token becomes a vector of size . Shape: .
3. Add positional embedding Shape stays .
4. Encoder stack 6 encoder blocks process all 3 tokens at once using bidirectional self-attention.
5. Output 3 context-rich vectors, one per input token. Shape: .

These 3 vectors carry the full meaning of the English sentence in a form the decoder can use.

The encoder's job ends here. It has read and understood. Now the decoder will generate the French: "Le chat est assis."

Scope — What the Encoder Is and Is Not.

  • The encoder does NOT generate text. It only produces representations. If you need a model that writes, you need a decoder.
  • The encoder sees all tokens at once. It uses bidirectional attention. This makes it powerful for understanding tasks but useless for autoregressive generation (where seeing the future would be cheating).
  • The encoder output is NOT human-readable. Context vectors are high-dimensional arrays of floats. You decode them into labels or text through a task-specific head.
  • Assumption: The encoder assumes the whole input is available at once. It cannot process streaming data token-by-token (a decoder can).

Visual Intuition. Picture a funnel. At the wide top, words flow in as 512-dimensional vectors. They pass through the funnel's neck.

the attention layers — where.

Each word's meaning gets refined by looking at every other word. At the narrow bottom, a compact representation emerges.

For a classification encoder, the bottom is a single [CLS] vector. For a translation encoder,.

The bottom is one refined vector per input token. The x-axis is token position (1 to N). The y-axis is richness of representation.

shallow at the top, deep and contextual at the bottom.

Pitfalls — Common Traps.

  1. Confusing encoder output with generated text. The encoder produces vectors, not words.

A student once asked, "Why is the encoder output not French yet?".

because the encoder only understands; generation is the decoder's job.

  1. Thinking the encoder compresses by dropping information. It does not pick and choose tokens.

Every token contributes to the context through weighted sums. The compression comes from learning which patterns matter, not from deleting data.

  1. Assuming a bigger context vector is always better. or 768 or 1024.

the right size depends on the task. Bigger is not always better; it costs more compute and may overfit on small datasets.

  1. **Forgetting that the encoder sees.

The entire input.** If your task requires causal (left-to-right) processing, a bidirectional encoder is the wrong choice.

Use a decoder instead.

16.1.2 What a Decoder Does

Intuition + Analogy. You receive a compressed ZIP file. You cannot read it directly. You run an unzip program.

The program reads the compressed bytes and reconstructs the original files..

The decoder is the unzip program. It takes the compressed context and generates a new sequence.

the original text, a translation, a summary, or a conversational reply.

Now extend the analogy: a decoder-only model is like a storyteller who starts with.

A single opening phrase ("Once upon a time") and generates every word that follows.

one at a time — each new word deciding what comes next.

There is no separate compressed input. The prompt itself is the seed.

Formalize. A decoder is an autoregressive function that generates an output sequence one token at a time:

where is the context from an encoder (in encoder-decoder models) or (in decoder-only models).

At each time step , the decoder outputs a probability distribution over the entire vocabulary:

where is the decoder's hidden state at position .

The model picks the next token by sampling from this distribution (greedy: pick the highest probability.

top-k: pick from the top candidates).

For a decoder-only model, the process starts with a special <START> token..

The first generated word gets fed back as input for step 2.

Step 2's output becomes input for step 3..

This loop continues until a <END> token is generated or a maximum length is reached.

Worked Example. Generating Text with a Decoder-Only Model.

Prompt: "The weather today"

Step Input seen so far Model predicts Picked
1 <START> The weather today is (0.42), was (0.31), will (0.18) is
2 <START> The weather today is sunny (0.55), cloudy (0.23), rainy (0.12) sunny
3 <START> The weather today is sunny . (0.61), and (0.22), with (0.09) .
4 <START> The weather today is sunny . <END> (0.88) stop

Sense-check: The model produced a plausible continuation. At each step it used only the words before it.

masked attention prevented peeking ahead. The generation stopped naturally. Final output: "The weather today is sunny."

16.1.3 When You Need Both

Formalize — Encoder-Decoder Architecture. A sequence-to-sequence (seq2seq) task maps one sequence to another. You need both encoder and decoder:

The encoder produces context . The decoder generates.

The output token by token, attending to at every step via cross-attention.

The encoder and decoder are trained end-to-end.

the loss from the decoder's wrong predictions flows back through both components.

Comparison — When to pick which architecture:

Task type Architecture Why
Classification, tagging, NER Encoder-only Only understanding needed; no generation
Chat, story generation, code completion Decoder-only Pure generation from a prompt
Translation, summarization, image captioning Encoder-decoder Map one sequence to a different sequence

16.1.4 Student Questions and Answers

Q: Can an encoder be thought of as compressing information into bits, like in information theory?

A: Yes, the analogy holds well. In neural networks, text and sequential data get compressed into numerical vector representations.

those vectors are the compressed data, similar to bits in a communication channel. The encoder compresses.

the decoder decompresses or uses that essence for downstream tasks.

The analogy helps even though the implementations differ across domains.

information theory deals with bits and entropy, while neural networks deal with floating-point vectors and learned representations.

Recap. An encoder reads and compresses input into a context vector. A decoder generates new output from a context or a prompt.

An encoder-decoder pairs them for sequence-to-sequence tasks. Architecture choice depends on one question.

do you need to only understand, only generate, or do both?

Bridge. Now that we know what encoders and decoders do, the next question is how they do it.

The core mechanism inside both is attention.

the topic of Section 16.2.

Real-World Connection. Google Translate uses an encoder-decoder transformer. The encoder reads your English sentence and produces context vectors.

The decoder generates the French (or any of 100+ languages) word by word, attending back to the English encoder output at every step. The same architecture.

with a vision encoder instead of a text encoder.

powers image captioning: the encoder "reads" the image patches; the decoder writes.

The description. Encoder-only BERT powers nearly every search engine's query understanding. Decoder-only GPT powers ChatGPT, Copilot, and Claude.

any interface where you type a prompt and get generated text back.

---

16.2 Attention Mechanisms — Recap

Hook. How does a model know that "cat" in "the cat sat on the mat" is the animal doing the sitting.

and not a person or a piece of construction equipment? It checks every other word in the sentence. This simple idea.

letting each word look at every other word — is called attention. And it powers every modern transformer.

Attention is the core computation inside both encoders and decoders. Before we dive into the architecture details, let's revisit how attention works.

16.2.1 Self-Attention

Intuition + Analogy. You walk into a noisy party. Someone says your name across the room.

Instantly, your brain filters out the background chatter and focuses on that one voice. Your brain just performed attention.

it assigned high weight to one sound source and low weight to everything else.

Now flip it: imagine every person at.

The party simultaneously tunes in to every other person, weighting how relevant each conversation is to them.

That is self-attention. Every word is both a listener (query) and a speaker (key/value) at the same time. The room hears itself.

Formalize — Scaled Dot-Product Self-Attention.

Given an input sequence of tokens, each with embedding dimension , we form three matrices from the same input:

where is the input, and are learned projection matrices.

The attention output is a weighted sum of values, where the weights come from query-key compatibility:

Step by step:

  1. Compute scores: .

each query is dotted with every key. For tokens, shape is .

  1. Scale: Divide by .

prevents the dot products from growing too large, which would push softmax into regions of tiny gradients.

  1. Softmax: Each row becomes a probability distribution over all keys.
  2. Weighted sum: Multiply the softmax weights by the values .

The output for each token is a mixture of all value vectors.

Because the queries, keys, and values all come from the same sequence, this is called self-attention.

Why scaling matters: With , two random vectors have dot product around 8. Without scaling, is near 1 while is near 0. The attention becomes nearly one-hot. The factor keeps variance at 1.

Worked Example — Self-Attention on a 3-word sentence.

Input: "the cat sat" with (tiny for illustration), .

Token Embedding (simplified)
the
cat
sat

Assume learned projections produce these query and key vectors (simplified):

Token Query Key
the
cat
sat

Compute (scores before scaling):

Scale by :

Apply softmax per row (approximate):

Token Attends to "the" Attends to "cat" Attends to "sat"
the 0.58 0.11 0.31
cat 0.11 0.58 0.31
sat 0.23 0.23 0.54

Sense-check: Each word attends most to itself (diagonal dominance is common in early layers) but also spreads some weight to other words.

"sat" distributes more evenly because it relates to both "the" and "cat."

Key insight: Self-attention ignores word order. If you shuffle "the cat sat on.

The mat" to "mat cat on the sat," the self-attention scores come out the same.

This is a critical insight: the sequence order is lost, so you must inject it back.

16.2.2 Positional Embedding

Intuition + Analogy. Self-attention treats the sentence like a bag of words.

it sees who talks to whom, but not who comes first..

A positional embedding is like writing a small number (1, 2, 3, ...) above each word before handing it to the model.

But instead of plain integers, we use a pattern of sine and cosine waves that lets.

The model figure out both absolute position ("this is word #3") and relative distance ("these two words are 2 positions apart").

Formalize. Positional encoding adds a fixed or learned vector to each token's embedding:

In the original transformer (Vaswani et al.), uses sinusoidal functions:

where is the token position and indexes the dimension (0 to ). Different dimensions oscillate at different frequencies.

low dimensions change fast, high dimensions change slow. This creates a unique fingerprint for every position.

If , then — same shape as the word embedding, so they add directly.

Worked Example — Positional Encoding for Position 3.

For , :

Position 3 gets the vector . Sense-check: The values span and are bounded.

they won't explode or vanish as sequence length grows.

16.2.3 Multi-Head Attention

Intuition + Analogy. A single self-attention head is like reading a sentence through one colored lens.

you see one kind of relationship (maybe subject-verb agreement). Multi-head attention gives you several lenses.

a red one for syntax, a blue one for semantics,.

A green one for co-reference. You look through all of them at once, then combine the views into one rich picture.

Formalize. Multi-head attention runs independent attention heads in parallel, each with its own projections:

Each head operates in a reduced dimension . The outputs are concatenated and projected back:

With and : each head works in .

Concatenating 8 heads gives back 512 dimensions. mixes information across heads.

The total cost is similar to a single head of full dimension.

the per-head is smaller — but the model learns richer representations because each head can specialize.

16.2.4 Bidirectional Self-Attention

Formalize. In bidirectional self-attention, every token attends to every other token.

left and right, simultaneously. This is the attention mode used in encoders.

For a token at position , the attention distribution covers all positions .

There is no mask. The word "cat" in "the cat sat on.

The mat" sees both "the" (left context) and "sat" (right context). This is why encoders excel at understanding tasks.

they have the full picture.

In contrast, decoders use causal (masked) attention, where position only sees positions .

This prevents the decoder from peeking at future words during autoregressive generation.

16.2.5 Symbol Registry — Attention Mechanisms

  • — embedding dimension per token — scalar (e.g., 512)
  • — number of attention heads — scalar (e.g., 8)
  • — dimension per head — (e.g., 64)
  • — query matrix — matrix in
  • — key matrix — matrix in
  • — value matrix — matrix in
  • — projection matrices — each in
  • — output projection matrix —
  • — sequence length — scalar

Pitfalls — Common Traps.

  1. Forgetting the scaling. Without it, large pushes softmax into near-one-hot territory. Gradients vanish. Training stalls.
  2. Confusing keys and queries. The query is "what am I looking for?".

The key is "what do I contain?" In self-attention they come from the same place.

But they play different mathematical roles.

  1. Confusing keys and queries. The query is "what am I looking for?".

The key is "what do I contain?" In self-attention they come from the same place, but they play different mathematical roles.

  1. Thinking positional embeddings are optional for transformers. They are mandatory. Without them, self-attention is permutation-invariant.

"dog bites man" and "man bites dog" produce identical attention patterns.

  1. Using multi-head attention but forgetting the output projection. Concatenating heads without means heads cannot share information.

The output projection is not optional.

it mixes and aligns the different head perspectives.

Recap. Self-attention lets every token weigh every other token. Positional embeddings restore lost order.

Multi-head attention runs several attention patterns in parallel. Bidirectional attention gives encoders full context.

causal attention keeps decoders honest during generation.

Bridge. With attention mechanics in place, we now assemble them into a complete encoder.

adding layer normalization, residual connections, and stacking blocks. That is Section 16.3.

Real-World Connection. The scaled dot-product attention formula.

— is the single most important equation in modern NLP..

It sits inside BERT (Google Search), GPT (ChatGPT), and every other transformer.

The positional encoding trick using sine/cosine waves means a model can handle sequences of any length — 10 words or 10,000.

without learning a separate position vector for each. Multi-head attention is why.

A single BERT model can simultaneously handle part-of-speech tagging, co-reference resolution, and sentiment — each head specializes.

---

16.3 Encoder Architecture Components

Hook. Take ten encoders, stack them on top of each other, and you get a deep understanding machine.

But stacking attention blocks is not enough.

you need two silent helpers to keep the training stable: layer normalization and residual connections.

Without them, a 12-layer encoder would not train at all.

An encoder block is a reusable building block. It contains multi-head self-attention, feed-forward layers, and two key stabilization mechanisms.

Let's build it step by step.

16.3.1 Layer Normalization

Intuition + Analogy. Imagine grading exams where one student writes in tiny 8-point font and another in giant 24-point font.

Before you can compare their answers fairly, you need to resize both to the same scale. Layer normalization does this for neural network activations.

it rescales each token's feature vector to zero mean and unit variance, so no single feature dominates the next layer's computation.

Formalize. Layer normalization (LayerNorm) operates on a single token's feature vector . It computes:

where:

  • — the mean across the features of this token
  • — the standard deviation ( prevents division by zero)
  • — learnable scale and shift parameters

The operation is applied independently to each token in the sequence. The relative ordering among features is preserved.

if feature 1 was larger than feature 2 before normalization, it stays larger after (just rescaled).

The purpose is gradient stability.

it prevents any one feature from having disproportionately large or small values that would cause gradients to explode or vanish.

Pre-LN vs. Post-LN: The original transformer (Vaswani et al.) placed LayerNorm after the attention sub-layer (post-LN).

Modern architectures place it before (pre-LN). Why does placement matter?

  • Post-LN: The large, unscaled values from attention flow through the residual connection before being normalized. This can destabilize training in deep stacks.
  • Pre-LN: The input is normalized before entering attention. Gradients flow more smoothly through the block. The number of computations is identical.

only the order changes.

Worked Example — LayerNorm on a 4D vector.

Input token embedding:

Assuming and (identity, before learning):

Sense-check: The output has mean and standard deviation . The value 25 (which was the largest) is now 1.501.

still the largest, but on a controlled scale. This prevents the next layer from being dominated by that single large value.

16.3.2 Residual Connections

Formalize. A residual connection (skip connection) adds the input of a sub-layer directly to its output:

This means the sub-layer only needs to learn the residual.

the difference between what the input already provides and what is needed.

If the best thing for a particular layer is to do nothing,.

The residual can be driven to zero and the identity passes through.

Gradient flow benefit: during backpropagation, the gradient has two paths.

through the sub-layer AND directly through the identity connection. This prevents gradients from vanishing in very deep stacks.

Every encoder and decoder block uses residual connections around both the attention sub-layer and the feed-forward sub-layer.

Scope — When Residual Connections Help and When They Don't.

  • Help: Very deep networks (12+ layers). Shallow gradients can flow through the skip path directly to early layers.
  • Help: Training stability. If a layer is initialized poorly, the residual path lets signal bypass it until the layer learns useful features.
  • Do not help with: The fundamental capacity of the network. Residual connections do not add parameters.

they only change the gradient flow. A 2-layer network with skip connections is still a 2-layer network.

  • Assumption: The input and output dimensions must match for the addition to work. If a sub-layer changes dimensionality (which does not happen inside transformer blocks), a linear projection is needed on the skip path.

16.3.3 Stacking Encoder Blocks

Formalize — One Complete Encoder Block.

An encoder block consists of two sub-layers:

A single block detects one level of pattern. Stacking blocks (e.g., for BERT-base, for BERT-large) builds hierarchical features:

  • Lower blocks (1-3): capture local patterns — word pairs, short phrases, basic syntax.
  • Middle blocks (4-8): capture sentence-level patterns — subject-verb-object structure.
  • Higher blocks (9-12): capture abstract, global patterns — sentiment, topic, discourse relations.

After the final encoder block, the output goes to a task-specific head (e.g., softmax classifier).

16.3.4 Dimensionality Throughout the Encoder

Worked Example — Dimensionality Walkthrough.

Given: sequence length , embedding dimension , heads .

Component Input Shape Output Shape Learnable Parameters
Input embedding Vocab 512
Positional encoding 0 (if sinusoidal)
LayerNorm #1 :
Multi-head attention : ; :
LayerNorm #2 :
Feed-forward 2 linear layers

Sense-check: Every component preserves the shape.

this is by design. The residual connections require identical input/output shapes.

So encoder blocks can be stacked arbitrarily without worrying about dimension mismatches.

16.3.5 Encoder-Only Use Cases

A bidirectional self-attention encoder is enough for any task that needs understanding but not generation:

  • Sentiment analysis — classify a review as positive, negative, or neutral.
  • Part-of-speech (POS) tagging — label each word as noun, verb, adjective, etc.
  • Named entity recognition — identify person names, locations, organizations.
  • Question answering by span extraction — find the start and end of the answer within a passage.
  • Semantic similarity — compare embeddings of two sentences.
  • Grammatical correctness checking.

All of these are classification or pattern-extraction tasks. No sequence generation required.

16.3.6 Symbol Registry — Encoder Components

  • — input token embedding — vector in
  • — mean across the features of one token — scalar
  • — standard deviation across the features of one token — scalar
  • — scaling factor in layer norm — learnable vector in
  • — shift factor in layer norm — learnable vector in
  • — small constant to prevent division by zero — scalar (e.g., )
  • — sequence length (number of tokens) — scalar
  • — query, key, value projection matrices — each in
  • — output projection matrix —
  • — number of attention heads — scalar (e.g., 8)
  • — dimension per head — (e.g., 64)

Pitfalls — Common Traps.

  1. Confusing LayerNorm with BatchNorm. BatchNorm normalizes across the batch dimension (different samples).

LayerNorm normalizes across the feature dimension (same sample). In transformers, batch sizes vary and sequences have different lengths.

LayerNorm is preferred because it operates per-token independently of batch size.

  1. Forgetting that LayerNorm has learnable parameters. and are trained.

They let the network undo the normalization if that turns out to be better. Do not treat them as fixed.

  1. Stacking blocks without residual connections. Without skip connections, a 12-layer network would suffer from vanishing gradients.

The residual path is not optional in deep transformers.

it is what makes deep stacks trainable.

  1. Assuming more encoder blocks always help. Diminishing returns set in. BERT-base (12 layers) to BERT-large (24 layers) shows improvements, but the gains taper off while compute cost doubles.

Choose depth based on your task complexity and data size.

Recap. An encoder block combines multi-head self-attention, layer normalization, and residual connections.

LayerNorm keeps feature scales stable; residual connections keep gradients flowing. Stacking blocks builds hierarchical understanding.

local patterns in lower layers, global patterns in upper layers.

Bridge. Now that we have the encoder fully assembled, let's build its counterpart.

the decoder — which adds masking and cross-attention to the same foundations. That is Section 16.4.

Real-World Connection. Pre-LN (layer norm before attention) is now standard in nearly every transformer implementation.

GPT, LLaMA, BLOOM all use it. The switch from post-LN to pre-LN was.

A key enabler for training models with 100+ layers. Without residual connections, the 175-billion-parameter GPT-3 would simply not converge. These two seemingly small design choices.

where to normalize and whether to add skip connections — are what made the deep learning revolution possible.

---

16.4 Decoder Architecture Components

Hook. You are taking a language exam. The question asks you to translate a sentence from Spanish to English.

You can see the entire Spanish sentence in front of you.

that is fair. But what if you could also see.

The answer key? You would just copy it, learn nothing, and fail any real test.

The decoder faces this exact problem. Its solution: a mask that blinds it to future words.

The decoder shares the encoder's foundations.

self-attention, layer norm, residual connections — but adds two new mechanisms: causal masking and cross-attention. These are what make generation possible.

16.4.1 Masked Multi-Head Attention

Intuition + Analogy. You are writing a sentence word by word.

When you write the third word, you know words one and two. You do NOT know word four.

you have not written it yet. Masked attention enforces this rule inside the model.

It is like putting blinders on a horse.

the horse can see the road ahead (past tokens) but cannot see what is in the adjacent lane (future tokens).

Formalize. During training, the decoder receives the full target sequence as input.

Without masking, when predicting token , the model could peek at tokens .

the very answers it should be predicting. Masking prevents this by setting attention scores for future positions to before the softmax:

where the mask matrix is:

After softmax, every future position gets weight exactly zero. The attention matrix is lower triangular.

tokens can only attend to themselves and earlier positions.

Consequence of not masking: The model produces perfect training loss but fails catastrophically at inference.

because at inference, future tokens truly are not available. The model never learned to generate without peeking.

This looks like overfitting but is actually a data-leakage problem in the causal direction.

Worked Example — Masked Attention Matrix.

Sequence: ["<START>", "I", "love", "NLP"] (4 tokens).

Before masking, might yield:

Apply mask (set upper triangle to ):

After softmax per row:

Sense-check: Token 1 ("<START>") can only attend to itself. Token 2 ("I") attends mostly to itself but some to "<START>".

Token 4 ("NLP") attends to.

All previous tokens but with zero weight on future positions (there are none for the last token). The diagonal has high values because each token's self-match is strong.

16.4.2 Cross Attention

Intuition + Analogy. Masked self-attention lets.

The decoder understand its own output so far. But for translation, the decoder also needs to look at the source sentence.

Cross attention is the bridge. Think of it as.

A translator who keeps glancing back at the original text while writing the translation.

each generated word prompts a fresh look at the source.

Formalize. Cross attention is the decoder's connection to the encoder. The key difference from self-attention: where do , , come from?

  • Query : Comes from the decoder's current hidden state — "what am I looking for in the source?"
  • Keys and Values : Come from the encoder's output — "here is everything the source means."

Every decoder position can attend to every encoder position.

no masking needed for cross attention because the encoder's output is already fully known (bidirectional).

This pulls source-language context for translation, source-document context for summarization, or image-region context for captioning.

16.4.3 Decoder Stacking

Like encoders, decoder blocks stack. Each block contains three sub-layers (with pre-LN):

  1. Masked multi-head self-attention — processes the decoder's own output so far.
  2. Cross multi-head attention — attends to the encoder's output.
  3. Feed-forward network — transforms each position independently.

Each sub-layer has its own layer normalization and residual connection.

A decoder with blocks learns to generate through progressively refined representations.

16.4.4 Output Layer

After the final decoder block, the output goes through:

  1. Linear transformation: Maps from to vocabulary size .
  2. Softmax: Converts to a probability distribution over all tokens.

At each time step, the token with the highest probability is picked (greedy decoding).

Alternatively, top-k sampling selects from the top most probable tokens, adding diversity to the generation.

16.4.5 Parameter Comparison with Encoder

The decoder has about 33% more learnable parameters than the encoder:

Component Encoder Decoder
Self-attention weights (masked)
Cross-attention weights Extra
Layer norms 2 per block 3 per block
Feed-forward 1 per block 1 per block

The extra cross-attention weights (4 matrices) and extra layer norm (2 parameters) account for the 33% increase.

This makes the decoder slower to train than the encoder.

16.4.6 Symbol Registry — Decoder Components

  • — current decoder position — integer index
  • — key token position — integer index
  • — mask matrix — if , if
  • — query from decoder —
  • — keys from encoder output —
  • — values from encoder output —
  • — vocabulary size — scalar

16.4.7 Student Questions and Answers

Q: Why do we need masked attention in the decoder? Is.

It to differentiate and not learn too much from the decoder's input?

A: You are very close. The core reason: during training, the decoder has access to the entire target sequence.

Without masking, when predicting word two, it could peek at word three, word four, and so on. The machine would cheat.

it would take shortcuts instead of learning the true conditional dependencies..

It is like knowing the answer key in a math exam and reverse-engineering the steps.

Masking hides future tokens by setting their attention scores to . That way the decoder learns to generate each word using only what came before.

Pitfalls — Common Traps.

  1. Forgetting to mask during training but not inference. If you apply masking during training but forget to enforce causal generation during inference, the model produces garbage. It expects a mask that is not there.

If you fail to mask during training, the loss will look great but inference will fail.

  1. Confusing cross attention with self-attention. In cross attention, keys and values come from the encoder.

In self-attention, they come from the decoder itself. Mixing them up means the model never looks at the source.

it hallucinates the output.

  1. Teacher forcing trap. During training, the decoder receives the ground-truth previous token (teacher forcing).

During inference, it receives its own prediction..

A small error at step 3 can cascade into nonsense by step 10. This is called exposure bias.

  1. Assuming greedy decoding is always best. Picking.

The single highest-probability token at every step can produce repetitive, boring text (e.g., "I am very very very very...").

Top-k or nucleus sampling adds controlled randomness.

Recap. The decoder extends the encoder with masked self-attention (prevents peeking at future tokens) and cross attention (looks at the encoder's output).

It has ~33% more parameters and is slower to train. The output layer converts hidden states to vocabulary probabilities for autoregressive generation.

Bridge. With both encoder and decoder fully understood, the next question is practical.

which architecture do you pick for your task? Section 16.5 answers that.

Real-World Connection. Masked self-attention is why GPT can generate coherent paragraphs.

it truly does not know word 100 when writing word 10. Cross attention is why Google Translate can translate this paragraph to Japanese.

the decoder generates each Japanese token while attending to the English source..

The teacher-forcing gap (training with ground-truth vs. inference with own predictions) is an active research problem.

techniques like scheduled sampling attempt to bridge it by occasionally feeding the model's own predictions during training.

---

16.5 Architecture Selection

Hook. You have three hammers. One is for pounding nails (classification). One is for pulling them out (generation).

One does both (translation). Picking.

The wrong hammer wastes weeks of training and fails at inference. The rule is simple: know your output.

16.5.1 When to Choose Each Architecture

Formalize — Decision Framework.

The architecture choice follows directly from the task type:

Architecture Use when... Examples
Encoder-only You need to understand and classify the input. Output is a label, not a sequence. Sentiment analysis, POS tagging, NER, span QA, semantic similarity
Decoder-only You need to generate text from a prompt. No separate source sequence exists. Chat, story generation, code completion
Encoder-decoder You need to map one sequence to a different sequence. Translation, summarization, image captioning

The decision tree:

  1. Does your output require generating new text? → If no: encoder-only.
  2. Is there a separate input sequence that must be transformed? → If yes: encoder-decoder.
  3. Is the input just a prompt that gets continued? → Decoder-only.

Comparison — Architectural Trade-offs:

Dimension Encoder-only Decoder-only Encoder-decoder
Attention Bidirectional Causal (masked) Both
Parameters Fewest Medium Most
Training speed Fastest Medium Slowest
Inference speed One pass Sequential (slow) Sequential (slowest)
Best for Understanding Generation Transformation

16.5.2 Training vs Inference Behavior

Formalize.

Training (encoder-decoder):

  • Encoder: processes all source tokens at once using bidirectional self-attention. Runtime: parallel over all positions.
  • Decoder: processes target tokens with causal masking. Since all target tokens are known during training, the decoder also runs in parallel.

each position's prediction is computed simultaneously. The mask handles causality.

  • Loss: cross-entropy between predicted and actual next tokens, averaged over all positions.

Inference (generation):

  • Encoder: runs once, caches its output. Same as training.
  • Decoder: runs sequentially. Generate token 1 → feed it back as input → generate token 2 → ... Each step depends on the previous step's output. No parallelism possible in the autoregressive loop. This is why inference latency matters for real-time applications.

Pitfalls — Common Traps.

  1. Using an encoder-only model for generation. BERT cannot generate coherent paragraphs.

It has no autoregressive decoder. If you need text output, you need at minimum a decoder.

  1. Using a decoder-only model when you need bidirectional understanding. GPT sees only left context.

For tasks like "fill in the blank in the middle of.

A sentence," an encoder-only model like BERT is superior because it sees both sides.

  1. Overlooking inference cost. Encoder-decoder models need both the encoder pass AND sequential decoder generation.

For real-time translation, this latency can be unacceptable.

streaming decoder-only models may be preferred.

Recap. Encoder-only for understanding, decoder-only for generation, encoder-decoder for transformation.

The choice is determined by one question: is your output a label, a continuation, or a transformation of a separate input?

Bridge. Now that we know which architecture to use, we need to understand how these models are trained before fine-tuning.

Section 16.6 covers pre-training for encoder-only models.

Real-World Connection. BERT (encoder-only) powers Google Search's query understanding.

GPT-4 (decoder-only) powers ChatGPT. Google Translate uses encoder-decoder transformers. The architecture choice is not academic.

it determines what the model physically can and cannot do. An interesting boundary case.

T5 treats every NLP task as text-to-text, forcing classification into a generation format ("sentiment: positive"). This blurs the line.

but T5 is still encoder-decoder under the hood.

---

16.6 Pre-training Encoder-Only Models — Masked Language Modeling

Hook. How do you teach a model to understand language without giving it labeled data? You give it a book, randomly erase 15% of the words, and ask: "What was here?" The model learns grammar, facts, and reasoning, all from the blank-filling game. This is masked language modeling, and it is how BERT learned to read.

16.6.1 The Masked Language Model (MLM) Objective

Intuition + Analogy. Your teacher hands you a paragraph with words blacked out.

"The [MASK] landed on the runway, its engines roaring." To fill in the blank, you use the surrounding words.

"landed," "runway," "engines." You infer "airplane." BERT learns the same way, but across billions of sentences.

The masking forces it to understand context, not just memorize keywords.

Formalize. MLM is a self-supervised pre-training objective:

  1. Masking: Randomly select 10-30% of tokens in each input sequence. Replace each selected token with:
  • [MASK] token (80% of the time)
  • A random token (10% of the time)
  • The original token unchanged (10% of the time)
  1. Prediction: The encoder processes the entire masked sequence bidirectionally. For each masked position, it outputs a probability distribution over the vocabulary:

  1. Loss: Cross-entropy between the predicted distribution and the true original token, summed over all masked positions.

The 80-10-10 split has a specific purpose.

if every masked position were [MASK], the model would never see [MASK] during fine-tuning (since downstream tasks don't use it).

The 10% random replacement forces the model to not blindly trust every token.

it must check context. The 10% unchanged keeps the representation aligned with real tokens.

Worked Example — MLM on a Sample Sentence.

Original sentence: "the movie was incredibly boring and poorly acted"

Position 1 2 3 4 5 6 7 8
Original the movie was incredibly boring and poorly acted
After masking (15% ≈ 1-2 tokens) the movie was [MASK] boring and poorly acted

The encoder sees: [CLS] the movie was [MASK] boring and poorly acted

The model's task: predict "incredibly" at position 4 using bidirectional context. It sees "was" (left) and "boring" (right), and knows the sentence expresses negative sentiment. It outputs probabilities over the full vocabulary. The correct token "incredibly" should receive the highest probability. Even if the model predicts "very" instead of "incredibly" (a semantically similar word), the loss is modest. The model still learned that an intensifying adverb fits in that slot.

Sense-check: The model cannot just memorize "incredibly boring" as a phrase — on other training iterations, a different word (like "boring" itself) might be masked instead. The model must learn the abstract relationship: negative adjectives follow intensifiers.

16.6.2 Why MLM Works

Formalize — Two Key Benefits.

  1. Robustness to missing words: An MLM-trained model handles imperfect input.

spelling mistakes, missing words, unusual phrasing. It learned semantics from the full context, not from individual keywords.

If a sentence reads "The movie was [MASK]," the model still outputs positive/negative sentiment correctly by reading the surrounding words.

  1. Prevents shortcut learning: Without masking, the model might latch onto one diagnostic word.

"difficult" always meaning negative sentiment. With 10-30% masking, many training instances hide that word.

The model must use the broader sentence structure. It learns deeper, more transferable features instead of brittle keyword associations.

16.6.3 The CLS Token

Formalize. A special [CLS] token is prepended to.

The start of every input sequence. Through bidirectional self-attention, every token (including [CLS]) attends to every other token across all encoder layers.

After processing through all layers, the output vector at the [CLS] position carries a fixed-size embedding that aggregates the entire sequence.

For classification tasks, this [CLS] output goes through a softmax classifier:

The [CLS] token has no inherent meaning.

it is a neutral token that learns to accumulate information through training. For.

The input "a very difficult product to use," the sequence becomes [CLS] a very difficult product to use.

The [CLS] embedding at the output layer carries the global sentiment signal.

16.6.4 Padding Masks

When a model expects fixed-length input (e.g., 512 tokens), shorter sequences get padding tokens appended at the end.

These padding positions use a special attention mask that sets their attention weights to zero.

the model ignores them during computation. Padding masks are separate from [MASK] tokens.

padding means "this position is empty, ignore it"; [MASK] means "predict what was here."

  • BERT (Bidirectional Encoder Representations from Transformers).

the original, available in BERT-base (110M params, 12 layers) and BERT-large (340M params, 24 layers).

  • RoBERTa — a robustly optimized BERT variant with more data, longer training, and no next-sentence-prediction objective.
  • DistilBERT — a distilled version that retains 95% of BERT's performance with 40% fewer parameters.

16.6.6 Student Questions and Answers

Q: What if the most important word — the one carrying the sentiment — gets masked? Would the model fail?

A: It would not fail because you do millions of training iterations with different random masks each time. On some iterations the important word is masked.

on others it is not.

Across epochs, the model sees the same sentence with many different masking patterns..

It learns to rely on holistic context, not just one keyword. Also, real sentences tend to be longer with many words contributing to meaning.

the model aggregates evidence from all of them.

Q: Could the CLS token be applied directly at the softmax layer instead of being prepended at the input?

A: No. The CLS token must be present from.

The very first layer. It propagates through every encoder layer alongside the real tokens.

At each layer, it attends to and accumulates information from.

All other tokens. If you added it only at the softmax, it would have no accumulated context.

it would be an untrained, meaningless vector. Added at the input, its representation evolves layer by layer.

By the output layer, it truly represents the whole sequence.

Pitfalls — Common Traps.

  1. Confusing [MASK] with padding tokens. They are completely different: [MASK] is a prediction target; padding tokens are placeholders for empty positions.
  2. Thinking MLM only works when the exact word is predicted..

The model learns useful representations even when it predicts the wrong word.

the gradient from the error still teaches it about context and word relationships.

  1. Not understanding the 80-10-10 split. If you only ever show [MASK],.

The model will not know what to do with real tokens during fine-tuning.

The 10% random and 10% unchanged tokens bridge the pre-training/fine-tuning gap.

Recap. MLM pre-trains encoders by masking random tokens and predicting them from bidirectional context.

The [CLS] token aggregates sequence-level information. The result is a model that deeply understands language and transfers to any classification task.

Bridge. Encoders learn by filling blanks. Decoders learn by predicting the next word.

Section 16.7 covers the autoregressive pre-training that powers GPT and other generative models.

Real-World Connection. BERT's MLM pre-training was trained on BooksCorpus (800M words) and English Wikipedia (2.5B words).

The cost: about \$7,000 in cloud compute for BERT-base in 2018 (4 TPUs for 4 days). Today,.

A fine-tuned BERT model can classify sentiment, extract answers, or tag entities on a laptop in minutes.

the pre-training cost is amortized across millions of downstream uses. Google uses BERT in nearly every search query processed worldwide.

---

16.7 Pre-training Decoder-Only Models — Autoregressive Task

Hook. Give a model one word.

"The." It predicts "cat." Feed back "The cat." It predicts "sat." Feed back "The cat sat.".

It predicts "on." This simple loop, repeated billions of times across the entire internet, is how GPT learned to write.

16.7.1 Autoregressive Pre-training

Intuition + Analogy. Imagine you are learning a language by reading a massive library, one sentence at a time.

After each word, you pause and guess.

The next one. If you are wrong, you adjust your mental model of how the language works. After reading millions of books this way, you can complete any prompt.

not because you memorized answers, but because you learned the patterns of language. This is autoregressive pre-training.

Formalize. Decoder-only models use a causal language modeling (CLM) objective.

Given a sequence of tokens , the model predicts each token using only the tokens before it:

At each position , the model outputs a probability distribution over the vocabulary.

The loss is the sum of cross-entropy losses across.

All positions. Masked self-attention (causal masking) ensures position never sees tokens

The key difference from MLM: autoregressive models predict every token (not just masked ones) and use only left context (not bidirectional).

This makes them natural generators.

during inference, they can continue any prefix by repeatedly predicting the next token.

  • GPT (Generative Pre-trained Transformer) — the series from GPT-1 (117M params) through GPT-4 (estimated 1.7T+), all decoder-only autoregressive models.
  • ChatGPT — GPT fine-tuned with RLHF (Reinforcement Learning from Human Feedback) for conversational interaction.
  • Claude — Anthropic's decoder-only model family focused on safety and helpfulness.
  • LLaMA (Meta) — open-source decoder-only models available in 7B, 13B, 33B, 65B parameter sizes.

16.7.3 Use Cases

Any task where you give a prompt and want generated text: chatbots, creative writing, code generation, dialogue systems, content summarization, and question answering with generated answers.

Pitfalls — Common Traps.

  1. Confusing autoregressive pre-training with MLM. MLM uses bidirectional context and masks random tokens.

Autoregressive uses only left context and predicts every next token. They produce fundamentally different models.

one understands, one generates.

  1. Hallucination. Decoder-only models confidently generate false information because the autoregressive objective optimizes for plausible continuation, not factual accuracy.

A model trained to "predict the next word" has no built-in truth-checking mechanism.

  1. Repetition loops. Without careful decoding strategies, autoregressive models can get stuck in repetitive cycles.

This happens because the model's own outputs reinforce the same high-probability tokens.

Recap. Autoregressive pre-training teaches decoders to predict the next token from previous tokens.

The objective is the sum of next-token cross-entropy losses. These models are the foundation of all modern chatbots and text generators.

Bridge. Having covered encoder-only (MLM) and decoder-only (autoregressive) pre-training, we now look at the combined approach: pre-training encoder-decoder models.

That is Section 16.8.

Real-World Connection. GPT-3 was trained on about 500 billion tokens from Common Crawl, WebText2, Books1/2, and Wikipedia.

The pre-training cost was estimated at \$4.6M-\$12M in compute. The key insight.

the autoregressive objective is so simple that.

It scales to any text data without any labeling. Every word on the internet becomes a training example automatically.

the task is always "predict the next word."

---

16.8 Pre-training Encoder-Decoder Models

Hook. Take a clean document. Scribble on it, tear out a paragraph, shuffle the sentences.

Now hand the mess to a model and say: "Rebuild the original." This game.

corrupting and reconstructing — is how encoder-decoder models like BART and T5 learn to transform sequences.

16.8.1 Sequence-to-Sequence Pre-training

Formalize. Encoder-decoder pre-training corrupts the input, encodes it, and decodes the reconstruction. Common objectives include:

  • Text infilling: Remove spans of text; the decoder regenerates them.
  • Denoising: Apply random noise (deletions, masking, shuffling); reconstruct the clean version.
  • Translation: One language → another (when parallel corpora exist).

16.8.2 BART and the Denoising Autoencoder

Intuition + Analogy. BART is like an archivist who receives water-damaged documents.

The encoder reads the smudged, incomplete version. It produces a context.

a mental model of what the document probably said. The decoder then writes out a clean, restored copy.

The archivist gets better by comparing the restored copy against the original undamaged document stored in the archive.

Formalize. BART uses a denoising autoencoder objective:

  1. Take original input . Corrupt it: randomly delete tokens, mask spans, permute sentence order.
  2. Pass corrupted through the encoder → context .
  3. Decoder autoregressively generates .
  4. Loss: cross-entropy between and the original, uncorrupted .

The original input serves as its own label.

this is self-supervised. No human annotation is needed. BART's corruptions are more aggressive than BERT's (span masking, sentence shuffling, deletion), forcing the decoder to learn stronger generative abilities.

Worked Example — BART Denoising.

Original input : "The quick brown fox jumps over the lazy dog."

Corrupt: Delete "quick" and "lazy", mask the span "jumps over".

Corrupted : "The [MASK] brown fox [MASK] [MASK] the [MASK] dog."

Encoder: Processes the corrupted text bidirectionally → produces context .

Decoder: Autoregressively generates from :

  • Step 1: "The"
  • Step 2: "quick"
  • Step 3: "brown"
  • ...
  • Final: "The quick brown fox jumps over the lazy dog."

Loss: Cross-entropy between generated tokens and the original . The model is penalized for every wrong token. Over millions of such training examples, it learns to restore missing words, reorder sentences, and fill spans.

Sense-check: The more aggressive corruptions (whole span deletion) force BART's decoder to generate multiple coherent words, unlike BERT which only predicts single masked tokens. This is why BART excels at generation tasks like summarization.

16.8.3 T5 and MT5

  • T5 (Text-to-Text Transfer Transformer).

reformulates every NLP task as text generation. Input: "translate English to French:.

The cat sits." Output: "Le chat est assis." Same architecture handles translation, summarization, QA, and classification — all as text-to-text.

  • MT5 — multilingual T5 covering 101 languages.

16.8.4 Example Use Cases for Encoder-Decoder

  • Machine translation: Source language → target language.
  • Summarization: Long document → short summary.
  • Abstractive QA: Passage + question → generated answer in natural language.
  • Structured data to text: Database records → human-readable report.
  • Image captioning: Image (via vision encoder) → descriptive sentence.

Pitfalls — Common Traps.

  1. Using cross-entropy loss when the professor says MSE. The professor's verbal description mentions "mean squared error loss" for BART's denoising.

In practice, BART and T5 use cross-entropy loss over the vocabulary..

The professor may be using MSE as a simplified illustration. On an exam, know the professor's phrasing.

  1. Assuming all encoder-decoder models use the same pre-training. BART uses denoising, T5 uses span corruption, and some use translation.

The pre-training objective shapes what the model is best at downstream.

  1. Forgetting that encoder-decoder models are slower to train than encoder-only or decoder-only..

They have more parameters (both encoder AND decoder weights) and need both passes.

Recap. Encoder-decoder models pre-train by corrupting input and reconstructing it (BART) or by casting every task as text-to-text (T5).

These models excel at sequence transformation tasks.

Bridge. Pre-training gives us powerful base models. But how do we adapt them to our specific task with limited data? Section 16.9 covers transfer learning and fine-tuning.

Real-World Connection. BART powers Facebook/Meta's content moderation and summarization pipelines.

T5's text-to-text paradigm is elegant: it means a single model checkpoint can do translation, summarization, and classification.

you just change the input prefix..

This "one model, many tasks" approach has influenced prompt engineering in GPT-style models, where the instruction itself becomes the task specification.

---

16.9 Transfer Learning and Fine-Tuning for Transformers

Hook. Training GPT-3 from scratch costs millions of dollars. But you can adapt it to your company's customer support logs for under \$100.

The trick: freeze the giant model and train only a tiny add-on.

a technique called LoRA.

16.9.1 Why Fine-Tune Instead of Training from Scratch

Formalize. Transfer learning reuses a pre-trained model's weights as a starting point.

The decision of how many layers to update depends on three factors:

  1. Task similarity: How different is your task from pre-training? Text classification is close to MLM.

code generation is further from language modeling.

  1. Data size: More labeled data → you can update more layers without overfitting.
  2. Compute budget: Full fine-tuning updates all parameters (e.g., 175B for GPT-3). Parameter-efficient methods update far fewer.

Rule of thumb:

  • Closely related task + small data → freeze most layers, fine-tune only the top few.
  • Very different task + large data → full fine-tuning (update all parameters).
  • Always budget-constrained → use LoRA or similar parameter-efficient methods.

16.9.2 Low-Rank Adaptation (LoRA)

Intuition + Analogy. You borrow a friend's detailed recipe book (the pre-trained model). You want to adapt it to your kitchen's ingredients.

Instead of rewriting every page, you stick a small Post-it note on each recipe. The note says.

"Substitute olive oil for butter" or "Add 5 more minutes baking time." The original book stays untouched. The Post-it notes.

small, cheap, easy to swap — are LoRA adapters.

Formalize. LoRA freezes the original pre-trained weights and adds a trainable low-rank update:

where the weight update is factorized as:

  • is the rank — a small number like 4, 8, or 16.
  • is initialized with small random values; is initialized to zero (so training starts at the pre-trained model's behavior).
  • Only and are trained. stays frozen.

Why low rank works: The weight updates needed for fine-tuning have low "intrinsic dimension".

only a few directions in weight space need to change. The constraint enforces this.

A full update has parameters; LoRA has parameters. For .

full update = 16.8M params; LoRA = 131K params — a 128× reduction.

16.9.3 Benefits of LoRA

  • Reduced memory: Only small adapter matrices are stored and updated. A single GPU can fine-tune a 70B model.
  • Faster training: Fewer parameters to optimize → fewer gradient computations.
  • Multi-task serving: Keep one base model. Swap in different LoRA adapters for different tasks.

no need to reload the full model.

  • Interpretability: tells you exactly what changed from the base model. The singular vectors of reveal which feature directions were most important for your task.

16.9.4 Symbol Registry — LoRA

  • — original pre-trained weight matrix —
  • — weight update —
  • — first low-rank matrix —
  • — second low-rank matrix —
  • — rank — scalar, (e.g., 4, 8, 16)
  • — hidden dimension — scalar
  • — input vector —
  • — output —

Pitfalls — Common Traps.

  1. Setting rank too high. or with LoRA approaches full fine-tuning cost.

Start with or .

these values work well for most NLP tasks.

  1. Applying LoRA to the wrong layers. LoRA is typically applied to the attention weight matrices ().

Applying it to all layers may not improve results and costs more.

  1. Thinking LoRA is always enough. For tasks radically different from pre-training (e.g., fine-tuning a language model on protein sequences), full fine-tuning may still be needed.
  2. Forgetting that LoRA adapters are task-specific. An adapter trained for sentiment analysis will not help with translation.

Each task needs its own adapter.

Recap. Transfer learning with LoRA freezes the pre-trained model and trains only small low-rank adapter matrices ().

This achieves near full-fine-tuning quality at a fraction of the cost.

Bridge. Transformers are not just for text. Section 16.10 shows how the same architecture.

with a clever input trick — handles images, opening the door to vision transformers.

Real-World Connection. LoRA is the dominant fine-tuning technique in the open-source LLM community.

Fine-tuning LLaMA-7B with full parameters requires ~56GB GPU memory; with LoRA (r=16), it fits in ~16GB.

a single consumer GPU. Companies deploy one base LLaMA model and hundreds of task-specific LoRA adapters.

When a user asks a medical question, the medical LoRA adapter is activated. When.

They ask about code, the coding adapter takes over. Same base model, infinite specializations.

---

16.10 Vision Transformers

Hook. What if you could take the transformer.

designed for words — and feed it an image instead? The trick.

chop the image into little squares, treat each square as.

A "word," and let attention figure out which squares matter most. No convolutions needed.

16.10.1 Image Patches as Tokens

Intuition + Analogy. You have a jigsaw puzzle. Each piece shows a fragment of the big picture.

To understand the whole scene, you must look at how pieces relate.

the blue piece (sky) sits above the green piece (grass), the brown piece connects to the trunk.

A Vision Transformer (ViT) works the same way. It slices.

The image into patches (puzzle pieces) and uses self-attention to discover how they fit together.

Formalize. ViT replaces convolutions with pure attention:

  1. Divide the input image of size into a grid of patches.
  2. Flatten each patch into a vector of length .
  3. Linearly project each flattened patch to dimension .
  4. The patch embeddings form the input sequence — exactly like word embeddings in NLP.

For a image with patches: there are patches.

Each patch is a -dimensional vector, projected to (e.g., 768 or 1024). The sequence length is 196.

the model attends to all 196 patches simultaneously.

16.10.2 Positional Embedding for Patches

Since transformers have no spatial awareness, each patch gets a positional embedding.

In ViT, positional embeddings are typically learned (not fixed sinusoidal). Patch one learns embedding vector one.

patch two learns embedding vector two. These are added to the patch embeddings before the encoder.

Through training, the model learns that patches with similar positional embeddings are spatially close.

16.10.3 ViT for Image Classification

For classification, ViT uses an encoder-only architecture:

  1. Patch embeddings + learned positional embeddings + [CLS] token → encoder stack.
  2. Encoder applies multi-head self-attention across all patches. The model learns which regions matter.

the dog's face gets more attention than the background grass.

  1. After encoder blocks, the [CLS] token's output goes through an MLP head + softmax → class prediction.

16.10.4 ViT for Image Captioning

For generation tasks, ViT becomes an encoder-decoder:

  • ViT encoder processes image patches → produces patch-level context vectors.
  • Text decoder attends to the encoder output via cross-attention → generates the caption word by word.
  • Architecture choice depends on the task: encoder-only for classification, encoder-decoder for captioning or visual QA.

16.10.5 Loss of Locality in ViT

Formalize — ViT vs. CNN.

CNNs use local receptive fields.

each neuron sees a small neighborhood of pixels. This builds in a strong inductive bias.

nearby pixels are related. ViT has no such bias. Every patch attends to every other patch from the first layer. Spatial relationships must be learned from data through positional embeddings.

Trade-off:

  • CNNs: strong locality prior → sample-efficient (good with small datasets), but may miss long-range dependencies.
  • ViT: no locality prior → less sample-efficient (needs more data to learn spatial patterns), but excels at long-range relationships from the start.

This is why ViT outperforms CNNs on large datasets (ImageNet-21k, JFT-300M) but CNNs still win on small datasets.

The inductive bias of convolutions is valuable when data is scarce.

16.10.6 Student Questions and Answers

Q: If the task is to take an image and write three sentences about it, what should the architecture be?

A: The output of the ViT encoder feeds into a decoder.

The decoder generates the sequence of words that forms the sentences. You pick the architecture based on the task.

not on the input modality. For image classification.

encoder-only is enough. For image captioning or visual question answering: you need encoder-decoder. Vision transformers are not limited to encoder-only architectures.

they provide the encoder, and you add the decoder when generation is needed.

Pitfalls — Common Traps.

  1. Using ViT on tiny datasets. ViT needs large datasets because it lacks the locality inductive bias of CNNs.

Training ViT on 5000 images will likely underperform a simple CNN.

  1. Forgetting positional embeddings. Without positional embeddings, the model treats patches as an unordered bag.

it cannot tell if a patch is in the top-left or bottom-right corner.

  1. Assuming patch size doesn't matter. Smaller patches = more tokens = longer sequences = quadratic attention cost.

patches on a image give 256 tokens. patches give 784 tokens.

nearly 10× the attention compute.

Recap. Vision Transformers treat image patches as tokens and apply the same transformer encoder. The key trade-off.

ViT lacks CNN's locality inductive bias, so it needs more data but excels at global relationships.

Architecture choice (encoder-only vs. encoder-decoder) depends on the output type, not the input type.

Bridge. Having covered transformers for both text and vision, Section 16.11 looks ahead at where the field is going.

multimodal models that handle text, images, and audio together.

Real-World Connection. ViT, introduced by Google Research in 2020 (Dosovitskiy et al.), matched or beat state-of-the-art CNNs on ImageNet while requiring fewer computational resources to train.

Today, ViT variants power image search in Google Photos, medical image analysis (detecting tumors in CT scans), and autonomous vehicle perception systems. The patch-as-token trick has been extended to video (tubelets as tokens), 3D point clouds (point patches), and audio (spectrogram patches).

---

16.12 Introduction to Optimization

Hook. You have a model with 175 billion knobs (parameters). You need to find the one setting of those knobs that minimizes error across terabytes of data.

You cannot try every combination.

there are more combinations than atoms in the universe. The solution: follow the slope downhill, one small step at a time.

This is optimization, and every deep learning model depends on it.

16.12.1 Why Optimization Matters

Intuition + Analogy. You are blindfolded on a mountain.

Your goal is to reach the lowest valley. You can only feel the slope under your feet.

steep downhill, gentle slope, or flat ground. You take a step in the steepest downhill direction. Feel again. Step. Repeat.

If your steps are too big, you might jump across the valley entirely. Too small, and you may never reach.

The bottom before nightfall. The learning rate is your step size. The mountain is the loss surface. The blindfolded hiker is the optimizer.

Formalize. Optimization in deep learning seeks weight values that minimize a loss function :

The standard approach is gradient descent:

where (eta) is the learning rate.

the step size. is the gradient.

the vector of partial derivatives telling us which direction increases the loss fastest. We move opposite the gradient to decrease the loss.

The quality of the final model depends on:

  • The shape of the loss surface (convex vs. non-convex, smooth vs. jagged)
  • Hyperparameter choices (learning rate, decay, momentum coefficient)
  • Weight initialization (where you start on the mountain)
  • The optimization algorithm variant (how you process data per update)

16.12.2 The Loss Surface and Its Complexity

The loss function as a function of the weights is rarely a simple convex bowl. It can have:

  • Multiple local minima — dips that trap gradient descent.
  • Saddle points — flat regions where the gradient is zero but it is not a minimum.
  • Plateaus — large flat areas where gradients are near zero, stalling progress.
  • Narrow valleys — steep in some directions, flat in others, causing zigzag paths.

The search starts at some initial point (determined by weight initialization) and must navigate this complex terrain to reach a good minimum.

16.12.3 Symbol Registry — Learning Rate

  • — learning rate — controls step size — scalar, typically 0.1, 0.01, 0.001
  • — weight vector at step
  • — cost/loss function — scalar
  • — gradient — vector of partial derivatives of with respect to each weight

16.12.4 Gradient Descent Variants

Formalize — Three Fundamental Variants.

Batch Gradient Descent: Uses all training instances per update. Trajectory is smooth.

steady movement toward the minimum. But each update processes the entire dataset, making it slow in wall-clock time. Smoothness: excellent. Speed: slow.

Mini-batch Gradient Descent: Uses a random batch of size (e.g., ).

The trajectory oscillates because each batch is a noisy estimate of.

The true gradient. Some batches pull the weights away from the minimum.

others pull back. Smoothness: moderate. Speed: faster per update.

Stochastic Gradient Descent (SGD): Uses exactly one random instance per update. Time per update is .

extremely fast. But the trajectory is the noisiest. A single outlier instance can push weights wildly off course.

SGD may skip past the global minimum entirely. Needs many more iterations overall, but each is cheap.

Worked Example — Comparing Variants on 1000 Instances.

Dataset: 1000 samples. Batch size . Learning rate .

Variant Instances per update Updates per epoch Wall-clock time per epoch Trajectory
Batch 1000 1 Slowest Smooth
Mini-batch 100 10 Medium Oscillating
SGD 1 1000 Fastest (per update) Very noisy

Sense-check: Mini-batch (10 updates per epoch) strikes the best balance for most deep learning.

It is fast enough to handle large datasets and noisy enough to escape local minima, but not so noisy that it never converges.

16.12.5 Understanding Contour Plots

Visual Intuition. Picture a loss function of two weights, and .

The surface looks like a bowl if the function is convex. Looking from above, you see contours.

concentric rings. Each ring is a set of values that produce the same loss.

  • Outermost ring: highest loss.
  • Each inner ring: progressively lower loss.
  • Innermost point: global minimum — yielding the lowest possible loss.

The optimizer traces a path across these rings. Each dot on the path is after one update.

Convergence means the dots move from outer rings to the innermost region..

The path can be smooth (batch), oscillating (mini-batch), or erratic (SGD) depending on the variant.

16.12.6 Student Questions and Answers

Q: What do the contours represent? Are they epochs?

A: The concentric loops are not epochs. They are contours of the loss function.

each ring is a set of values that produce the same loss. The outermost contour has the maximum loss.

The innermost point has the minimum loss. The path traced across contours shows the optimizer's trajectory..

Each dot is after one update. Convergence means the dots move from outer rings to the innermost region.

Q: So optimization is about managing the learning rate — should we not keep it constant?

A: Yes, adapting the learning rate is one key part of optimization..

A constant learning rate may be too high (overshooting the minimum) or too low (convergence is too slow).

Adaptive methods like Adam and RMSProp vary the learning rate per parameter during training.

Q: Is optimization also about making the model generalize better?

A: Yes. A model should neither overfit nor underfit. Good optimization helps reach a minimum that generalizes.

not just a training-set minimum. But many factors beyond optimization affect generalization: cost function choice, data quality, regularization, and hyperparameters.

16.12.7 The Learning Rate Size Effect

With a fixed learning rate:

  • Too high (): The search overshoots, oscillates wildly, may miss the minimum entirely. Like taking 10-meter steps down a narrow valley.

you jump from one wall to the other.

  • Too low (): The search moves in tiny steps. Convergence is extremely slow. It may never reach the minimum within the training budget.
  • Just right: Steady progress toward the minimum without wild oscillations or excessive slowness.

Even with an optimal fixed learning rate, mini-batch or stochastic variance introduces oscillations. This is where momentum and adaptive learning rates help.

16.12.8 Momentum

Intuition + Analogy. You are cycling down a hilly road. On.

A steep slope, gravity pulls you down naturally. When you hit a flat patch or a small uphill bump, your speed from the previous downhill carries you through.

That carried-forward speed is momentum. Without.

It, you would stop on every small bump. Gradient descent stops at every local minimum. Momentum rolls through them.

Formalize. Momentum keeps a running average of past gradients and uses it to smooth and accelerate the weight update.

The professor's formulation — the one you should know for the exam:

where:

  • is the velocity (accumulated gradient history)
  • is the momentum coefficient — how much of the past velocity to retain (e.g., )
  • scales the current gradient contribution
  • is the learning rate

Relationship to the standard form: Standard texts (e.g., the Deep Learning book by Goodfellow et al.) write momentum as:

The professor's form is equivalent but uses a different sign convention and normalization. In the professor's version.

the negative sign is in the weight update ().

The factor normalizes the gradient so that stays as a weighted average bounded by the gradient magnitude. In.

The standard form, grows without bound if gradients are consistent, and the learning rate controls the overall scale. Both forms describe the same algorithm.

the professor's version emphasizes the exponential moving average interpretation.

Why momentum helps: At a local minimum, ..

A pure gradient descent would stop. But momentum carries forward the accumulated past gradients ( is non-zero).

This accumulated push can propel the search out of the local trap toward.

A better minimum. The velocity acts like a heavy ball rolling through small dips.

Worked Example — Momentum Escaping a Local Minimum.

Suppose the gradient history was strongly downhill for 100 steps ( each step). Then it hits a shallow local minimum where .

Without momentum (): .

Update: . Stuck.

With momentum (, ):

Sense-check: Even though the gradient is zero at this position, the accumulated velocity produces a step of .

The optimizer keeps moving. Over several more steps, the velocity decays as , eventually slowing to zero.

but by then, it may have escaped the local minimum and found a real downhill slope.

16.12.9 Adaptive Learning Rate

Formalize. Rather than a single fixed learning rate, adaptive methods adjust per parameter or over time:

  • Adagrad: Accumulates squared gradients.

divides learning rate by the square root of.

This sum. Parameters with large gradients get smaller updates; parameters with small gradients get larger updates.

  • RMSProp: Uses a leaky (exponential) average of squared gradients instead of a full sum, preventing the learning rate from decaying to zero.
  • Adam: Combines momentum (leaky average of gradients) with RMSProp (leaky average of squared gradients), plus bias correction.

Momentum provides acceleration — faster convergence. Adaptive learning rates provide stability — smoother convergence.

Together they form the basis of modern optimizers like Adam, which is the default choice for most transformer training.

Pitfalls — Common Traps.

  1. Learning rate too high. The most common optimization failure.

The loss explodes to NaN. Start small ( or ) and increase if training is stable.

  1. Confusing momentum coefficient with learning rate . They control different things: controls step size.

controls how much history is remembered. Common values: or .

  1. Thinking SGD is always worse than Adam. SGD with momentum can generalize better than Adam on some tasks (especially image classification).

Adam converges faster but may find sharper minima that generalize worse. This is an active research topic.

  1. Ignoring the scale of gradients. If gradients are very small (vanishing gradient problem), momentum alone will not help.

the accumulated velocity will be tiny too. You may need better weight initialization or architecture changes (e.g., residual connections, LayerNorm).

Recap. Optimization finds the best weights by following the negative gradient downhill. Batch GD is smooth but slow.

mini-batch GD balances speed and noise; SGD is fast but noisy. Momentum carries past gradients forward to escape local minima.

Adaptive learning rates (Adam, RMSProp) adjust step sizes per parameter. Together, these techniques enable training models with billions of parameters.

Exam note: The examination may require you to connect optimization concepts from DNN with those from Mathematical Foundations for Machine Learning and Machine Learning courses.

Know the gradient descent variant comparison, momentum intuition and formulas, and adaptive learning rate motivation. The recorded lectures and prescribed materials are enough for exam preparation.

Real-World Connection. Adam is the default optimizer for training virtually every large transformer.

GPT, BERT, LLaMA all use Adam or AdamW (Adam with decoupled weight decay). The choice of optimizer hyperparameters can make the difference between a model that converges in 3 days versus 3 weeks on.

A cluster of 1000 GPUs. At the scale of GPT-3 training (\$5M+ compute), even a 10% efficiency improvement from better optimization saves hundreds of thousands of dollars. Learning rate schedules (cosine decay, warmup, linear decay) are standard practice.

the learning rate is typically increased linearly for a few thousand steps (warmup) then decayed, rather than kept constant.

---

Exam Guidance Summary

Exam note: The examination may require you to connect concepts from multiple courses.

Deep Neural Networks, Mathematical Foundations for Machine Learning, and Machine Learning.

Ensure you understand the relationships across subjects. The recorded lecture content and prescribed materials are enough for exam preparation.

no external resources are needed.

Key topics to prepare:

  1. Encoder vs. decoder vs. encoder-decoder architecture selection. Know when to use each.

The decision tree: output is a label → encoder-only. Output is generated continuation of.

A prompt → decoder-only. Output is a transformation of a separate input → encoder-decoder.

  1. Masked attention rationale. Understand why causal masking is necessary in decoder self-attention.

it prevents the decoder from peeking at future tokens during training, which would cause it to cheat and fail at inference.

Know the mask formula: if , if .

  1. Cross attention mechanics. Know that queries come from the decoder's current state, keys and values come from the encoder's output.

This is how the decoder accesses source information during generation.

  1. MLM pre-training objective. Randomly mask 10-30% of tokens.

model predicts the originals using bidirectional context. Know the 80-10-10 split: 80% masked with [MASK], 10% random token, 10% unchanged.

  1. CLS token role. Prepended at input.

its output embedding after all encoder layers encodes a global summary of the sequence, used for classification.

  1. LoRA fine-tuning mechanism. Freeze , learn with low-rank factorization.

Forward pass: . Know the parameter count advantage: vs. .

  1. Gradient descent variant comparison. Batch (all data, smooth, slow), mini-batch (subset, oscillating, faster per update), SGD (single instance, noisy, fastest per update).

Know trade-offs in smoothness vs. speed.

  1. Momentum intuition and formulas. Know the professor's formulation.

, .

Understand why momentum helps escape local minima (accumulated past gradients provide non-zero velocity even when current gradient is zero).

  1. Adaptive learning rate motivation. Fixed learning rates cannot handle parameters with different gradient scales.

Adaptive methods (Adam, RMSProp) adjust the learning rate per parameter.

The optimization content in this session is drawn from Mathematical Foundations for Machine Learning.

You are expected to know nonlinear optimization concepts from that parallel course, including Adam, RMSProp, and momentum variants.

---

Key Industry Applications

The architectures and techniques covered in this lecture power the vast majority of modern AI systems deployed in production today.

Encoder-Only Models:

  • BERT (Google, 2018) — powers Google Search query understanding, document ranking, and featured snippets across billions of daily queries. Available in BERT-base (110M parameters, 12 layers) and BERT-large (340M, 24 layers).
  • RoBERTa (Meta, 2019) — an optimized BERT variant trained on more data with better hyperparameters. Widely used for text classification, sentiment analysis, and content moderation.
  • DistilBERT (Hugging Face, 2019).

a distilled version retaining 95% of BERT's performance with 40% fewer parameters. Used in latency-sensitive production systems and edge devices.

Decoder-Only Models:

  • GPT-1 through GPT-4 (OpenAI, 2018-2023).

autoregressive models scaling from 117M to.

An estimated 1.7T+ parameters. GPT-3.5 and GPT-4 power ChatGPT, GitHub Copilot, and the OpenAI API serving millions of developers.

  • Claude (Anthropic, 2023) — a decoder-only model family focused on safety and constitutional AI, used in enterprise chat and document analysis.
  • LLaMA (Meta, 2023) — open-source decoder-only models (7B, 13B, 33B, 65B, 70B parameters). Available for research and commercial fine-tuning, powering the majority of open-source LLM applications.

Encoder-Decoder Models:

  • T5 (Google, 2019) — treats every NLP task as text-to-text. Used in Google's internal systems for translation, summarization, and question answering.
  • MT5 (Google, 2020) — extends T5 to 101 languages for multilingual applications.
  • BART (Meta, 2019) — denoising autoencoder for text generation and summarization. Powers Facebook's content understanding and abstractive summarization pipelines.

Vision Transformers (ViT):

  • Image classification at scale (ImageNet-21k, JFT-300M), object detection, semantic segmentation, medical image analysis, and autonomous vehicle perception.

Fine-Tuning Techniques:

  • LoRA (Microsoft, 2021) — the dominant parameter-efficient fine-tuning method. Enables fine-tuning of 70B+ models on consumer GPUs. Adapters can be swapped at runtime for multi-task serving. Used by virtually every open-source LLM deployment.

Emerging:

  • Sparse attention — active research for processing documents with 100K+ tokens (entire books, legal documents, genomic sequences).
  • Multimodal transformers — GPT-4V, Google Gemini, Meta ImageBind — combining text, image, and audio understanding in unified architectures.

---

DNN Lecture 16 notes · Transformer Architectures and Optimization

Deep Neural Networks· postgraduate· 2026-07-15

Sections Breakdown

1Encoder and Decoder — Purpose and Intuition
2Attention Mechanisms — Recap
3Encoder Architecture Components
4Decoder Architecture Components
5Architecture Selection
6Pre-training Encoder-Only Models — Masked Language Modeling
7Pre-training Decoder-Only Models — Autoregressive Task
8Pre-training Encoder-Decoder Models
9Transfer Learning and Fine-Tuning for Transformers
10Vision Transformers
11Emerging Trends
12Introduction to Optimization
13Exam Guidance Summary
14Key Industry Applications
Postgraduate students in Deep Neural Networks

Exam Revision Notes

Below is the distilled, exam-ready core of this lecture. Every entry is built from the full textbook notes above. Use this section for rapid review — but if something doesn't make sense, go back to the full explanation in the main content.

Below is the distilled, exam-ready core of this lecture. Every entry is built from the full textbook notes above. Use this section for rapid review — but if something doesn't make sense, go back to the full explanation in the main content.

Encoder vs Decoder

Must-know: An encoder reads input and produces context vectors for understanding. A decoder generates new output token by token using autoregressive generation. The architecture choice depends on whether you need understanding, generation, or both.

⚠️ Top pitfall: Confusing encoder output with generated text — the encoder produces vectors, not words. The decoder is what generates text.

Self-check: If you need to classify a sentence as positive or negative, which architecture do you use?

Connects to: Self-Attention, Cross-Attention, Sequence-to-Sequence Models

Self-Attention

Must-know: Self-attention computes a weighted sum of all token values, with weights determined by query-key compatibility. The formula is \text{Attention}(Q,K,V) = \text{softmax}(QK^T/\sqrt{d_k})V.

⚠️ Top pitfall: Forgetting the \sqrt{d_k} scaling factor. Without it, large d_k pushes softmax into near-one-hot territory, causing vanishing gradients.

Self-check: Why does self-attention require positional embeddings?

Connects to: Multi-Head Attention, Positional Encoding, Scaled Dot-Product

Positional Encoding

Must-know: Positional encoding adds a sinusoidal vector to each token embedding so the model can use token order. Without it, self-attention is permutation-invariant.

⚠️ Top pitfall: Thinking positional embeddings are optional — they are mandatory for any transformer using self-attention.

Self-check: What would happen to 'dog bites man' vs 'man bites dog' without positional encoding?

Connects to: Self-Attention, Embedding Layer

Multi-Head Attention

Must-know: Multi-head attention runs h independent attention heads in parallel, each in a reduced dimension d_k = d_model / h. Outputs are concatenated and projected back.

⚠️ Top pitfall: Using multi-head attention but forgetting the output projection W_O — concatenating without mixing means heads cannot share information.

Self-check: Why does multi-head attention cost about the same as single-head attention despite having more parameters?

Connects to: Self-Attention, Scaled Dot-Product Attention

Layer Normalization

Must-know: LayerNorm normalizes each token's feature vector to zero mean and unit variance independently. It uses learnable scale \gamma and shift \beta parameters.

⚠️ Top pitfall: Confusing LayerNorm with BatchNorm. LayerNorm normalizes across features per token; BatchNorm normalizes across the batch per feature.

Self-check: Why is LayerNorm preferred over BatchNorm in transformers?

Connects to: Residual Connections, Pre-LN vs Post-LN

Residual Connections

Must-know: Residual connections add the input of a sub-layer to its output, so the sub-layer only learns the residual. Gradients flow directly through the skip connection, preventing vanishing gradients in deep stacks.

⚠️ Top pitfall: Stacking blocks without residual connections — in deep transformers (12+ layers), the skip path is essential for training.

Self-check: How does a residual connection help gradient flow during backpropagation?

Connects to: Layer Normalization, Encoder Stacking

Masked Self-Attention (Causal Attention)

Must-know: Masked attention prevents the decoder from peeking at future tokens by setting attention scores for future positions to -\infty before softmax. The mask matrix M has M_{ij}=0 if j\leq i and M_{ij}=-\infty if j>i.

⚠️ Top pitfall: Forgetting to mask during training — the model will have perfect training loss but fail at inference because it never learned to generate without peeking.

Self-check: Why can't a decoder-only model (like GPT) use bidirectional attention?

Connects to: Cross-Attention, Autoregressive Generation

Cross-Attention

Must-know: Cross-attention lets the decoder attend to the encoder's output. Queries come from the decoder, keys and values from the encoder. No masking is needed because the encoder output is fully known.

⚠️ Top pitfall: Confusing cross-attention with self-attention — in cross-attention, K and V come from the encoder, not the decoder.

Self-check: In an encoder-decoder translation model, where do the keys and values in cross-attention come from?

Connects to: Encoder-Decoder Architecture, Masked Self-Attention

Architecture Selection

Must-know: Encoder-only for understanding tasks (classification, NER). Decoder-only for generation from a prompt (chat, story writing). Encoder-decoder for sequence transformation (translation, summarization).

⚠️ Top pitfall: Using an encoder-only model for generation — BERT cannot generate coherent text. Using a decoder-only model for fill-in-the-blank — GPT cannot see right context.

Self-check: What architecture would you use for image captioning (image → text)?

Connects to: Encoder, Decoder, Encoder-Decoder

Masked Language Modeling (MLM)

Must-know: MLM pre-trains encoders by randomly masking 15% of tokens and predicting them from bidirectional context. The 80-10-10 split (80% [MASK], 10% random, 10% unchanged) bridges pre-training and fine-tuning.

⚠️ Top pitfall: Confusing [MASK] with padding tokens — [MASK] is a prediction target, padding marks empty positions. Also, not understanding why the 80-10-10 split matters.

Self-check: Why does MLM use 10% unchanged tokens instead of always masking?

Connects to: CLS Token, BERT, Self-Supervised Learning

Autoregressive Pre-training

Must-know: Decoder-only models use causal language modeling: predict every next token using only the tokens before it. The loss is the sum of cross-entropy losses over all positions. Every token on the internet becomes a training example.

⚠️ Top pitfall: Confusing autoregressive pre-training with MLM — one uses left context only and predicts every token; the other uses bidirectional context and predicts only masked tokens.

Self-check: Why does autoregressive training have no masking strategy like MLM's 80-10-10?

Connects to: GPT, Decoder-Only, Causal Masking

LoRA (Low-Rank Adaptation)

Must-know: LoRA freezes pre-trained weights and adds trainable low-rank matrices \Delta W = BA with r \ll d. For d=4096, r=16: full update = 16.8M parameters, LoRA = 131K parameters (128x reduction).

⚠️ Top pitfall: Setting rank r too high (r=64+) approaches full fine-tuning cost. Start with r=8 or r=16. Also, LoRA adapters are task-specific.

Self-check: Why can a single GPU fine-tune a 70B parameter model with LoRA but not with full fine-tuning?

Connects to: Transfer Learning, Fine-Tuning, Parameter-Efficient Methods

Vision Transformers (ViT)

Must-know: ViT treats image patches as tokens — divides H\times W image into P\times P patches, flattens and projects each to d_model. For 224\times224 with 16\times16 patches: 196 patch tokens. ViT lacks CNN's locality inductive bias.

⚠️ Top pitfall: Using ViT on tiny datasets — without locality bias, ViT needs large data (ImageNet-21k+) to match CNN performance.

Self-check: Why does ViT need more data than a CNN to achieve comparable performance?

Connects to: Encoder, Positional Embedding, Image Classification

Gradient Descent Variants

Must-know: Batch GD uses all N instances per update (smooth, slow). Mini-batch GD uses a batch of size B (balanced). SGD uses one instance (noisy, fast). Mini-batch is the standard for deep learning.

⚠️ Top pitfall: Thinking SGD is always worse than Adam — SGD with momentum can generalize better on some tasks (e.g., image classification), even though Adam converges faster.

Self-check: Why does mini-batch GD strike the best balance for most deep learning tasks?

Connects to: Momentum, Adaptive Learning Rate, Learning Rate

Momentum

Must-know: Momentum keeps a running average of past gradients (velocity v_t). It helps escape local minima because accumulated past gradients provide non-zero velocity even when the current gradient is zero.

⚠️ Top pitfall: Confusing momentum coefficient \beta with learning rate \eta. \beta controls how much history is remembered (typically 0.9 or 0.99); \eta controls step size.

Self-check: How does momentum help an optimizer escape a local minimum?

Connects to: Gradient Descent, Adaptive Learning Rate, Optimization

Was this lecture useful?

Loading comments…
🤖

BitsNotes AI Assistant

Subject Notes Assistant

Configure AI Chat

Choose how to access the chatbot
Have your own API key?

Switch to "Bring Your Own Key" tab above for unlimited access with any OpenAI-compatible provider.

🔑 Enter API key above to fetch live models from provider, or enter model name manually.
OpenAI-Compatible API Support

Choose any provider preset (Gemini, DeepSeek, Kimi, GLM, MiniMax, Qwen, OpenAI, Groq, Ollama, etc.) or enter a custom endpoint URL.

Security & Privacy First

Your API key is sent directly from your browser to your specified provider. BitsNotes servers never store or see your key.