Sub topic 3
Natural Language Inference: Premise, Hypothesis, and Entailment
This chapter assumes basic Python, familiarity with supervised text classification, and a general idea of pretrained language models. You will learn the reasoning task that makes zero-shot classification possible.
The NLI task
Natural language inference (NLI) is a task that compares two pieces of text:
- A premise is the original statement or document.
- A hypothesis is a statement whose relationship to the premise must be judged.
- The model predicts whether the premise entails, contradicts, or is neutral toward the hypothesis.
| NLI result | Meaning | Example |
|---|---|---|
| Entailment | The premise supports the hypothesis | “The package arrived today.” → “The package was delivered.” |
| Contradiction | The premise conflicts with the hypothesis | “The package arrived today.” → “The package has not arrived.” |
| Neutral | The premise does not provide enough information | “The package arrived today.” → “The package was expensive.” |
An NLI model is usually a pretrained transformer encoder followed by a classification layer. The encoder reads both texts together and produces a representation of their relationship. During training, the model learns language patterns that indicate support, conflict, or missing information.
Reformulating classification as entailment
Suppose you want to classify this customer message:
> I was charged twice for my subscription this month.
Your categories are billing, technical support, and sales. Instead of asking a model trained specifically on those categories, turn each category into a hypothesis:
- “This message is about billing.”
- “This message is about technical support.”
- “This message is about sales.”
The premise is the customer message. The hypothesis is one candidate label expressed as a sentence. The label with the strongest entailment score becomes the prediction.
This is called zero-shot classification because the NLI model was not trained with your particular category names or labeled examples. It was trained to recognize relationships between text pairs. Your application reuses that ability for a new classification task.
Inspecting an NLI score
The Hugging Face model facebook/bart-large-mnli returns logits for contradiction, neutral, and entailment. A logit is an unnormalized score produced by the model. A softmax converts selected logits into probabilities that sum to one.
The following code reproduces the entailment probability for one premise and hypothesis:
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
name = "facebook/bart-large-mnli"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name)
premise = "I was charged twice for my subscription this month."
hypothesis = "This message is about billing."
inputs = tokenizer(premise, hypothesis, return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits[0]
entail = model.config.label2id["entailment"]
contra = model.config.label2id["contradiction"]
probability = torch.softmax(logits[[contra, entail]], dim=0)[1].item()
print(f"Entailment probability: {probability:.3f}")The two-class softmax above compares contradiction and entailment, which matches the common scoring approach used for candidate labels. Neutral is not included in that comparison. The probability is therefore useful for ranking candidate labels, not as a universal measure of real-world certainty.
Why wording matters
The model evaluates the complete hypothesis, not just the raw label. “Billing” and “This message concerns a payment or charge” can produce different scores. Later, you can test hypothesis templates and choose wording that matches your data.
Exercise
For the premise “The server returns a 500 error after login,” write one hypothesis that should be entailed and one that should be contradicted.
Show answer
Entailed: “This report is about a technical problem.” Contradicted: “This report is about a successful login.”