File size: 5,251 Bytes
645fa57 |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inference"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from transformers import(\n",
" EncoderDecoderModel,\n",
" PreTrainedTokenizerFast,\n",
" # XLMRobertaTokenizerFast,\n",
" BertJapaneseTokenizer,\n",
" BertTokenizerFast,\n",
")\n",
"\n",
"import torch\n",
"import csv"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"The tokenizer class you load from this checkpoint is not the same type as the class this function is called from. It may result in unexpected tokenization. \n",
"The tokenizer class you load from this checkpoint is 'GPT2Tokenizer'. \n",
"The class this function is called from is 'PreTrainedTokenizerFast'.\n"
]
}
],
"source": [
"encoder_model_name = \"cl-tohoku/bert-base-japanese-v2\"\n",
"decoder_model_name = \"skt/kogpt2-base-v2\"\n",
"\n",
"src_tokenizer = BertJapaneseTokenizer.from_pretrained(encoder_model_name)\n",
"trg_tokenizer = PreTrainedTokenizerFast.from_pretrained(decoder_model_name)\n",
"model = EncoderDecoderModel.from_pretrained(\"./dump/best_model\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'길가메시 토벌전'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text = \"ギルガメッシュ討伐戦\"\n",
"# text = \"ギルガメッシュ討伐戦に行ってきます。一緒に行きましょうか?\"\n",
"\n",
"def translate(text_src):\n",
" embeddings = src_tokenizer(text_src, return_attention_mask=False, return_token_type_ids=False, return_tensors='pt')\n",
" embeddings = {k: v for k, v in embeddings.items()}\n",
" output = model.generate(**embeddings)[0, 1:-1]\n",
" text_trg = trg_tokenizer.decode(output.cpu())\n",
" return text_trg\n",
"\n",
"print(translate(text))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Evaluation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction\n",
"smoothie = SmoothingFunction().method4"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Testing: 0%| | 0/267 [00:00<?, ?it/s]/home/tikim/.local/lib/python3.8/site-packages/transformers/generation/utils.py:1288: UserWarning: Using `max_length`'s default (20) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
" warnings.warn(\n",
"Testing: 100%|██████████| 267/267 [01:01<00:00, 4.34it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bleu score: 0.9619225967540574\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"from statistics import mean\n",
"\n",
"bleu = []\n",
"f1 = []\n",
"\n",
"DATA_ROOT = './output'\n",
"FILE_JP_KO_TEST = 'ja_ko_test.csv'\n",
"FILE_FFAC_TEST = 'ffac_test.csv'\n",
"\n",
"with torch.no_grad(), open(f'{DATA_ROOT}/{FILE_FFAC_TEST}', 'r') as fd:\n",
"# with torch.no_grad(), open(f'{DATA_ROOT}/{FILE_JP_KO_TEST}', 'r') as fd:\n",
" reader = csv.reader(fd)\n",
" next(reader)\n",
" datas = [row for row in reader] \n",
"\n",
" for data in tqdm(datas, \"Testing\"):\n",
" input, label = data\n",
" embeddings = src_tokenizer(input, return_attention_mask=False, return_token_type_ids=False, return_tensors='pt')\n",
" embeddings = {k: v for k, v in embeddings.items()}\n",
" with torch.no_grad():\n",
" output = model.generate(**embeddings)[0, 1:-1]\n",
" preds = trg_tokenizer.decode(output.cpu())\n",
"\n",
" bleu.append(sentence_bleu([label.split()], preds.split(), weights=[1,0,0,0], smoothing_function=smoothie))\n",
"\n",
"print(f\"Bleu score: {mean(bleu)}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|