How to Use the Cross-Encoder for Natural Language Inference

Mar 16, 2022 | Educational

Natural Language Inference (NLI) is an essential task in NLP that helps determine the relationship between pairs of sentences. Whether it’s identifying contradictions, entailment, or neutrality between sentences, the Cross-Encoder model based on microsoft/deberta-v3-xsmall is a powerful tool. This article guides you on how to use this model effectively, addressing potential issues along the way.

Understanding the Model

The Cross-Encoder model is trained on the SNLI and MultiNLI datasets and can provide you with three scores corresponding to the labels: contradiction, entailment, and neutral. Imagine it as a judge that not only assesses verbal statements but gives them a score based on their coherence.

Performance Metrics

  • Accuracy on SNLI-test dataset: 91.64%
  • Accuracy on MNLI mismatched set: 87.77%

For further evaluation results, you can check the SBERT.net – Pretrained Cross-Encoder.

Usage Instructions

To get started with the pre-trained model, follow these steps:

Using SentenceTransformers

Here’s how you can use the Cross-Encoder from the SentenceTransformers library:

python
from sentence_transformers import CrossEncoder

model = CrossEncoder('cross-encoder/nli-deberta-v3-xsmall')
scores = model.predict([
    ("A man is eating pizza", "A man eats something"), 
    ("A black race car starts up in front of a crowd of people.", "A man is driving down a lonely road.")
])

# Convert scores to labels
label_mapping = ["contradiction", "entailment", "neutral"]
labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]

Think of the model.predict method as a referee in a sports match, assessing competing teams (or sentences) and declaring the outcome based on the performance of each.

Using Transformers Directly

If you prefer to use the Transformers library directly, here’s how:

python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-deberta-v3-xsmall')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-v3-xsmall')

features = tokenizer(
    ["A man is eating pizza", "A black race car starts up in front of a crowd of people."],
    ["A man eats something", "A man is driving down a lonely road."],
    padding=True,
    truncation=True,
    return_tensors='pt'
)

model.eval()
with torch.no_grad():
    scores = model(**features).logits
    label_mapping = ["contradiction", "entailment", "neutral"]
    labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)]
    print(labels)

Here, the tokenizer functions like a skilled translator, converting sentences into understandable formats for the model to digest.

Zero-Shot Classification

This model can also perform zero-shot classification:

python
from transformers import pipeline

classifier = pipeline("zero-shot-classification", model='cross-encoder/nli-deberta-v3-xsmall')
sent = "Apple just announced the newest iPhone X"
candidate_labels = ["technology", "sports", "politics"]

res = classifier(sent, candidate_labels)
print(res)

Consider this step as a talent scout evaluating an athlete (sentence) and matching it with the sport (labels) they excel at, without prior training.

Troubleshooting Tips

As you dive into using the Cross-Encoder model, you may face some challenges. Here are some troubleshooting ideas:

  • Model Not Found: Ensure that you’ve installed the required libraries (`sentence-transformers` and `transformers`).
  • Score Output Confusion: Remember that the output scores correspond to the indices of the labels; use the label mapping correctly.
  • Runtime Errors: Double-check that your input sentences match the expected format and length required by the tokenizer.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox