Views
No views yet
1from transformers import pipeline
2
3model_name = "tsmatz/roberta_qa_japanese"
4qa_pipeline = pipeline(
5 "question-answering",
6 model=model_name,
7 tokenizer=model_name)
8result = qa_pipeline(
9 question = "決勝トーナメントで日本に勝ったのはどこでしたか。",
10 context = "日本は予選リーグで強豪のドイツとスペインに勝って決勝トーナメントに進んだが、クロアチアと対戦して敗れた。",
11 align_to_words = False,
12)
13print(result)1import torch
2import numpy as np
3from transformers import AutoModelForQuestionAnswering, AutoTokenizer
4
5model_name = "tsmatz/roberta_qa_japanese"
6model = (AutoModelForQuestionAnswering
7 .from_pretrained(model_name))
8tokenizer = AutoTokenizer.from_pretrained(model_name)
9
10def inference_answer(question, context):
11 question = question
12 context = context
13 test_feature = tokenizer(
14 question,
15 context,
16 max_length=318,
17 )
18 with torch.no_grad():
19 outputs = model(torch.tensor([test_feature["input_ids"]]))
20 start_logits = outputs.start_logits.cpu().numpy()
21 end_logits = outputs.end_logits.cpu().numpy()
22 answer_ids = test_feature["input_ids"][np.argmax(start_logits):np.argmax(end_logits)+1]
23 return "".join(tokenizer.batch_decode(answer_ids))
24
25question = "決勝トーナメントで日本に勝ったのはどこでしたか。"
26context = "日本は予選リーグで強豪のドイツとスペインに勝って決勝トーナメントに進んだが、クロアチアと対戦して敗れた。"
27answer_pred = inference_answer(question, context)
28print(answer_pred)| Training Loss | Epoch | Step | Validation Loss |
|---|---|---|---|
| 2.1293 | 0.13 | 150 | 1.0311 |
| 1.1965 | 0.26 | 300 | 0.6723 |
| 1.022 | 0.39 | 450 | 0.4838 |
| 0.9594 | 0.53 | 600 | 0.5174 |
| 0.9187 | 0.66 | 750 | 0.4671 |
| 0.8229 | 0.79 | 900 | 0.4650 |
| 0.71 | 0.92 | 1050 | 0.2648 |
| 0.5436 | 1.05 | 1200 | 0.2665 |
| 0.5045 | 1.19 | 1350 | 0.2686 |
| 0.5025 | 1.32 | 1500 | 0.2082 |
| 0.5213 | 1.45 | 1650 | 0.1715 |
| 0.4648 | 1.58 | 1800 | 0.1563 |
| 0.4698 | 1.71 | 1950 | 0.1488 |
| 0.4823 | 1.84 | 2100 | 0.1050 |
| 0.4482 | 1.97 | 2250 | 0.0821 |
| 0.2755 | 2.11 | 2400 | 0.0898 |
| 0.2834 | 2.24 | 2550 | 0.0964 |
| 0.2525 | 2.37 | 2700 | 0.0533 |
| 0.2606 | 2.5 | 2850 | 0.0561 |
| 0.2467 | 2.63 | 3000 | 0.0601 |
| 0.2799 | 2.77 | 3150 | 0.0562 |
| 0.2497 | 2.9 | 3300 | 0.0516 |