Views
No views yet
1from transformers import pipeline
2
3classifier = pipeline("zero-shot-classification", model="Formzu/bert-base-japanese-jsnli")
4
5sequence_to_classify = "いつか世界を見る。"
6candidate_labels = ['旅行', '料理', '踊り']
7out = classifier(sequence_to_classify, candidate_labels, hypothesis_template="この例は{}です。")
8print(out)
9#{'sequence': 'いつか世界を見る。',
10# 'labels': ['旅行', '料理', '踊り'],
11# 'scores': [0.6758995652198792, 0.22110949456691742, 0.1029909998178482]}1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
5
6model_name = "Formzu/bert-base-japanese-jsnli"
7model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
8tokenizer = AutoTokenizer.from_pretrained(model_name)
9
10premise = "いつか世界を見る。"
11label = '旅行'
12hypothesis = f'この例は{label}です。'
13
14input = tokenizer.encode(premise, hypothesis, return_tensors='pt').to(device)
15with torch.no_grad():
16 logits = model(input)["logits"][0]
17 probs = logits.softmax(dim=-1)
18 print(probs.cpu().numpy(), logits.cpu().numpy())
19#[0.68940836 0.29482093 0.01577068] [ 1.7791482 0.92968255 -1.998533 ]| Training Loss | Epoch | Step | Validation Loss | Accuracy |
|---|---|---|---|---|
| 0.4054 | 1.0 | 16657 | 0.2141 | 0.9216 |
| 0.3297 | 2.0 | 33314 | 0.2145 | 0.9236 |
| 0.2645 | 3.0 | 49971 | 0.2085 | 0.9288 |