shunk031/JGLUE
Updated • 752 • 47
How to use akiFQC/bert-base-japanese-v3_nli-jsnli-jnli-jsick with sentence-transformers:
from sentence_transformers import CrossEncoder
model = CrossEncoder("akiFQC/bert-base-japanese-v3_nli-jsnli-jnli-jsick")
query = "Which planet is known as the Red Planet?"
passages = [
"Venus is often called Earth's twin because of its similar size and proximity.",
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet."
]
scores = model.predict([(query, passage) for passage in passages])
print(scores)This model was trained using SentenceTransformers Cross-Encoder class. This model is based on tohoku-nlp/bert-base-japanese-v3.
The model was trained on following datasets.
For a given sentence pair, it will output three scores corresponding to the labels: {0:"entailment", 1:"neutral", 2:"contradiction}.
Pre-trained models can be used like this:
from sentence_transformers import CrossEncoder
model = CrossEncoder('akiFQC/bert-base-japanese-v3_nli-jsnli')
scores = model.predict([('男はピザを食べています', '男は何かを食べています'), ('黒いレーシングカーが観衆の前から発車します。', '男は誰もいない道を運転しています。')])
#Convert scores to labels
label_mapping = ['entailment', 'neutral', 'contradiction',]
labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]
You can use the model also directly with Transformers library (without SentenceTransformers library):
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-deberta-v3-base')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-v3-base')
features = tokenizer(['男はピザを食べています', '黒いレーシングカーが観衆の前から発車します。'], ['男は何かを食べています', '男は誰もいない道を運転しています。'], 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)
This model can also be used for zero-shot-classification:
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model='akiFQC/bert-base-japanese-v3_nli-jsnli')
sent = "Appleは先程、iPhoneの最新機種について発表しました。"
candidate_labels = ["技術", "スポーツ", "政治"]
res = classifier(sent, candidate_labels)
print(res)
JGLUE-JNLI validation set accuracy: 0.914