Sub topic 4
Pretrained Encoders as the Backbone
This chapter assumes basic Python, familiarity with tensors, and a general idea of neural networks, pretrained language models, and tokenization. You should also know that a tokenizer converts text into token IDs that a model can process.
What a pretrained encoder does
A pretrained encoder is a neural network that converts an input sequence into a contextual representation for each token. A contextual representation is a vector whose meaning depends on the surrounding tokens.
For example, the word “bank” has different meanings in these sentences:
- “She deposited money at the bank.”
- “They sat beside the river bank.”
The encoder reads the complete sequence and gives “bank” a representation that reflects its context. It does this with transformer layers and self-attention. Self-attention lets each token compare itself with other tokens in the input.
A pretrained encoder has already learned language patterns from a large collection of text. It has learned relationships such as which words commonly occur together and how grammatical structures fit together. Question answering then adds a smaller task-specific component instead of learning language from the beginning.
The encoder in extractive question answering
An extractive question-answering model receives two inputs:
- A question.
- A context passage.
The tokenizer combines them into one sequence. Special tokens mark the boundaries. The encoder processes the complete pair and produces one hidden vector per input token.
A question-answering head reads these vectors. It produces two scores for every token:
- A start score: how likely the token is to begin the answer.
- An end score: how likely the token is to finish the answer.
The highest-scoring valid pair identifies a span in the original context. The model does not normally create new wording. It selects text that already appears in the passage.
from transformers import AutoTokenizer, AutoModel
name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(name)
encoder = AutoModel.from_pretrained(name)
question = "Where is the Amazon River?"
context = "The Amazon River is in South America."
inputs = tokenizer(question, context, return_tensors="pt")
outputs = encoder(**inputs)
print(inputs["input_ids"].shape)
print(outputs.last_hidden_state.shape)last_hidden_state has the shape (batch_size, sequence_length, hidden_size). The first dimension supports multiple examples. The second contains one vector for each token. The third is the size of each contextual vector.
A question-answering model uses the same kind of encoder, followed by a span head. The pretrained encoder supplies language understanding; the span head converts that understanding into start and end predictions.
Encoder versus decoder and retriever
An encoder reads the input to build representations. A decoder generates a sequence one token at a time. A retrieval system searches a collection for relevant passages. These approaches can work together, but they solve different problems.
| Component | Main output | Role in extractive QA |
|---|---|---|
| Encoder | Contextual vectors | Understands the question and passage |
| Span head | Start and end scores | Selects the answer boundaries |
| Retriever | Relevant documents | Finds passages before extraction |
| Decoder | Generated text | Produces an answer in generative QA |
Common errors
A pretrained encoder does not automatically know the correct answer format. The model must be fine-tuned for question answering so that its span head learns how to use the encoder’s vectors.
Do not treat hidden vectors as answer text. They are numerical representations. The tokenizer’s offset mapping is needed to connect predicted token positions back to character positions in the original context.
Also, the encoder can only process a limited number of tokens. A long passage may need overlapping windows, and each window must be scored separately.
Exercise
Why does extractive question answering need a span prediction head after the pretrained encoder?
Show answer
The encoder produces contextual vectors, while the span head turns those vectors into start and end scores that identify a text span in the context.