Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3import os
4
5MODEL_NAME = "ce-lery/japanese-mistral-300m-instruction"
6torch.set_float32_matmul_precision('high')
7
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9print(device)
10
11tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False,trust_remote_code=True)
12model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,trust_remote_code=True).to(device)
13
14MAX_ASSISTANT_LENGTH = 100
15MAX_INPUT_LENGTH = 128
16INPUT_PROMPT = r'<s>\n以下は、タスクを説明する指示と、文脈のある入力の組み合わせです。要求を適切に満たす応答を書きなさい。\n[SEP]\n指示:\n{instruction}\n[SEP]\n入力:\n{input}\n[SEP]\n応答:\n'
17NO_INPUT_PROMPT = r'<s>\n以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。\n[SEP]\n指示:\n{instruction}\n[SEP]\n応答:\n'
18
19def prepare_input(instruction, input_text):
20 if input_text != "":
21 prompt = INPUT_PROMPT.format(instruction=instruction, input=input_text)
22 else:
23 prompt = NO_INPUT_PROMPT.format(instruction=instruction)
24 return prompt
25
26def format_output(output):
27 output = output.lstrip("<s>").rstrip("</s>").replace("[SEP]", "").replace("\\n", "\n")
28 return output
29
30def generate_response(instruction, input_text):
31 prompt = prepare_input(instruction, input_text)
32 token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
33 n = len(token_ids[0])
34 # print(n)
35
36 with torch.no_grad():
37 output_ids = model.generate(
38 token_ids.to(model.device),
39 min_length=n,
40 max_length=min(MAX_INPUT_LENGTH, n + MAX_ASSISTANT_LENGTH),
41 top_p=0.95,
42 top_k=50,
43 temperature=0.4,
44 do_sample=True,
45 no_repeat_ngram_size=2,
46 num_beams=3,
47 pad_token_id=tokenizer.pad_token_id,
48 bos_token_id=tokenizer.bos_token_id,
49 eos_token_id=tokenizer.eos_token_id,
50 bad_words_ids=[[tokenizer.unk_token_id]]
51 )
52
53 output = tokenizer.decode(output_ids.tolist()[0])
54 formatted_output_all = format_output(output)
55 response = f"Assistant:{formatted_output_all.split('応答:')[-1].strip()}"
56
57 return formatted_output_all, response
58
59instruction = "あなたは何でも正確に答えられるAIです。"
60questions = [
61 "日本で一番高い山は?",
62 "日本で一番広い湖は?",
63 "世界で一番高い山は?",
64 "世界で一番広い湖は?",
65 "冗談を言ってください。",
66]
67
68# 各質問に対して応答を生成して表示
69for question in questions:
70 formatted_output_all, response = generate_response(instruction, question)
71 print(response)
72| Training Loss | Epoch | Step | Validation Loss |
|---|---|---|---|
| 3.595 | 3.51 | 40 | 3.5299 |
| 3.4769 | 7.02 | 80 | 3.3722 |
| 3.3037 | 10.53 | 120 | 3.1871 |
| 3.1255 | 14.05 | 160 | 3.0088 |
| 2.9615 | 17.56 | 200 | 2.8684 |
| 2.8468 | 21.07 | 240 | 2.7808 |
| 2.7699 | 24.58 | 280 | 2.7205 |
| 2.7139 | 28.09 | 320 | 2.6793 |
| 2.6712 | 31.6 | 360 | 2.6509 |
| 2.6356 | 35.12 | 400 | 2.6294 |
| 2.6048 | 38.63 | 440 | 2.6120 |
| 2.5823 | 42.14 | 480 | 2.5974 |
| 2.5536 | 45.65 | 520 | 2.5849 |
| 2.5293 | 49.16 | 560 | 2.5740 |
| 2.5058 | 52.67 | 600 | 2.5644 |
| 2.482 | 56.19 | 640 | 2.5556 |
| 2.4575 | 59.7 | 680 | 2.5477 |
| 2.4339 | 63.21 | 720 | 2.5405 |
| 2.4073 | 66.72 | 760 | 2.5350 |
| 2.3845 | 70.23 | 800 | 2.5303 |
| 2.3606 | 73.74 | 840 | 2.5253 |
| 2.329 | 77.26 | 880 | 2.5215 |
| 2.3071 | 80.77 | 920 | 2.5185 |
| 2.2768 | 84.28 | 960 | 2.5155 |
| 2.2479 | 87.79 | 1000 | 2.5144 |
| 2.2181 | 91.3 | 1040 | 2.5151 |
| 2.1901 | 94.81 | 1080 | 2.5139 |
| 2.1571 | 98.33 | 1120 | 2.5148 |
| 2.1308 | 101.84 | 1160 | 2.5166 |
| 2.1032 | 105.35 | 1200 | 2.5193 |
| 2.0761 | 108.86 | 1240 | 2.5204 |
| 2.0495 | 112.37 | 1280 | 2.5269 |
| 2.0231 | 115.88 | 1320 | 2.5285 |
| 2.0021 | 119.4 | 1360 | 2.5328 |
| 1.9793 | 122.91 | 1400 | 2.5383 |
| 1.9575 | 126.42 | 1440 | 2.5442 |
| 1.9368 | 129.93 | 1480 | 2.5488 |
| 1.9216 | 133.44 | 1520 | 2.5534 |
| 1.902 | 136.95 | 1560 | 2.5584 |
| 1.8885 | 140.47 | 1600 | 2.5609 |
| 1.8728 | 143.98 | 1640 | 2.5657 |
| 1.8605 | 147.49 | 1680 | 2.5697 |
| 1.8476 | 151.0 | 1720 | 2.5741 |
| 1.8402 | 154.51 | 1760 | 2.5770 |
| 1.8274 | 158.02 | 1800 | 2.5803 |
| 1.8218 | 161.54 | 1840 | 2.5829 |
| 1.8144 | 165.05 | 1880 | 2.5847 |
| 1.8097 | 168.56 | 1920 | 2.5867 |
| 1.8076 | 172.07 | 1960 | 2.5883 |
| 1.8014 | 175.58 | 2000 | 2.5892 |
| 1.8001 | 179.09 | 2040 | 2.5899 |
| 1.7987 | 182.61 | 2080 | 2.5903 |
| 1.7971 | 186.12 | 2120 | 2.5906 |
| 1.7979 | 189.63 | 2160 | 2.5907 |
| 1.7975 | 193.14 | 2200 | 2.5907 |