§11.3Going Beyond Text: Vision & Tables

Part III NLPT pp. 84–91 · ~5 min read

  • vision transformer
  • patch embedding
  • multimodal

Text has taken transformers a long way, but training only on text has limits: reporting bias (text frequencies ≠ real frequencies), missing common sense (rarely written down), unreliable facts, and — the root issue — no other modality (audio, vision, tables). Solving modality could address the rest. This section (and the next) show transformers branching out.

Vision

Convolutional nets (CNNs) long owned vision, but transformers have caught up.

iGPT (“image GPT”) applies the GPT recipe to pixels: treat an image as a sequence of pixels and autoregressively predict the next one. Pretrained on large image sets, it can “autocomplete” a partial image, and adding a classification head makes it a solid classifier.

ViT

Vision Transformer (ViT) is the BERT-style counterpart. The trick is turning an image into a sequence of tokens:

image → 9 patchesLinearpatch embeddings++ positionsTransformer encoderclass
Figure 11-9 (recreated). ViT: split the image into patches, linearly embed each (a patch embedding), add positions, and run a standard transformer encoder.

Each patch embedding is the linear projection of a patch — ViT’s analogue of a token embedding. After that, it’s the same encoder we built in §3.4:

encoder block× 12
patch representations
patch + position embeddings

Click a sublayer to see what it does. Data flows bottom → top; the output has the same shape as the input.

ViT is just the §3.4 transformer encoder — fed image patches instead of word tokens.

Pretraining masks some patches and predicts their average color (a visual masked-language-modeling). ViT doesn’t beat CNNs on small data (ImageNet) but scales better on large datasets. Using it is a one-liner — the image-classification pipeline classifies a photo of a dog:

from transformers import pipeline
image_classifier = pipeline("image-classification")
preds = image_classifier(image)   # a photo of a husky
ViT image-classification output for a photo of a dog (top-5 probabilities).
Probability
Eskimo dog, husky0.644
Siberian husky0.207
dingo0.060
Norwegian elkhound0.035
malamute0.013
Dotted-underlined cells have explanations — click one.

A natural next step is video — models like TimeSformer add a temporal attention dimension on top of the spatial one.

Tables

Lots of data lives in tables, not prose. TAPAS (“Table Parser”) does question answering over tables by feeding the flattened table and the query into a transformer. Take this table of contents:

A fictitious table of contents — the structured data we want to query in natural language.
namestart_pageend_pagenumber_of_pages
0Introduction11110
1Text classification124836
2Named Entity Recognition497324
3Question Answering7412046
4Summarization12114019
5Conclusion1411443
Dotted-underlined cells have explanations — click one.

TAPAS answers natural-language questions about it — and crucially predicts an aggregator (NONE / SUM / AVERAGE / COUNT) when the answer needs computation:

TAPAS answers natural-language questions over the table — and picks an aggregator when needed.

1What’s the topic in chapter 4?

Answer: Summarization. Aggregator: NONE — the answer is a single cell read directly from the table, no computation.

step 1 / 4

The magic is letting a non-programmer ask “how many chapters have more than 20 pages?” instead of writing Pandas. TAPAS is arguably multimodal — but the table is still treated as text. Genuinely combining two modalities is the subject of speech-to-text and vision-plus-text.

Check yourself: vision & tables

1.What is a fundamental limitation of training only on text?

2.How does iGPT apply the transformer to images?

3.How does ViT turn an image into something a transformer encoder can process?

4.How does ViT compare to CNNs?

5.What does TAPAS do, and what makes it special for tables?

6.For 'What is the total number of pages?', TAPAS answers 'SUM > 10, 36, 24, 46, 19, 3'. What is it doing?

6 questions