File size: 3,159 Bytes
d2ee7b8
 
a7b372a
 
 
809eee2
 
 
 
a7b372a
 
 
d2ee7b8
a7b372a
 
809eee2
 
 
 
 
a7b372a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809eee2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
license: cc-by-sa-4.0
language: ja
pipeline_tag: zero-shot-classification
tags:
  - cross-encoder
  - tohoku-nlp/bert-base-japanese-v3
  - nli
  - natural-language-inference
datasets:
- shunk031/jsnli
library_name: sentence-transformers
---


# Cross-Encoder for Natural Language Inference(NLI) for Japanese

> [!NOTE]
> Considering the results of the JNLI evaluation result, we recommend using [akiFQC/bert-base-japanese-v3_nli-jsnli-jnli-jsick](https://huggingface.co/akiFQC/bert-base-japanese-v3_nli-jsnli-jnli-jsick) for natural language inference in 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 the [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) datasets. 
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 validation set accuracy: 0.770