Views
No views yet
1# !pip install transformers sentencepiece --quiet
2import torch
3from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
5model_id = 'Marwolaeth/rubert-tiny-nli-terra-v1'
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForSequenceClassification.from_pretrained(model_id)
8if torch.cuda.is_available():
9 model.cuda()
10
11# An example from cointegrated NLI models
12premise1 = 'Сократ - человек, а все люди смертны.'
13hypothesis1 = 'Сократ никогда не умрёт.'
14with torch.inference_mode():
15 prediction = model(
16 **tokenizer(premise1, hypothesis1, return_tensors='pt').to(model.device)
17 )
18 p = torch.softmax(prediction.logits, -1).cpu().numpy()[0]
19print({v: p[k] for k, v in model.config.id2label.items()})
20# {'not_entailment': 0.68763, 'entailment': 0.31237}
21
22# An example concerning sentiments
23premise2 = 'Мне не нравятся желтые ковры.'
24hypothesis2 = 'Я люблю желтые ковры.'
25with torch.inference_mode():
26 prediction = model(
27 **tokenizer(premise2, hypothesis2, return_tensors='pt').to(model.device)
28 )
29 p = torch.softmax(prediction.logits, -1).cpu().numpy()[0]
30print({v: p[k] for k, v in model.config.id2label.items()})
31# {'not_entailment': 0.5894801, 'entailment': 0.41051993}
32
33# A tricky example
34# Many NLI models fail to refute premise-hypothesis pairs like:
35# 'It is good for our enemies that X' — 'It is good for us that X'
36# This contradiction is quite clear, yet many NLI models struggle to accurately identify it,
37# highlighting their limitations in understanding conflicting sentiments in natural language inference.
38premise3 = 'Для наших врагов хорошо, что это дерево красное.'
39hypothesis3 = 'Для нас хорошо, что это дерево красное.'
40with torch.inference_mode():
41 prediction = model(
42 **tokenizer(premise3, hypothesis3, return_tensors='pt').to(model.device)
43 )
44 p = torch.softmax(prediction.logits, -1).cpu().numpy()[0]
45print({v: p[k] for k, v in model.config.id2label.items()})
46# {'not_entailment': 0.54253, 'entailment': 0.45746994}| Metric | Value |
|---|---|
| Validation Loss | 0.6492 |
| Validation Accuracy | 67.43% |
| Validation F1 Score | 67.11% |
| Validation Precision | 67.55% |
| Validation Recall | 66.67% |
| Validation Runtime* | 0.2631 seconds |
| Samples per Second* | 1 167.02 |
| Steps per Second* | 7.60 |