Skip to main content
Unsupervised Deep Learning

BERT, GPT, and Vision-Language Models

Published: 2026-08-25
Level: postgraduate
Audience: Postgraduate students in machine learning

BERT, GPT, and Vision-Language Models

16.1 Exam Preparation Road Map

16.1.1 What the Comprehensive Exam Will Cover

Before the last content block of the course, here is the full picture of what the final exam rewards. Treat these six points as a checklist — each one is a lever you control before exam day.

First, review the past comprehensive question papers together with their posted solutions. The style of those papers is the strongest predictor of what you will see again: same question families, similar numerical structures, comparable mark distribution. Working a past paper under time pressure and then checking the posted solution tells you two different things — where you lose marks for knowledge and where you lose marks for speed.

Second, questions related to Assignment 2 are guaranteed to appear. If you worked the assignment diligently and also studied the theory behind each step, you are covered there. The distinction matters: an examiner can take any step you executed in the assignment and ask "why this step, and what would happen without it?" — that version cannot be answered from memory of your submission alone.

Third, bring a scientific calculator. Programmable calculators are not expected; follow whatever the official communication says about the allowed model. Diffusion and energy-based questions can involve evaluating exponentials, so a calculator you already know how to drive saves real minutes.

Exam note: The question types will resemble the past papers, with the addition of Assignment 2 related items. Past-paper style plus assignment theory is the core bet.

Fourth, refresh the deep neural network basics you already know: weight updates using backpropagation, and parameter calculation of a network. You are expected to remember these without re-learning them. For parameter calculation, practice counting weights and biases layer by layer on small networks until it is mechanical.

Fifth, pre-midsem topics occupy only a small slice of the paper — about 5 to 15 percent, not more. The rest comes from everything covered after that point plus the recommended reading. Allocate study hours roughly in that proportion.

Sixth, three areas are named explicitly as likely question sources: diffusion plus energy-based models, natural language processing (NLP) applications, and vision applications — alongside the Assignment 2 topics. Notice how much of this lecture feeds directly into those areas: BERT and GPT are the NLP applications; captioning and visual question answering are the vision applications.

16.1.2 Study Plan and Practical Advice

Practice numerical problems. You can generate extra drills with ChatGPT or any similar tool, focusing on diffusion basics, energy-based models (EBMs), NLP applications, and the assignment topics. A useful drill loop: ask for a problem, solve it fully by hand, then ask for the worked solution and compare line by line — the mismatches show exactly which concept needs another pass.

Printed watermarked slides may be carried into the exam room, so prepare them in advance. Well-organised printouts work as a second memory: annotate them during revision so finding a formula mid-exam takes seconds rather than minutes.

Managed well, you have more than ten days of preparation time — enough to cover all of the above comfortably. A simple split works: first pass over post-midsem topics, daily numerical practice from day two onward, full past papers near the end, and light slide-annotation throughout.

16.2 BERT: Bidirectional Transformers for Language Understanding

16.2.1 Why Bidirectionality Matters: Understanding versus Generation

Here is the question that launches this whole block: what would it take for a machine to genuinely understand a sentence rather than just produce one?

The architecture to master here is BERTBidirectional Encoder Representations from Transformers, built at Google. (The name is sometimes misheard as "BART"; every property listed next — masking, next-sentence prediction, an encoder stack, and the Google origin — identifies the model unambiguously as BERT.) The phrase "language understanding" is the key part of its name, because deep understanding powers an entire family of applications:

  • Sentiment analysis — deciding whether a text expresses a positive or negative opinion ("This phone is a beautiful paperweight" — positive words, negative sentiment; catching that needs real understanding).
  • Question answering — answering a question from a given passage, by finding where in the passage the answer lives.
  • A large family of similar tasks: paraphrase detection, entailment, entity recognition.

Why does BERT look at text in both directions? Compare it with a generative model. To generate new text, an autoregressive (AR) model may only condition on tokens that already happened — the past. There is no choice about this: you cannot generate a word from words that do not exist yet. If you are writing "The cat sat on the ___", the next word must be produced using only "The cat sat on the". But BERT's scope is understanding, not generation, so this restriction disappears. When the goal is to interpret a word in a sentence, using the tokens on its right is perfectly reasonable — indeed, the right-side context is often exactly the evidence you need.

Real-world placement: this understanding-first design is why BERT-style models sit behind search engines (matching a query to relevant documents), sentiment systems, and question answering products, rather than behind text generators.

16.2.2 Architecture Properties

Three properties distinguish the architecture.

1. Depth. It stacks multiple layers of transformers, so it is a deep architecture. Each transformer layer receives information from a position, from positions before it, and from positions after it — every layer sees both sides. Nowhere in the stack is there a rule limiting information to the past. (Contrast this with the decoder's masked self-attention, which zeroes out attention to future positions precisely so that generation cannot cheat.)

2. Bidirectionality, precisely stated. The representation of any token is computed from the full surrounding context, left and right. This is the precise meaning of "bidirectional" here: not two separate networks reading opposite ways, but every token attending to every other token.

3. Self-supervised pre-training. Like many models in this course, it uses self-supervised learning to build its initial language competence. During pre-training the model learns to predict a held-out token from all the remaining tokens — predict token from together with through the end of the sequence. No human labels are needed for this stage, which is what unlocks enormous corpora.

16.2.3 Pre-training Task 1: Masked Language Modelling

The signature pre-training technique is masked language modelling (MLM). Think of it as a fill-in-the-blank exam that the model sets for itself: hide a word, look at everything around the blank, guess the hidden word, check against the truth you secretly hold.

Take a sentence such as "The cat sat on the mat." For every sentence, randomly pick some words and replace each with a dummy mask token. About 15 percent of the input tokens get masked. The model must predict each masked word from the tokens before it and after it — both sides feed the prediction through the transformer weights.

Formalizing the goal. In words, the training goal reads: maximise the probability of the masked word given all the other tokens, changing all the parameters of the network to achieve it. Written as an objective:

Each symbol in turn:

  • is the hidden token — the word replaced by the mask.
  • is the set of all surviving tokens around it, from both the left and the right side.
  • collects every trainable parameter of the architecture — attention weights, feed-forward layers, embeddings, all of it.
  • means "the value of that makes the quantity after it as large as possible."

Because the context includes tokens from both directions, the procedure forces the model to fuse information from the whole neighbourhood of the mask into one representation. This task is completely unsupervised — really self-supervised, since the labels come from hiding parts of the input itself.

In practice the maximisation runs as gradient descent on the equivalent minimisation: take the negative logarithm of that probability and drive it down. At each masked position the model outputs a score for every vocabulary word, a softmax turns scores into probabilities, and a multiclass cross-entropy loss compares the predicted distribution with the true hidden word. Maximising and minimising are the same instruction.

Worked sketch: if the original sentence is "cat sat on the mat" and the mask lands on "cat", the model sees the embedded representations of everything else and adjusts all parameters until is high. Repeat over millions of sentences and the encoder absorbs grammar, facts, and word senses.

Masked-word walkthrough. Sentence: "The cat sat on the mat." Mask position 3: "The cat [MASK] on the mat."

Step 1 — Tokenize into subwords: [The] [cat] [MASK] [on] [the] [mat] — six input positions. Step 2 — Embed each position (token + position + segment embeddings, covered below). Step 3 — Run all six embeddings through the transformer stack. Because attention is bidirectional, the output vector at position 3 has absorbed both "The cat" from the left and "on the mat" from the right. Step 4 — Map the position-3 output to vocabulary scores, apply softmax. Suppose the top predictions come out: sat 0.62, lay 0.11, rested 0.05, slept 0.04, other words share the rest. Step 5 — Loss: the true word is "sat", so the loss is . Gradient descent nudges θ to raise that 0.62 next time.

Answer: predicted word = "sat", loss ≈ 0.478. Sense-check: "sat" beats "lay" plausibly because "sat on the mat" is the more frequent collocation in ordinary English — which is exactly the kind of statistic MLM is designed to absorb.

A note on efficiency worth knowing: MLM makes inefficient use of data. Only about 15 percent of positions contribute a prediction, so processing seven tokens adds roughly two loss terms. Autoregressive models predict at every position and use data more efficiently — but they give up the right-side context. That trade-off is the whole design decision between encoder and decoder.

The original token embeddings may come from fixed encodings such as GloVe vectors before training starts; the MLM objective then sharpens them into contextualised representations — the same word ends up with different output vectors in different sentences, which a fixed embedding can never do.

16.2.4 Pre-training Task 2: Next Sentence Prediction

BERT pre-training has a second, equally particular innovation: next sentence prediction (NSP). Given a sentence A during pre-training, with 50 percent probability you attach the truly following sentence as segment B; for the remaining 50 percent you attach a random sentence that does not follow A. A classification label rides along with every pair:

The model reads both segments and predicts the label. So indirectly it learns what makes one sentence follow another — discourse coherence. The pre-training corpus supplies unlabelled sentence pairs in bulk; the 50/50 split keeps the task honest, since always guessing "yes" would score only half.

Labelling two pairs.

Pair 1 — A: "My dog is cute." B: "He likes playing." These read as consecutive sentences from the same description, so the label is .

Pair 2 — A: "My dog is cute." B: "The bank raised interest rates last quarter." Nothing connects them, so .

During training the model must output 1 for the first and 0 for the second. To succeed across millions of such pairs it must pick up pronoun links (he ↔ my dog), topic continuity, and register consistency. Sense-check: a model that ignores B entirely scores exactly 50 percent on this task — the balanced split makes lazy strategies worthless.

Together, MLM and NSP are the complete pre-training recipe. Both run on a huge unlabelled corpus, and both shape the contextualised token representations that fine-tuning later exploits. (Worth knowing for perspective: later analysis found the NSP signal contributes only marginally once MLM is strong — a fact RoBERTa will act on in the next section.)

16.2.5 Input Representation and Tokenization

Many NLP tasks feed two segments at once: a question and an answer passage, or two sentences plus a paraphrase/entailment label. BERT's input format reflects this. Consider the pair "my dog is cute" and "he likes playing".

Subword tokenization. The text is first broken into tokens by a highly optimised subword tokenizer — rare or composite words are split into smaller pieces so the model never faces an unknown word. BERT's scheme is called WordPiece: a vocabulary of frequent pieces (whole common words plus frequent fragments like "##ing") covers any input by composition. So "unhappiness" might split into "un" + "##happiness", and even a typo or a made-up word still maps to known pieces. WordPiece is the standard subword scheme shipped with BERT.

Special tokens. A special classification token [CLS] opens the sequence and a separator token [SEP] divides the two segments. The [CLS] position will later carry a summary vector used for whole-sequence classifications.

Three summed embeddings. On top of raw tokens the model computes three kinds of embeddings, summed into one vector per position:

  • Token embeddings say which subword sits there.
  • Position embeddings say where it sits in the concatenation of both segments — the position counter runs continuously across the pair, not restarting at each segment.
  • Segment embeddings say which of the two segments it belongs to — one embedding value for segment A, another for segment B.

Input layout for "my dog is cute" + "he likes playing".

Position:    1      2    3   4    5       6     7    8     9        10
    Token:    [CLS]   my   dog  is cute  [SEP]  he  likes playing  [SEP]
    Segment:    A     A    A    A    A       A     B    B      B        B

Both sentences are plausibly consecutive, so the NSP label during pre-training for this pair would be 1. Position embeddings run 1 through 10 across the whole thing; the segment row switches from A to B at the separator. Each column's three embeddings are added to form the single vector entering layer 1 of the transformer.

Sense-check: count the columns — 10 inputs, 10 output vectors, one per position, exactly matching what the transformer consumes.

A practical warning: the embeddings BERT produces are sensitive to the tokenizer choice. The WordPiece vocabulary shipped with the model was tuned together with it, so swapping tokenizers degrades the representations — always load the matching tokenizer when reusing a checkpoint.

16.2.6 Fine-Tuning for Question Answering

After pre-training comes fine-tuning on a specific task with limited labelled data. Question answering is the canonical example. The input is the question as one segment and the paragraph that contains the answer as the other segment.

Two output vectors are attached at the top, one for answer start and one for answer end:

where is the number of paragraph tokens, carries a 1 at the token where the answer starts, and carries a 1 at the token where it ends — all other entries are 0. Supervised training pushes the two vectors toward these one-hot targets, so the network learns to classify the boundary tokens of the answer span inside the passage. In implementation terms, every token position produces two scores (start-score and end-score); softmax over the paragraph positions turns each set of scores into a distribution, and training maximises the probability mass on the true boundary tokens.

Locating an answer span. Paragraph: "The cat sat on the mat because it was soft." Question: "Where did the cat sit?"

Tokenize the paragraph into 10 tokens: [The] [cat] [sat] [on] [the] [mat] [because] [it] [was] [soft], so .

The answer span is "on the mat": it starts at token 4 ("on") and ends at token 6 ("mat").

Target start vector: . Target end vector: .

Training drives the model's start-distribution toward and end-distribution toward . Answer: the span "on the mat" = tokens 4 through 6. Sense-check: every entry of each target vector sums to 1 and marks exactly one boundary — a valid one-hot pair.

This is why the workflow is called semi-supervised: the bulk of the learning is self-supervised pre-training on a huge corpus, and only a thin supervised layer is added per task. The same pre-trained checkpoint fine-tunes into question answering, summarisation-style tasks, and other understanding jobs.

16.2.7 Where Masking Came From: The Cloze Task

Masked language modelling did not appear from nowhere. In 1953, researchers from the psychology and linguistics community introduced the cloze task: blank out words in a text and ask a person to fill them. It had nothing to do with training deep networks — it measured mental ability and psychological conditions. The deep learning community borrowed the format and turned it into a training signal: hide a token, predict it from both sides, and you have a self-supervised task over unlimited raw text.

The historical echo is pleasing: a seventy-year-old psychology instrument became, with transformers behind it, one of the most productive training objectives ever found. The mechanism transfers because filling a blank well requires exactly what we want the model to learn — grammar, semantics, and world knowledge fused into one prediction.

16.2.8 Training Cost and Industrial Scale

BERT came out of Google, which owns enormous training corpora and its own accelerator hardware: the TPU (Tensor Processing Unit), a chip designed specifically for the matrix multiplications that dominate neural network training. Pre-training runs on TPU-based servers and takes on the order of days. Nobody retrains daily — the schedule is more like once a year or once in a few months. Fine-tuning, in contrast, is cheap: only a few hours, because it nudges an already competent representation rather than building one from scratch.

Scope: the pre-train/fine-tune cost asymmetry defines who can play. Expensive pre-training once, cheap fine-tuning many times is the economic engine of the whole pre-trained-model industry. If your organisation cannot afford stage one, its realistic strategy is to fine-tune someone else's released checkpoint.

BERT reached state-of-the-art results on language understanding benchmarks: paraphrase detection, sentiment analysis, question answering. Its limit is equally important: it has no generation capability. Everything it does rests on deep understanding of language — ask it to continue a document and there is simply no machinery for producing the next token from past-only context.

Exam note: Be ready to state the two pre-training tasks (MLM with ~15 percent masking, NSP with a 50/50 label split), write the MLM objective, explain the three summed input embeddings, and trace the start/end vector construction for extractive question answering. The bidirectional-vs-autoregressive contrast is the single most testable idea here.

Recap: BERT is a deep bidirectional transformer encoder trained self-supervisively with MLM and NSP, then fine-tuned cheaply per task — built for understanding, not generation. The natural next question is whether the recipe itself can be improved without touching the architecture, which is exactly RoBERTa's story.

16.3 RoBERTa: An Optimized BERT

16.3.1 What Changed in Pre-training

An improved version followed: RoBERTa — the name expands to a robustly optimized BERT. Most of the engineering carries over unchanged. Three changes to the pre-training strategy deliver the gains.

First, the next sentence prediction task is dropped entirely — it turned out to be unnecessary overhead. Removing it frees model capacity and training time for the task that actually drives understanding, MLM, and simplifies the input format since single sentences no longer need pairing.

Second, the masking strategy changes: instead of masking a single static choice of locations per sentence, masking covers multiple locations, refreshed during training. In static masking, each sentence is pre-masked once and the model sees the same blanks every epoch — it can quietly memorise those particular completions. Dynamic masking re-draws the mask pattern each time a sentence is fed in, so across epochs the same sentence yields many different prediction problems, which acts like free data augmentation and keeps the task from going stale.

Third, the model trains on an even bigger corpus, scaling up masked language modelling itself — more data, larger batches, longer training. The gains came from doing the same objective harder, not differently.

16.3.2 What Stayed the Same

The architecture remains a bidirectional transformer encoder, the objective remains masked language modelling, and the deploy pattern remains pre-train then fine-tune. Nothing in the network drawing changed at all — same attention blocks, same encoder stack, same embedding scheme.

Exam note: The lesson worth remembering: a large part of model quality lives in the pre-training recipe, not only in the architecture drawing. If asked "how does RoBERTa differ from BERT?", the answer is three training-recipe points (drop NSP, dynamic multi-location masking, bigger corpus) and zero architecture points.

Recap: RoBERTa kept BERT's body and rebuilt its training schedule, proving that recipe quality was being left on the table. Next we flip to the other family entirely — models built to generate rather than understand.

16.4 BERT versus GPT: Understanding versus Generation

16.4.1 Encoder versus Decoder

Set the two families side by side.

Dimension BERT GPT
Direction of attention Bidirectional — every token sees left and right Past-only — each token sees itself and earlier tokens
Transformer block Encoder Decoder with masked self-attention
Purpose Language understanding: question answering, sentiment, entailment Language generation: producing new text token by token
Pre-training task Masked language modelling (+ NSP in original BERT) Next-token prediction at every position
Adaptation to tasks Fine-tune with a small head per task Fine-tune in GPT-1; prompting alone in GPT-2/3
Cannot do Generate text Use future context for representation

Same underlying transformer machinery, opposite information regimes, opposite jobs. The masked self-attention inside a decoder is what enforces the past-only rule: attention weights to future positions are set to zero before the softmax, so no position can peek ahead during training or generation.

Earlier standard practice built a separate model from scratch for every downstream task, including older sequence-to-sequence systems for language generation. GPT-1 changed that: pre-train one language model, then apply a standardised fine-tuning procedure across all downstream tasks. One backbone, many heads — the same transfer-learning move BERT made on the understanding side.

16.4.2 Stock Transformers: The Analogy

The professor's analogy for what pre-training buys: stock photography.

Think of what pre-training buys with a stock-photography analogy. A stock photo library holds pictures of all kinds — landscapes, offices, people mid-handshake; a buyer picks and adapts rather than staging a shoot from scratch. A stock text corpus likewise holds a sample of the language itself: books, articles, forums — language as it is actually used. Pre-training learns the language model embedded inside that stock of text — the transformers internalise how the language works, its grammar, its common associations, its typical phrasings. Downstream, you adapt this stock representation to your specific need: a sentiment head here, a question-answering span predictor there.

Where the analogy breaks: a photo buyer only ever re-crops or recolours what exists, whereas a fine-tuner changes the model itself — and a prompt-steered GPT does not even do that much. The deeper point survives both readings: as long as huge training data exists, you can learn the language model to a very large extent, and the learned model unlocks generation, question answering, and many fascinating capabilities.

Recap: BERT and GPT are two deployments of one machine — encoder-bidirectional-understanding versus decoder-autoregressive-generation — and both replaced per-task engineering with pre-train once, adapt cheaply. The next section follows the generation branch as it scales up and discovers that prompts can replace fine-tuning altogether.

16.5 GPT-2 and GPT-3: Scale and Prompting

16.5.1 Scaling Parameters and Context

GPT-2 and GPT-3 extend the original GPT. Two things distinguish them: the scale of the architecture and the scale of the context used to generate.

On sizes: GPT-2 sits around the billion-parameter range (roughly 1.5 billion), and GPT-3 reaches about 175 billion parameters, trained on roughly 300 billion tokens. (If you ever meet a figure like "more than 150 million parameters" for GPT-3, read the unit as billions; the published size agrees once the unit is corrected.) Either way the trend is what matters: each generation is orders of magnitude larger than the last. Both remain completely unsupervised — self-supervised language modelling, no task labels at all. Every token of training text supplies its own supervision: predict the next token from the ones before it.

The deeper change is procedural, not architectural. GPT-1 needed a fine-tuning stage with labelled data for each downstream task. GPT-2 and GPT-3 drop task-specific fine-tuning. Instead, the prompt itself steers the model — you describe or show the job inside the input text, and nothing about the weights changes.

16.5.2 Zero-Shot Learning with Prompts

Zero-shot learning means solving a task from a single instruction prompt, with no training examples attached. The prompt acts as a cue that reshapes how the pre-trained network behaves — a very shallow adaptation compared with gradient-based fine-tuning, where labelled data physically moves millions of weights. Here no weight moves at all; only the input pattern changes.

This raised an immediate doubt in class, and the resolution matters:

Q: When you said it makes use of zero-shot prompting to learn, does it make use of RAG or anything else instead of fine tuning to get the data the GPT-2 model does not know? How does it actually get the data?

A: No — GPT-2 and GPT-3 do not use the RAG (retrieval-augmented generation) model; nothing fetches external documents at inference time. Compare with BERT's fine-tuning: there the prompt is like a question, paired with the segment containing the answer between its start and end points. For GPT-2 and GPT-3, the prompt question and the prompted answer are used as information during inference. Zero-shot means the answer is produced from the whole context, and the prompt itself becomes a signal — a very shallow signal for adapting the pre-trained GPT-2 architecture. Nothing retrieves external documents. GPT-3 goes further: it consumes multiple prompts, which is exactly why it is called few-shot learning.

Why did the RAG guess seem plausible? Because if a model answers questions without training on them, something must be supplying the missing knowledge — and retrieval is one honest way to do that. The resolution is subtler: the knowledge was already inside the pre-trained network, compressed there during self-supervised training on the huge corpus. The prompt does not deliver knowledge; it selects which of the behaviours already learned should surface. The takeaway: the prompt is not a database lookup. It is a demonstration folded into the input, and the network's pre-trained knowledge does the rest.

16.5.3 Few-Shot Learning in GPT-3

Few-shot learning packs a handful of solved examples into the prompt before the real query.

Teaching addition through three demonstrations.

Prompt given to GPT-3:

2 + 3 = 5
    9 + 8 = 17
    12 + 30 = 42
    3 + 4 =

Step 1 — The model conditions on all four lines: three completed exchanges plus the open question. Step 2 — Pattern extraction: two numbers, an equals sign, their arithmetic sum. Three consistent instances pin down the transformation. Step 3 — Next-token prediction continues the sequence. Answer: 7.

Sense-check: 3 + 4 = 7 is correct, and crucially it was not among the demonstrations — the model generalised the operation rather than copying a shown pair. No gradients moved anywhere in the network.

The same pattern works for editing requests: hand over three corrected-writing demonstrations — original page, corrected page — then supply a fresh page to fix, and the model returns the corrected page by imitating the shown transformation. The demonstrations define the mapping; the model applies it.

Exam note: Keep the adaptation spectrum straight — zero-shot (instruction only, shallowest), few-shot (a handful of in-context demonstrations), full fine-tuning (gradient updates on labelled data, deepest). GPT-2/3 live on the first two rungs; BERT-style models live on the third.

16.5.4 Data and Compute Requirements

The key message cuts both ways. With enormous training data and nearly limitless computing, you can learn the language model almost completely. Generation, question answering, summarisation, and design-flavoured generation all fall out of it. Remove those resources and you fall back on traditional NLP techniques — hand-built parsers, task-specific feature engineering, smaller statistical models.

Real-world placement: companies with vast data and compute — Amazon, Google, OpenAI — can digitise data themselves, train, and keep improving such models; scale is their moat. For small datasets on local infrastructure, the realistic hope is open-source models; several strong ones now come out of China and compete respectably with proprietary systems such as GPT and Gemini. The strategic question for any team is not "which architecture?" but "whose checkpoint can we afford to adapt?"

Recap: GPT-2/3 scaled parameters and context, then replaced fine-tuning with prompts — instructions for zero-shot, demonstrations for few-shot. Having followed the generation branch to its conclusion, we now switch modalities: what happens when images join text?

16.6 Vision-Language Tasks: What Modern Systems Can Do

16.6.1 Captioning, Visual Question Answering, and Retrieval

Given one image, today's AI systems produce a caption — a natural-language description of the scene (visual captioning). Given an image plus a question, they answer it: visual question answering (VQA). Ask "What is the woman doing?" and the answer comes back "riding a horse". Counting questions work too: how many horses are there, how many women.

Retrieval closes the loop in the opposite direction: submit "show me horses on which a woman rider is present in front of a river" and the system fetches matching images from a database. Notice what that query demands — the system must parse a compound spatial-and-identity condition ("horse", plus "woman rider", plus "in front of river") and score every image against all three clauses at once.

These are joint-modelling applications: the system must represent image content and textual content together, and connect them. Neither modality alone is enough — the text describes only what the image shows, and the image only matters through what the words say about it.

16.6.2 The Joint Tokenization Principle

The organising idea mirrors the NLP section exactly: tokenise everything, pre-train on the tokens, fine-tune or prompt for the task.

There, you tokenise the corpus into word pieces, pre-train, and optionally fine-tune for understanding or generation. Here, you tokenise the text as before — and tokenise the image as well, turning pixels or detected regions into discrete units a transformer can consume alongside words. Joint modeling of image tokens with text tokens enables the tasks above: captioning attends from image tokens to generate words; VQA conditions answers on both; retrieval scores image-text pairs with one shared representation.

Processing demand grows enormously compared with text alone, especially with state-of-the-art transformers stacked in multiple layers over both modalities — two modalities mean more tokens per example, longer sequences, and cross-attention patterns to learn on top of each modality's internal structure. The technical recipe extends what this course already covered; cost and purchase constraints are set aside here.

Scope: one boundary deserves a flag — video-text modelling is the next elaboration. Activities unfold as multiple things happening one after another, so a single frame cannot carry the semantics of an action: a frozen frame of a hand near a cup could be picking it up, putting it down, or missing it entirely. You need a sequence of images, which multiplies the data and compute bill yet again.

Recap: captioning, VQA, and retrieval all reduce to one recipe — joint token streams from both modalities. The next section starts with the simplest classical way to build such a bridge, before any transformer enters the picture.

16.7 Classical Visual Captioning with CNN plus RNN

16.7.1 Pipeline Walkthrough

Basic visual captioning needs no transformer. It is supervised learning applied in a smart way, using pieces from a standard deep learning course.

Purpose. The problem: map an image to a word sequence that describes it. Two different data types must meet — an image grid of pixels and a sentence of discrete words — and something has to translate between them.

Inputs and outputs. Input: an image plus its training caption (image-caption pairs). Output at generation time: the caption, one word at a time.

The four steps.

  1. Encode the image. Pass the image through a CNN — for example a ResNet. Late in the network you get a feature map such as channels, or : each entry summarises what a small patch of the image looks like in terms learned by the vision network. Pool or flatten this into one feature vector describing the whole image.
  2. Seed the decoder. Use that feature vector to initialise an RNN — it becomes the hidden state from which word predictions begin.
  3. Train on caption pairs. With the image vector as context, teach the RNN to predict the next word of the associated training caption, one word at a time, beginning from a designated start token. The supervision is just the caption itself shifted by one position.
  4. Generate. After training, a new image flows through the CNN, produces its context vector, and starting from the start token the RNN emits the caption word by word — each predicted word fed back in as input for predicting the next, until an end token appears.

The whole system is a joint CNN-plus-RNN architecture trained end to end on caption pairs. Its view of the image is global: one vector stands for the entire picture.

Trace on one image. A photo of a dog chasing a ball on grass.

Step 1 — The image enters ResNet; the late stage outputs a tensor — that is 7 × 7 = 49 spatial cells, each holding a 512-number summary. Global pooling collapses the 49 cells into a single vector . Step 2 — initialises the RNN's hidden state. Step 3 — Training targets for this pair: <START> → "a", then "a" → "dog", "dog" → "chases", "chases" → "a", "a" → "ball", "ball" → <END>. Each step predicts exactly one next word while carrying 's influence forward. Step 4 — At test time on a fresh image, the same loop runs without ground truth: the model emits a, feeds back a, emits dog, and so on until <END> wins.

Result: caption "a dog chases a ball". Sense-check: every generated word was a genuine argmax over the vocabulary conditioned on image features and history — no lookup tables involved.

Complexity and cost. One CNN pass per image plus a number of RNN steps equal to caption length — cheap compared with transformers. But the single-vector bottleneck limits expressiveness: a 512- or 1024-number summary must carry everything worth describing, so fine spatial relationships tend to wash out.

16.7.2 Ways to Improve the Basic System

Several upgrade paths exist:

  • Swap in a different CNN architecture for stronger vision features — better backbone, better description of what is present.
  • Replace the plain RNN with an LSTM variant to capture long-term dependencies across longer captions — gates inside the LSTM decide what to remember and what to forget, which keeps early words ("a woman") available when generating later ones ("who is juggling").
  • Add attention mechanisms so the decoder can focus on relevant image regions while emitting each word — instead of one global vector, the decoder consults a weighted mix of region features, re-weighted per word.
  • Bolt transformer-based language modelling on top of the CNN features as a stronger word-prediction engine.

Each upgrade raises caption quality, but the global-feature bottleneck remains until the region-based methods of the coming sections — those replace "one vector per picture" with "one token per object", which is precisely what joint modelling needs.

Scope: the classic pipeline is fully supervised — it needs curated (image, caption) pairs, unlike the self-supervised schemes of the next section. It also cannot answer questions or retrieve by text; captioning is its only job.

Recap and bridge: CNN encodes, RNN decodes, captions supervise. The limitation — everything squeezed through one vector — motivates both the self-supervised tricks coming next and, eventually, token-per-region representations.

16.8 Self-Supervised Learning for Images

16.8.1 Image Colorization

Self-supervision transfers to images with the same trick as language: hide something, predict it back. In text you hide a word; in an image you destroy a property — colour — and ask the model to restore it.

For image colorization, take photos captured with a mobile phone and manufacture the input-output pair yourself — no human labeller ever enters the loop. Create the grey version by simple averaging of the colour channels:

where , , and are the red, green, and blue channel values of each pixel (each in for 8-bit images) — the sum of the three channels divided by three. A note on standards: many image tools instead use a luminance-weighted blend such as , which mimics eye sensitivity; the plain average is what this lecture assumes.

Train on the pair (grey input, colour original) and the model learns colorization: given greyness at every pixel, predict the three missing channel values per pixel.

Building one training pair by hand. Take a single pixel from a photo where the sky meets a roof:

Step 1 — Original colour channels: , , (a pale blue). Step 2 — Grey version: . Step 3 — The training example becomes input (grey), target (colour). Step 4 — Repeat for every pixel of every photo in the collection; the model learns to map each grey value in context — neighbouring pixels matter; sky-blue above, roof-red below — back to its true colour triplet.

Result: a network that colors unseen grey photos. Sense-check: the target triplet lies inside as required, and reversing the arithmetic confirms .

Beyond this global version, chop the image into small blocks and colourize per patch. Patch-wise processing lets the model specialise to local textures — grass needs one palette, brick another — because each patch presents a narrower, more consistent prediction problem than the whole scene at once.

16.8.2 Image Inpainting

Image inpainting follows the masked-language blueprint even more directly. From one original photo, generate several masked copies — punch holes or cover regions, differently each time. Train a generative model to reconstruct the complete image from the masked input. Suitable engines include GAN variants and the diffusion models covered earlier in the course.

Manufacturing inpainting data from ONE photo.

Original: a portrait with background shelf. Copy 1 — mask the face region; target: full original. Copy 2 — mask the top-left shelf corner; target: same full original. Copy 3 — mask a horizontal band across the middle; target: same full original.

Each copy yields fresh (damaged, complete) supervision from zero new labels — three examples from one photograph, unlimited if you keep drawing new masks. The model must learn what texture plausibly fills each gap. Sense-check: the targets never change and always stay realistic, so any hallucinated filler shows up directly against ground truth during validation.

The parallel worth internalising: just as masked language modelling forces a model to learn what word fits a linguistic gap, masked image modelling forces it to learn what texture fills a visual gap. Same self-supervision contract — corrupt, then repair — different modality.

Recap and bridge: colourization hides chroma, inpainting hides regions; both turn unlabelled photos into free supervision. But real vision-language systems need more than repaired pixels — they need captions, and captions live on the internet, noise included, which is exactly where we go next.

16.9 Pre-training on Large, Noisy, Cheap Data

16.9.1 Weak Labels from Social Media

Vision-language pre-training consumes large, noisy, cheap data: uncurated image-plus-text pairs already lying around in social media posts, digitised books, journals, and magazines. No collection campaign is needed — the data exists whether or not anyone curates it, and that is what makes it cheap.

Alongside this raw data you need some small, clean, labelled data for fine-tuning. End users often supply the labels unknowingly: comments and likes on social media images act as implicit annotations, for example signalling positive or negative sentiment toward an image. A million likes is a million weak votes for "this image pleases people". Companies such as Google exploit exactly this user-generated labelling at scale — the users are the annotation workforce without ever being told.

Scope: "weak" labels cut both ways. They arrive free and in bulk, but nobody verified them: captions may describe things the image does not show, likes may reflect meme value rather than content, and comments may be jokes. Everything built later inherits that noise.

16.9.2 Case Study: A Mismatched Caption and Hallucination

Worked case study — auditing one training pair.

The pair: an image captioned "little girl and her dog in northern Thailand. They both seem interested in what we were doing."

Audit it phrase by phrase against the pixels:

Caption phrase Supported by the image? Verdict
little girl yes — a child is visible keep
her dog yes — a dog is visible keep
northern Thailand nothing in the frame indicates location spurious
interested in what we were doing intent cannot be seen; both look toward camera spurious

So the honest content of this pair is only: a child and a dog looking toward the camera. The best possible caption would be minimal — something like "a girl with a dog sitting in a rural setting and looking forward" (the spoken reconstruction stumbled into "a man and a man and a dog"; the intended minimal description is one person plus one dog, no location, no mind-reading). Every extra unsupported word is noise injected straight into training.

Sense-check: two of four phrases survive the audit — a realistic noise rate for web data, which is precisely why it matters.

Train on many such pairs and the model absorbs spurious associations — it overtrains on words the pixels never justify. The mechanism: gradient descent does not know which words are grounded and which are decoration; minimising caption likelihood rewards producing "Thailand"-flavoured text whenever the visual context resembles these photos. Consequence: hallucination. If a later image shows two children in a similar background, the system may emit a confident caption referencing Thailand or unseen intentions, and the answer looks nonsensical to the user.

This is why hallucinations observed in modern image models often trace to imperfect training data rather than to the architecture alone. The network faithfully learned exactly what we taught it — including our errors.

16.9.3 The Standard Strategy: Pre-train, Then Fine-tune

Because uncurated pre-training can never be enough (unless captions were curated perfectly, which they never are), the typical strategy is two-stage:

  1. Stage one — foundation model. Learn a foundation model on stock data of the noisy kind shown above — the broad base model that everything else adapts, with the name emphasising its role as common groundwork. The goal is broad coverage of vision-language statistics, accepting some noise as the price of scale.
  2. Stage two — fine-tune. Adapt that model on small clean labelled data for each downstream task. Clean labels here re-anchor the model to grounded associations after the noisy pass.

Visual question answering illustrates the boundary. Reasonable questions — who is in the image, what are they doing, urban or rural setting — receive sound answers after fine-tuning. Hyper-specific questions about caption content the image never supported return hallucinated answers, because the model's confidence was calibrated on ungrounded text.

Exam note: Two dials control hallucination risk: curate the pre-training captions properly and the gap shrinks; skip fine-tuning entirely and it does not. Both stages matter — noisy-scale then clean-adaptation is the standard recipe, not either stage alone.

Recap and bridge: noisy data gives scale, clean data gives grounding, and hallucination names what happens when the first runs without the second. Next: how to represent images so they can sit beside words inside one transformer at all.

16.10 From Pixels to Tokens: Region-Based Visual Tokenization

16.10.1 Bounding Boxes as Visual Tokens

Modern systems no longer squeeze an image into one global feature — the bottleneck of the classical captioner. Instead they tokenise the image itself using region-based CNN detectors: the R-CNN family, including Faster R-CNN, from the computer vision toolkit. A YOLO-style detector serves the same role (YOLO stands for You Only Look Once, a one-pass object detector). Detection places a bounding box around each object and classifies its content.

Anatomy of a visual token. Each box becomes one visual token carrying three things:

  1. Normalised coordinates of the box — normalisation means the full image width counts as 1 and the full height counts as 1, so each coordinate edge is a fractional number between 0 and 1 expressing how far along the axis it sits. A box written spans from a tenth of the way across to just under half, and from a quarter down to nine-tenths.
  2. Class identity of the object inside — person, dog, sofa, mug.
  3. Feature values describing that content — the CNN's internal feature vector for the region, encoding appearance detail beyond the label.

Worked demo on a living-room scene. The detector emits five tokens:

Token Box colour Class Normalised box Reading
1 red person centre-left, upper three-quarters
2 green dog lower right
3 purple sofa wide band across the bottom
4 blue mug small, upper-right area
5 green dog curled on the person's lap

Note token 5: possibly a second dog on the lap — same class as token 2, different position. And note that even though surrounding text might say "man", the class label coming from the detector is person: detectors classify into their fixed training taxonomy. Running this over the original image yields a sequence of five visual tokens — coordinates plus classes plus features — ready to sit beside text tokens in a transformer.

Sense-check: every coordinate is inside , boxes may overlap (dog-on-lap sits inside person), and each row carries all three components — exactly the contract a joint encoder expects.

16.10.2 Learning Spatial Semantics

Joint training over visual and textual tokens teaches surprisingly abstract language. Three mechanisms do the teaching:

  • Relative positions carry spatial words. Seeing the couch box below and the person box above teaches the model what "on" denotes — the geometry of the purple sofa region under the red person region instantiates the preposition. Nobody wrote a definition of "on"; thousands of stacked configurations are the definition.
  • Co-occurrence binds synonyms. The same visual configuration labelled sometimes sofa, sometimes couch, ties the two words together — they compete to describe identical pixels, so their embeddings converge.
  • Association attaches attributes. Linking the red person region with the word man in text lets the model infer gender attributes from vision plus language jointly — the word anchors onto the region across many images until the pairing is reliable.

Capturing such cross-token associations is exactly what attention in a transformer does naturally — every token can consult every other token — which is why attention-based joint encoders dominate this area.

Pitfall: poor detections poison everything downstream. If the visual tokens coming out of the detector are poor — wrong classes, sloppy boxes, missed objects — every later task inherits the damage: captions misname objects, VQA answers about objects that were never detected, entailment judgements inherit phantom or missing evidence. Quality control belongs at the detector stage, before any fusion happens.

Recap and bridge: regions become tokens; geometry becomes meaning; attention does the wiring. Next question: should the two modalities be encoded separately and merged late, or together from the start?

16.11 Two-Stream versus Single-Stream Fusion

16.11.1 Late Fusion: Separate Modality Encoders

Early vision-language architectures used two streams:

  1. The sentence — say "a young man playing frisbee" — passes through its own self-attention transformer, learning a contextualised representation of the text modality.
  2. The region tokens from an R-CNN or YOLO-style detector pass through a second self-attention transformer, learning a contextualised representation of the image modality.
  3. A third transformer then merges the two contextualised streams.

Each modality first becomes fluent in its own language, and only then do the languages meet.

The weakness is where fusion happens: high and late. If the text says "young person" instead of "young man", aligning the word with the corresponding visual region becomes harder, because the two modalities only meet after each has already frozen its own representation. The text encoder committed to a person-without-gender reading; the image encoder committed to region summaries with no knowledge that a gender word might matter — and only the third transformer must now reconcile both commitments from above.

16.11.2 Early Fusion: One Shared Transformer

Later architectures went single-stream: feed text tokens and image tokens together into one multilayer transformer, so cross-modal relationships form at lower levels. From layer 1 onward, the token for man can attend directly to the red person-region token; alignment is built up gradually inside every layer rather than patched on at the end. This is early fusion, set against the two-stream late fusion.

The comparison, settled by results.

Dimension Late fusion (two-stream) Early fusion (single-stream)
Where modalities meet Third transformer, after separate encoding Layer 1 of one shared transformer
Cross-modal attention depth Only in the merge stage Every layer
Word-to-region alignment when wording varies ("young person" vs "young man") Harder — representations already frozen Easier — refined jointly
Verdict from practice Historical baseline More effective, better results

Today the comparison is settled: early fusion wins. Named single-stream systems from this line include ViLBERT-era successors such as VisualBERT, LXMERT, UNITER, and PixelBERT — the standard single-stream vision-language models of that generation.

Recap and bridge: fuse early, let every layer negotiate between words and regions. The next section opens up UNITER's pre-training objectives — the full masking toolkit applied to this single-stream design.

16.12 UNITER: Joint Pre-training Objectives

16.12.1 Embedding Setup and Notation

UNITER is a high-performing single-stream joint model — one shared transformer over words and regions, exactly the early-fusion design the last section settled on. Its pre-training shows all the masking ideas in one place.

Fix the notation first.

  • The image contributes regions, giving visual tokens — one token per detected bounding box, as built in the region-tokenization section.
  • The sentence contributes word tokens .
  • In general — and that is fine. The situation mirrors machine translation, where the source segment and destination segment never need equal length: transformers consume sequences of whatever length they are given, and attention does not care that the counts differ.
  • A mask index set records which positions are hidden during a training step — simply the set of masked positions, drawn fresh for each example.

Embeddings follow the BERT pattern extended to vision:

  • Image side: region content embedding plus a location encoding computed from the normalised bounding-box coordinates, passed through fully connected layers and layer normalisation. The location encoding is what lets geometry participate at all — without it, two boxes with identical content but different positions would be indistinguishable, and spatial words could never ground.
  • Text side: token embeddings and position embeddings, concatenated, then layer normalisation.

Everything trains jointly — one parameter set θ serves all objectives defined below.

16.12.2 Masked Language Modelling with Visual Context

Task one applies the masking principle uniformly — the model does not fuss over which tokens are textual and which are visual; masking just hides items and demands them back.

For a masked word, the goal in words: maximise the log likelihood of the masked word given all the other text tokens and all the visual region tokens, adjusting the parameters theta of the whole network. Written as a minimisation-ready loss:

Each symbol: selects the masked word positions; is the surviving text (everything except masked slots); means all image regions through ; the minus sign converts maximising likelihood into minimising loss. This reconstruction follows the verbal description "maximise the log likelihood ... with respect to all words and all vision region tokens by changing the parameters theta" — and it passes its sanity checks: the sum yields a scalar loss, and setting collapses it back to plain BERT-style MLM.

The difference from pure BERT is precisely the conditioning: visual evidence helps pick the word. If the sentence reads "the man rides a [MASK]" and the image contains a horse-shaped box under the rider, the probability mass shifts toward "horse" over "bicycle" — information no amount of left-right text alone supplies reliably.

16.12.3 Masked Region Modelling: Regression and Classification

Task two masks image regions instead of words — and it splits in two, because a region carries more than content: it also owns a bounding box.

Part A — masked region feature regression. From the fused hidden states, regress the coordinates of the masked region's box. In words: use regression over the bounding boxes, so that minimising the regression functional makes the model detect bounding boxes correctly:

Symbols: denotes the fused vision-language hidden states (the transformer's output vectors); is the predicted four-number box for masked region , produced by a small head ; is the ground-truth box; is a coordinate regression loss, with standard squared-error on the normalised coordinates being the usual reading. Domain check: because coordinates are normalised to , a good prediction must land inside the unit square — anything outside is visibly wrong.

To see why this forces real understanding, trace the logic: the region's own features are hidden, so the only route to guessing where it sat is context — the words describe it ("the mug on the table") and the surrounding boxes constrain it (between the table top and the shelf). Geometry must be inferred from neighbours, exactly the skill spatial language requires.

One regression step in numbers. Ground-truth box of the masked mug region: . The model, reading "a mug sits beside the lamp" plus neighbouring boxes, predicts .

Squared-error coordinate loss:

Answer: — small, because the guess landed close; gradients push the remaining offsets toward zero. Sense-check: all four terms are squares, so none can go negative — a valid loss.

Part B — masked region classification. Given all the words and all vision tokens except the masked -th region, predict that region's object class. Training computes the cross entropy between the predicted class distribution and the correct class label. Additionally, a KL divergence compares the predicted content distribution with the ground-truth content distribution, checking that the contents agree. In words from the lecture: calculate the cross entropy, and use KL divergence to decide whether the content here and the content there are the same.

with the true class of masked region , the predicted class distribution, and the target distribution. The relative weighting between the cross-entropy and KL terms was not stated in the session, so they are shown unweighted here — the two terms play complementary roles: cross entropy rewards putting probability on the right single class, while the KL term disciplines the whole predicted distribution against a reference, penalising confident disagreement anywhere, not just at the winner.

Why mask regions at all? Same reason as masking words: prediction under hiding forces fusion. To classify an invisible region, the model must combine textual clues ("a shaggy dog") with visible neighbours (a leash leading off-frame), which trains exactly the cross-modal wiring these systems exist for.

16.12.4 Image-Text Matching

Task three asks a blunt question: do this image and this sentence belong together? A classification head on the CLS-style token outputs

where 1 means the region tokens and sentence tokens match and 0 means they do not. Pair the boss-asleep image with an unrelated sentence and the label is 0.

Two matching pairs.

Pair 1 — image: office desk with a person asleep on it; sentence: "he fell asleep at work after lunch." Label: 1 — desk, sleeping posture, and sentence agree. Pair 2 — same image; sentence: "two horses gallop across a field." Label: 0 — zero overlap between scene content and claim.

Training feeds thousands of such pairs; the head learns to read the joint representation at [CLS] as evidence of coherence. Sense-check: random pairing gives label 1 half the time by construction, so chance-level performance sits at 50 percent accuracy — the same honest baseline NSP used.

The mechanism leans on the same CLS-and-pair design that BERT's next sentence prediction made famous, lifted from sentence pairs to image-sentence pairs.

16.12.5 Combined Loss Function

Pre-training optimises all three objectives together. In words: the loss function is made up of three things — image-text matching, masked region estimation, and masked language estimation — and you want to optimise all of them together.

where bundles the regression and classification parts of masked region modelling from the previous subsection. Shape check: each term is a scalar, so their sum is a scalar — one number gradient descent can chase.

Exam note: Memorise the trio by what each forces the model to learn: MLM grounds words in pixels, MRM grounds regions in words and geometry (regression for the box, classification-plus-KL for the content), and ITM grounds coherence of the pair. Asked to reproduce the combined objective, write the three-term sum and name each term's conditioning.

The purpose of the whole stage: train every transformer weight so that words can consult regions and regions can consult words. Recap and bridge: pre-training has now wired the joint representation; the next section walks through the downstream tasks that fine-tuning plugs into it.

16.13 Fine-Tuning Tasks for Vision-Language Models

16.13.1 Visual Question Answering

Fine-tuning plugs the pre-trained joint model into concrete tasks — first of all VQA. The pre-trained weights already know how words and regions interact; fine-tuning only teaches the output format of each new job.

Sample questions show the difficulty ceiling:

  • "What colour are her eyes?" (answer: black) — needs locating a face, then a sub-part of it.
  • "What is the moustache made up of?" — needs material-level recognition, not just object labels.
  • Counting demands genuine perception: "How many slices does this pizza have?" requires detecting the cut boundaries that separate slices and then counting them — you cannot count slices without first seeing where one ends and the next begins.
  • Judging whether the pizza is purely vegetarian or a mix of vegetarian and non-vegetarian toppings requires fine-level content identification at topping granularity.

Trickier items probe inference beyond direct perception: "Is this person expecting company?", "What is just under the 3?", "Does it appear to be rainy?", "Does this person have 20/20 vision?". The model breaks the question into pieces, grounds them in image tokens, and answers — sometimes from pixels alone ("rainy": umbrellas, wet ground), sometimes by combining pixels with commonsense.

Exam note: The pizza-slice question is the memorable one: counting is a two-step perception task (find boundaries, then count), which is why it separates genuine visual grounding from caption-memorisation.

16.13.2 Visual Entailment

Here the premise is the image and the hypothesis is a caption; the model judges one of three labels: entailment, neutral, or contradiction.

Worked triple on one photograph (two people holding items outdoors).

Hypothesis Verdict Reasoning chain
"Two women are holding packages" Entailment The detector found two women; the bag and folder they hold count as packages. Every claim has pixel support — definitely true.
"Two sisters are hugging" Neutral Nothing contradicts it, yet nothing in the image implies sisterhood or hugging. Consistent but not necessary — the relationship word cannot be verified or denied.
"Two men are fighting outside a deli" Contradiction There is no fighting anywhere in the frame, and these people will not be detected as men. The image refutes both content claims.

The logic chain matters: entailment needs proof from the image; neutrality means consistency without implication; contradiction means the image refutes the claim.

Sense-check: notice that neutral is not "unsure" — it is a positive judgement that the image neither proves nor refutes the hypothesis.

A standing caveat ties back to tokenization: if the original visual tokens coming out of the detector are poor, entailment judgements inherit the errors — wrong person-class detections flip entailments into false contradictions. That failure mode belongs to the R-CNN stage, not to the language modelling.

16.13.3 Natural Language for Visual Reasoning

This task hands the model two images and one compound statement, and it answers true or false.

Worked example 1 — dog counting. Statement: "The left image contains twice the number of dogs as the right image, and at least 2 dogs are outstanding in total."

Step 1 — Architecture: the left image is processed separately from the right; the text is processed through both branches so each clause can consult its own image. Step 2 — Detection: left image → 2 dogs. Right image → 2 candidate objects, but only 1 qualifies as a dog. Step 3 — Clause check 1: "twice" holds, since 2 = 2 × 1. Step 4 — Clause check 2: total = 2 + 1 = 3 dogs, and 3 satisfies "at least 2". Step 5 — Both clauses verified. Verdict: true.

Via such training the model acquires quantitative phrases like "at least 2" — arithmetic over detected counts rather than pattern-matched captions.

Worked example 2 — acorn statement. Statement: "One image shows exactly 2 brown acorns in back-to-back caps on green foliage."

Observation: only one acorn appears across both images, and it is not in back-to-back caps. Two claims broken simultaneously — count and arrangement. Verdict: false. (an NLVR-style benchmark)

Sense-check: a single failed clause is enough for "false", because the statement asserts the conjunction.

16.13.4 Visual Commonsense Reasoning

Beyond perception, the model must reason about motives. Worked example: "Why is person 4 pointing at person 1?" Among candidate rationales, the correct one: person 3 is delivering to the table and might not know whose order this is, so person 4 points to clarify.

Selecting that answer requires fusing the visual scene — who holds what, who faces whom — with social commonsense supplied by language: pointing near food service conventionally means clarifying an order. Neither modality alone licenses the rationale; vision gives the configuration, language gives the social script.

16.13.5 Referring Expression Comprehension

Given a referring phrase such as "the woman watching dishes", the model must locate the referred person among all the people in the image. It is grounding in its purest form: language picks one entity out of many visual candidates, using the distinguishing clause ("watching dishes") as the filter. Output is a box on the right person — retrieval inside a single image rather than across a database.

16.13.6 Image-Text Retrieval

Retrieval runs both ways: given text, retrieve matching images; given an image, retrieve matching descriptions. A query like "a guard with a cat on grass" is scored against an image database using the CLS-style classifier — each image-query pair gets a true/false match score, and the highest scorers are returned.

This completes the syllabus. Optional supplementary material exists beyond this point, but it is not part of the syllabus.

Final reminders stand from the opening: review the past papers and solutions, master the theory behind Assignment 2, refresh backpropagation and parameter counting, and expect only a small pre-midsem fraction.

Exam Guidance Summary

  • Review past comprehensive question papers and their posted solutions; expect similar question patterns, with additions from Assignment 2. Working one paper under time pressure before checking solutions shows you both knowledge gaps and speed gaps.
  • Assignment 2 topics are guaranteed exam material; know the underlying theory, not just the submitted answers — examiners can ask "why this step?" about any part of it.
  • Bring a scientific calculator; programmable calculators are ruled out by official communication.
  • Refresh deep neural network basics: weight updates using backpropagation and parameter calculation of networks. These are assumed known, not re-taught.
  • Pre-midsem topics contribute only about 5 to 15 percent of the paper; allocate revision hours accordingly.
  • Core expected areas: diffusion plus energy-based models, NLP applications (BERT and GPT families), vision and vision-language applications, and Assignment 2 topics.
  • Practice numerical problems; tools like ChatGPT can generate extra drills on diffusion basics, EBMs, NLP applications, and assignment topics — solve by hand first, then compare against its worked solution.
  • Printed watermarked slides are allowed in the exam room; arrange printouts early and annotate them during revision so formulas can be found in seconds.
  • Time budget: more than ten days of preparation if managed well — enough for a full post-midsem pass, daily numericals, and past papers at the end.

Exam note: The highest-yield plan mirrors the named question sources directly: past-paper patterns + Assignment 2 theory + diffusion/EBM numericals + BERT/GPT concepts + vision-language tasks.

Key Industry Applications

  • Sentiment analysis and question answering built on bidirectional understanding models (BERT family) — the engines behind search relevance, support-ticket routing, and review mining.
  • Text generation driven by decoder models (GPT family), with zero-shot and few-shot prompting replacing task-specific fine-tuning — one deployed model now serves drafting, summarising, and editing through different prompts.
  • Periodic large-scale pre-training on TPUs at Google; cheap downstream fine-tuning as the deployment pattern — pre-train once a year or so, adapt many times daily.
  • Social media likes and comments harvested as weak labels for vision-language fine-tuning data — users annotate at scale without ever filling a form.
  • Image captioning and visual question answering in consumer AI products — accessibility describers for visually impaired users, photo-search assistants.
  • Image colorization and inpainting for photo restoration, powered by GANs and diffusion models — reviving archives and retouching images with zero per-photo labelling cost.
  • Cross-modal retrieval: searching image databases with text queries and vice versa — stock photography search, e-commerce "find this product from a photo".
  • The compute-and-data moat of Amazon, Google, and OpenAI versus the rising open-source alternative ecosystem, including competitive models out of China — the practical choice facing most teams is whose checkpoint to fine-tune, not whether to train from scratch.

Exam note: Each application maps back to a lecture concept — sentiment/QA to BERT's bidirectional encoding, prompting to GPT's zero/few-shot regime, restoration to self-supervised corruption-and-repair, retrieval to CLS-style joint scoring. Linking application to mechanism is the examinable move.

UDL Lecture 16 notes · BERT, GPT, and Vision-Language Models

Unsupervised Deep Learning· postgraduate· 2026-08-25

Sections Breakdown

1Exam Preparation Road Map

What the comprehensive exam covers: past papers, Assignment 2 theory, calculator rules, refreshed network basics, and a study plan.

2BERT: Bidirectional Transformers for Language Understanding

Bidirectional encoder architecture, masked language modelling and next sentence prediction, WordPiece input representation, and QA fine-tuning.

3RoBERTa: An Optimized BERT

Three pre-training recipe changes: dropping NSP, dynamic multi-location masking, and a bigger corpus with no architecture change.

4BERT versus GPT: Understanding versus Generation

Encoder-versus-decoder comparison across attention direction, purpose, and adaptation, plus the stock photography analogy for pre-training.

5GPT-2 and GPT-3: Scale and Prompting

Scaling to 175 billion parameters, zero-shot instruction prompts, few-shot demonstrations, and the data and compute picture.

6Vision-Language Tasks: What Modern Systems Can Do

Captioning, visual question answering, and text-to-image retrieval as joint tokenization problems, with video as the next boundary.

7Classical Visual Captioning with CNN plus RNN

The four-step CNN-to-RNN pipeline on caption pairs, a full trace, and the upgrade paths from LSTMs to attention.

8Self-Supervised Learning for Images

Colorization pairs built by averaging RGB channels and inpainting data manufactured by masking holes in copies of one photo.

9Pre-training on Large, Noisy, Cheap Data

Weak labels from social media, an audited mismatched caption, how spurious phrases train hallucination, and the pre-train then fine-tune recipe.

10From Pixels to Tokens: Region-Based Visual Tokenization

Bounding boxes as visual tokens carrying coordinates, classes, and features, and how joint training teaches spatial words.

11Two-Stream versus Single-Stream Fusion

Late fusion's frozen-representation weakness versus early single-stream fusion, settled by results in favour of early fusion.

12UNITER: Joint Pre-training Objectives

Shared-transformer notation, MLM conditioned on visual regions, masked region regression and classification, image-text matching, and the combined loss.

13Fine-Tuning Tasks for Vision-Language Models

VQA difficulty ceiling, visual entailment's three-way logic, two-image statement checking, commonsense rationales, referring expressions, and retrieval.

14Exam Guidance Summary

The consolidated exam strategy checklist distilled from the opening road map.

15Key Industry Applications

Where each lecture mechanism powers real products, from search relevance to photo restoration and cross-modal retrieval.

Postgraduate students in machine learning

Exam Revision Notes

Below is the distilled, exam-ready core. Every entry comes from the full explanation above. Use this section for rapid review; return to the main notes when a point needs more context.

Exam Preparation Road Map

Must-know: Past-paper patterns repeat; Assignment 2 topics are guaranteed; pre-midsem is only 5-15 percent; core areas are diffusion+EBMs, NLP applications, and vision applications.

⚠️ Top pitfall: Studying only submitted assignment answers without the theory behind each step.

Self-check: What fraction of the paper comes from pre-midsem topics?

Connects to: BERT (16.2), GPT-2 and GPT-3 (16.5), Fine-Tuning Tasks for Vision-Language Models (16.13)

BERT: Bidirectional Transformers for Language Understanding

Must-know: BERT = bidirectional encoder; MLM masks ~15% of tokens and maximizes P(masked word | both-side context); NSP uses 50/50 real/random pairs labeled 1/0; QA fine-tuning learns one-hot start/end vectors over paragraph tokens.

⚠️ Top pitfall: Confusing BERT with an autoregressive/generative model — BERT never predicts from past-only context and cannot generate text.

Self-check: In 'The cat sat on the mat because it was soft', what are s and e for the answer 'on the mat'?

Connects to: BERT versus GPT (16.4), UNITER (16.12), RoBERTa (16.3)

RoBERTa: An Optimized BERT

Must-know: RoBERTa differs from BERT in exactly three recipe choices — drop NSP, dynamic refreshing masks, bigger corpus — with zero architectural changes.

⚠️ Top pitfall: Assuming RoBERTa changed the architecture; every change lives in the training data and masking schedule.

Self-check: Why does dynamic masking act like data augmentation?

Connects to: BERT (16.2), BERT versus GPT (16.4)

BERT versus GPT: Understanding versus Generation

Must-know: Encoder + bidirectional attention = understanding tasks; decoder + masked self-attention (past-only) = generation tasks.

⚠️ Top pitfall: Saying 'bidirectional' loosely without tying it to which transformer block (encoder vs masked-self-attention decoder) enforces the direction.

Self-check: Which family would you fine-tune for extractive question answering, and why?

Connects to: BERT (16.2), GPT-2 and GPT-3 (16.5)

GPT-2 and GPT-3: Scale and Prompting

Must-know: Zero-shot = instruction-only prompting; few-shot = demonstrations folded into the prompt; neither uses RAG or gradient updates — the prompt is a selection signal over pre-trained knowledge.

⚠️ Top pitfall: Believing prompts retrieve external data; they only reshape behaviour of what the model already knows.

Self-check: In the three-demonstration addition prompt, why does the model output 7 for '3 + 4'?

Connects to: BERT versus GPT (16.4), Vision-Language Tasks (16.6)

Vision-Language Tasks: What Modern Systems Can Do

Must-know: Joint modelling = represent image content and text together and connect them; video-text requires sequences of frames.

⚠️ Top pitfall: Treating VQA or retrieval as pure vision problems — they require joint image+text representations by definition.

Self-check: Why can't a single frame support activity recognition?

Connects to: Classical Visual Captioning (16.7), Region-Based Visual Tokenization (16.10)

Classical Visual Captioning with CNN plus RNN

Must-know: Four steps: CNN encode → seed RNN → train next-word on caption pairs → generate autoregressively from start token.

⚠️ Top pitfall: Forgetting the system is supervised (needs paired captions), unlike the self-supervised colourization/inpainting tasks.

Self-check: What role does the 7x7x512 feature map play before the RNN ever runs?

Connects to: Self-Supervised Learning for Images (16.8), Region-Based Visual Tokenization (16.10)

Self-Supervised Learning for Images

Must-know: Grey input = (R+G+B)/3 per pixel; inpainting = train generative model (GAN/diffusion) to fill masked regions; both need no human labels.

⚠️ Top pitfall: Thinking self-supervised means unsupervised in the 'no signal' sense — the signal comes from corrupting the input itself.

Self-check: How many training pairs can inpainting extract from a single original photo?

Connects to: BERT (16.2), Pre-training on Large, Noisy, Cheap Data (16.9)

Pre-training on Large, Noisy, Cheap Data

Must-know: Hallucination = model emitting confident claims about things the pixels never support, traced to spurious training captions; fix via caption curation + fine-tuning on clean labels.

⚠️ Top pitfall: Blaming the architecture for hallucinations when the training pairs themselves contain unsupported caption words.

Self-check: In the Thailand case study, which caption phrases were grounded and which were spurious?

Connects to: Self-Supervised Learning for Images (16.8), Region-Based Visual Tokenization (16.10)

From Pixels to Tokens: Region-Based Visual Tokenization

Must-know: Each visual token = normalized box coordinates + class + features; spatial prepositions are learned from relative box geometry; bad detections corrupt every downstream task.

⚠️ Top pitfall: Forgetting normalization makes coordinates fractional in [0,1], not pixel values.

Self-check: How does a model learn what 'on' means without any explicit definition?

Connects to: Pre-training on Large, Noisy, Cheap Data (16.9), Two-Stream versus Single-Stream Fusion (16.11)

Two-Stream versus Single-Stream Fusion

Must-know: Early (single-stream) fusion beats late (two-stream) fusion because cross-modal relationships form at lower levels.

⚠️ Top pitfall: Assuming more specialised per-modality encoders are better — freezing modality representations before they meet hurts alignment.

Self-check: Why does 'young person' vs 'young man' hurt late fusion specifically?

Connects to: Region-Based Visual Tokenization (16.10), UNITER (16.12)

UNITER: Joint Pre-training Objectives

Must-know: Combined pre-training loss = L_MLM + L_MRM + L_ITM; MRM splits into box-coordinate regression and class classification with CE plus KL; MLM conditions on surviving text AND all K visual regions.

⚠️ Top pitfall: Forgetting that UNITER's MLM conditions on visual region tokens too, unlike pure-text BERT MLM.

Self-check: What two heads make up masked region modelling, and what does each predict?

Connects to: Two-Stream versus Single-Stream Fusion (16.11), Fine-Tuning Tasks for Vision-Language Models (16.13)

Fine-Tuning Tasks for Vision-Language Models

Must-know: Entailment needs proof, neutral means consistent-without-implication, contradiction means refuted; poor detector tokens cap every downstream task.

⚠️ Top pitfall: Treating 'neutral' as uncertainty — it is a positive judgement that the image neither proves nor refutes the hypothesis.

Self-check: Why does 'how many pizza slices' require genuine perception rather than caption knowledge?

Connects to: UNITER (16.12), Region-Based Visual Tokenization (16.10), the Exam Preparation Road Map (16.1)

Exam Guidance Summary

Must-know: Past-paper patterns repeat; Assignment 2 topics guaranteed; pre-midsem only 5-15 percent; core areas: diffusion+EBMs, NLP applications, vision applications.

⚠️ Top pitfall: Memorising assignment submissions without the theory behind each step.

Self-check: Which three areas are explicitly named as likely question sources?

Connects to: the Exam Preparation Road Map (16.1), BERT (16.2), GPT-2 and GPT-3 (16.5), Fine-Tuning Tasks for Vision-Language Models (16.13)

Key Industry Applications

Must-know: Every named application maps to a lecture mechanism: BERT→understanding tasks, GPT→prompted generation, colorization/inpainting→self-supervision, retrieval→CLS-style scoring.

⚠️ Top pitfall: Listing applications without connecting each to its underlying mechanism.

Self-check: Which family powers sentiment analysis, and which powers text generation?

Connects to: BERT (16.2), GPT-2 and GPT-3 (16.5), Self-Supervised Learning for Images (16.8), Fine-Tuning Tasks for Vision-Language Models (16.13)

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.