--- license: cc-by-sa-4.0 language: ja pipeline_tag: zero-shot-classification library_name: sentence-transformers tags: - cross-encoder - tohoku-nlp/bert-base-japanese-v3 - nli - natural-language-inference datasets: - shunk031/jsnli - hpprc/jsick - shunk031/JGLUE --- # Cross-Encoder for Natural Language Inference(NLI) for Japanese This model was trained using [SentenceTransformers](https://sbert.net) [Cross-Encoder](https://www.sbert.net/examples/applications/cross-encoder/README.html) class. This model is based on [tohoku-nlp/bert-base-japanese-v3](https://huggingface.co/tohoku-nlp/bert-base-japanese-v3). ## Training Data The model was trained on following datasets. - [JSNLI](https://nlp.ist.i.kyoto-u.ac.jp/?%E6%97%A5%E6%9C%AC%E8%AA%9ESNLI%28JSNLI%29%E3%83%87%E3%83%BC%E3%82%BF%E3%82%BB%E3%83%83%E3%83%88) - [JNLI](https://github.com/yahoojapan/JGLUE) (only train set) - [JSICK](https://github.com/verypluming/JSICK) (only train set) For a given sentence pair, it will output three scores corresponding to the labels: {0:"entailment", 1:"neutral", 2:"contradiction}. ## Usage Pre-trained models can be used like this: ```python 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)] ``` ## Usage with Transformers AutoModel You can use the model also directly with Transformers library (without SentenceTransformers library): ```python 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) ``` ## Zero-Shot Classification This model can also be used for zero-shot-classification: ```python 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) ``` ## Benchmarks [JGLUE-JNLI](https://github.com/yahoojapan/JGLUE) validation set accuracy: 0.914