Sentence Meaning and Natural Language Inference › How NLI Models Work

Michael BrenndoerferMay 9, 20256 min read
Language AI HandbookAll chapters
Part 8 of 67Semantics and Information Extraction
Chapter 51Sentence Meaning and Natural Language Inference › How NLI Models Work
Reading section 7 of 12 · How NLI Models Work · 6 minSection 7 of 12 · 6 min

Section 7 of 12 · 6 min read

Sentence Meaning and Natural Language Inference › How NLI Models WorkSection view · Sections in this chapter7 / 12 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

NLI models have evolved from feature-based classifiers to large pretrained transformers. The core problem remains the same: represent the relationship between two sentences.

Classical Feature-Based Models

Earlier RTE systems often combined hand-engineered features. For example, MENT's RTE-2 system used syntactic and thesaurus features with an alignment score. A feature-based baseline can combine:

  • Lexical overlap between premise and hypothesis.
  • Word alignment and edit distance.
  • Syntactic parse features.
  • Named entity overlap.
  • Negation detection.
  • A text-similarity feature, building on TF-IDF or BM25. Such a score measures a form of similarity or relevance, not entailment by itself.

A logistic regression or SVM classifier could combine these features. Their limitations depend on the chosen features: a word-count representation omits order, while explicit syntax or negation features can preserve some distinctions. A classifier that succeeds through lexical overlap still needs tests where overlap and entailment disagree.

Bi-Encoders and Cross-Encoders

Two common designs illustrate different ways to represent the sentence pair.

A bi-encoder encodes the premise and hypothesis separately:

u=fθ(p),v=fθ(h)u = f_\theta(p), \qquad v = f_\theta(h)

where:

  • pp: the premise sentence
  • hh: the hypothesis sentence
  • fθf_\theta: the encoder function parameterized by weights θ\theta
  • uu: the encoded representation (vector) of the premise
  • vv: the encoded representation (vector) of the hypothesis

It then combines the two vectors, for example:

z=[u;v;∣u−v∣;u⊙v]z = [u; v; |u-v|; u \odot v]

Here u,v∈Rdu,v \in \mathbb{R}^{d} have the same dimension. Semicolons concatenate vectors, ∣u−v∣|u-v| takes the componentwise absolute difference, and ⊙\odot multiplies corresponding components. The pair vector z∈R4dz \in \mathbb{R}^{4d} has four blocks of dd entries. Its ordered uu and vv blocks can preserve direction even though the difference and product blocks are symmetric.

A classifier uses the combined vector zz. Separate encoding allows sentence vectors to be cached and reused across pairs. It omits direct cross-sentence token interaction during encoding. A sentence encoder can still represent internal negation and role information, but the pair classifier must recover the relation from the fixed vectors.

A cross-encoder concatenates the premise and hypothesis into a single input:

[CLS] p [SEP] h [SEP][\text{CLS}] \ p \ [\text{SEP}] \ h \ [\text{SEP}]

where:

  • [CLS][\text{CLS}]: the special classification token whose final hidden state is used for prediction
  • pp: the token sequence of the premise
  • [SEP][\text{SEP}]: the special separator token marking segment boundaries
  • hh: the token sequence of the hypothesis

A transformer encodes the whole sequence. With an unblocked bidirectional attention mask, self-attention permits each premise token to attend to each hypothesis token. The final hidden state at the [CLS] position, or a pooled representation, is passed to a linear classification head:

pθ(y∣p,h)=softmax⁡(Wh[CLS]+b)yp_\theta(y \mid p,h) = \operatorname{softmax}(W h_{[\text{CLS}]} + b)_y

where:

  • pθ(y∣p,h)p_\theta(y \mid p,h): the predicted probability of label yy given premise pp and hypothesis hh
  • yy: a label in {entailment, contradiction, neutral}
  • h[CLS]h_{[\text{CLS}]}: the final hidden state at the [CLS] position
  • WW: the weight matrix of the linear classification head
  • bb: the bias vector of the classification head
  • softmax⁡(⋅)y\operatorname{softmax}(\cdot)_y: the softmax function evaluated at the entry for label yy

With a pooled hidden state h[CLS]∈Rdh_{[\mathrm{CLS}]} \in \mathbb{R}^{d}, a three-label head uses W∈R3×dW \in \mathbb{R}^{3 \times d} and b∈R3b \in \mathbb{R}^{3}. Its output has one logit per label. This hidden-state symbol is distinct from the hypothesis sentence hh.

A cross-encoder requires joint encoding of each pair, whereas a bi-encoder can reuse an already computed sentence vector. Joint encoding permits token-level interaction across the pair; the resulting accuracy depends on the model, training data, and evaluation set. For a workload comparing many new pairs, this joint computation cannot use a cache of independently encoded sentences in the same way. Compute comparisons still depend on sequence length, architecture, batching, and hardware.

The special tokens [CLS] and [SEP] were introduced in Special Tokens. Their role here is structural: [CLS] aggregates the pair representation, and [SEP] marks the boundary between premise and hypothesis.

Training Objective

For this supervised classifier, take a nonempty dataset of N>0N>0 labeled pairs with one gold label per pair. Minimize the mean cross-entropy loss:

L(θ)=−1N∑i=1Nlog⁡pθ(yi∣pi,hi)\mathcal{L}(\theta) = -\frac{1}{N} \sum_{i=1}^{N} \log p_\theta(y_i \mid p_i, h_i)

where:

  • L(θ)\mathcal{L}(\theta): the cross-entropy loss for model parameters θ\theta
  • NN: the number of labeled pairs in the dataset
  • ii: an index over training pairs
  • pip_i: the premise of the ii-th pair
  • hih_i: the hypothesis of the ii-th pair
  • yiy_i: the gold label of the ii-th pair
  • pθ(yi∣pi,hi)p_\theta(y_i \mid p_i, h_i): the model's predicted probability of the gold label
  • log⁡\log: the natural logarithm

Minimizing this objective rewards higher probability on the supplied gold labels. One training recipe is to fine-tune a pretrained transformer on MNLI, then adapt it to a domain-specific dataset. This is a recipe to evaluate, not a guarantee of transfer.

Class weighting, label smoothing, and adversarial training are optional refinements. Their suitability needs separate evaluation; changing training weights, targets, or examples does not by itself establish semantic generalization. The objective teaches the model to match labels, and if the labels are predictable from shortcuts, the model will happily learn the shortcuts.

LLM-Based NLI

Large language models can perform NLI zero-shot or few-shot. A typical prompt asks:

Premise: ...

Hypothesis: ...

Does the premise entail the hypothesis, contradict it, or neither? Answer with one of: entailment, contradiction, neutral.

There are two ways to score the answer:

  1. Generate the label as text and parse it.
  2. Score the probability of each label token or verbalizer and choose the highest.

Free generation introduces decoding and parsing choices. Temperature zero makes token selection greedy, but does not by itself guarantee reproducible inference across hardware, software, or batching configurations. Prompt wording, demonstration choice, and demonstration order can also change predictions. Scoring label tokens fixes the candidate outputs, but still depends on the verbalizer and tokenization. For example, "entailment" may tokenize into multiple pieces, so score the sequence, not just one token.

Prompting an LLM provides a way to attempt the task without task-specific training examples. It does not establish an advantage over a fine-tuned cross-encoder. Compare model families on the same evaluation protocol and inspect their errors.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy