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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "12d87b30",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Load Data\n",
|
| 9 |
+
"This notebook loads and preproceses all necessary data, namely the following.\n",
|
| 10 |
+
"* OpenWebTextCorpus: for base DistilBERT model\n",
|
| 11 |
+
"* SQuAD datasrt: for Q&A\n",
|
| 12 |
+
"* Natural Questions (needs to be downloaded externally but is preprocessed here): for Q&A\n",
|
| 13 |
+
"* HotPotQA: for Q&A"
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"cell_type": "code",
|
| 18 |
+
"execution_count": 4,
|
| 19 |
+
"id": "7c82d7fa",
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"from tqdm.auto import tqdm\n",
|
| 24 |
+
"from datasets import load_dataset\n",
|
| 25 |
+
"import os\n",
|
| 26 |
+
"import pandas as pd\n",
|
| 27 |
+
"import random"
|
| 28 |
+
]
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"cell_type": "markdown",
|
| 32 |
+
"id": "1737f219",
|
| 33 |
+
"metadata": {},
|
| 34 |
+
"source": [
|
| 35 |
+
"## Distilbert Data\n",
|
| 36 |
+
"In the following, we download the english openwebtext dataset from huggingface (https://huggingface.co/datasets/openwebtext). The dataset is provided by Aaron Gokaslan and Vanya Cohen from Brown University (https://skylion007.github.io/OpenWebTextCorpus/).\n",
|
| 37 |
+
"\n",
|
| 38 |
+
"We first load the data, investigate the structure and write the dataset into files of each 10 000 texts."
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"execution_count": null,
|
| 44 |
+
"id": "cce7623c",
|
| 45 |
+
"metadata": {},
|
| 46 |
+
"outputs": [],
|
| 47 |
+
"source": [
|
| 48 |
+
"ds = load_dataset(\"openwebtext\")"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 4,
|
| 54 |
+
"id": "678a5e86",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [
|
| 57 |
+
{
|
| 58 |
+
"data": {
|
| 59 |
+
"text/plain": [
|
| 60 |
+
"DatasetDict({\n",
|
| 61 |
+
" train: Dataset({\n",
|
| 62 |
+
" features: ['text'],\n",
|
| 63 |
+
" num_rows: 8013769\n",
|
| 64 |
+
" })\n",
|
| 65 |
+
"})"
|
| 66 |
+
]
|
| 67 |
+
},
|
| 68 |
+
"execution_count": 4,
|
| 69 |
+
"metadata": {},
|
| 70 |
+
"output_type": "execute_result"
|
| 71 |
+
}
|
| 72 |
+
],
|
| 73 |
+
"source": [
|
| 74 |
+
"# we have a text-only training dataset with 8 million entries\n",
|
| 75 |
+
"ds"
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"cell_type": "code",
|
| 80 |
+
"execution_count": 5,
|
| 81 |
+
"id": "b141bce7",
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"outputs": [],
|
| 84 |
+
"source": [
|
| 85 |
+
"# create necessary folders\n",
|
| 86 |
+
"os.mkdir('data')\n",
|
| 87 |
+
"os.mkdir('data/original')"
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"cell_type": "code",
|
| 92 |
+
"execution_count": null,
|
| 93 |
+
"id": "ca94f995",
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"outputs": [],
|
| 96 |
+
"source": [
|
| 97 |
+
"# save text in chunks of 10000 samples\n",
|
| 98 |
+
"text = []\n",
|
| 99 |
+
"i = 0\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"for sample in tqdm(ds['train']):\n",
|
| 102 |
+
" # replace all newlines\n",
|
| 103 |
+
" sample = sample['text'].replace('\\n','')\n",
|
| 104 |
+
" \n",
|
| 105 |
+
" # append cleaned sample to all texts\n",
|
| 106 |
+
" text.append(sample)\n",
|
| 107 |
+
" \n",
|
| 108 |
+
" # if we processed 10000 samples, write them to a file and start over\n",
|
| 109 |
+
" if len(text) == 10000:\n",
|
| 110 |
+
" with open(f\"data/original/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 111 |
+
" f.write('\\n'.join(text))\n",
|
| 112 |
+
" text = []\n",
|
| 113 |
+
" i += 1 \n",
|
| 114 |
+
"\n",
|
| 115 |
+
"# write remaining samples to a file\n",
|
| 116 |
+
"with open(f\"data/original/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 117 |
+
" f.write('\\n'.join(text))"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"cell_type": "markdown",
|
| 122 |
+
"id": "f131dcfc",
|
| 123 |
+
"metadata": {},
|
| 124 |
+
"source": [
|
| 125 |
+
"### Testing\n",
|
| 126 |
+
"If we load the first file, we should get a file that is 10000 lines long and has one column\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"As we do not preprocess the data in any way, but just write the read text into the file, this is all testing necessary"
|
| 129 |
+
]
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"cell_type": "code",
|
| 133 |
+
"execution_count": 13,
|
| 134 |
+
"id": "df50af74",
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"outputs": [],
|
| 137 |
+
"source": [
|
| 138 |
+
"with open(\"data/original/text_0.txt\", 'r', encoding='utf-8') as f:\n",
|
| 139 |
+
" lines = f.read().split('\\n')\n",
|
| 140 |
+
"lines = pd.DataFrame(lines)"
|
| 141 |
+
]
|
| 142 |
+
},
|
| 143 |
+
{
|
| 144 |
+
"cell_type": "code",
|
| 145 |
+
"execution_count": 14,
|
| 146 |
+
"id": "8ddb0085",
|
| 147 |
+
"metadata": {},
|
| 148 |
+
"outputs": [
|
| 149 |
+
{
|
| 150 |
+
"name": "stdout",
|
| 151 |
+
"output_type": "stream",
|
| 152 |
+
"text": [
|
| 153 |
+
"Passed\n"
|
| 154 |
+
]
|
| 155 |
+
}
|
| 156 |
+
],
|
| 157 |
+
"source": [
|
| 158 |
+
"assert lines.shape==(10000,1)\n",
|
| 159 |
+
"print(\"Passed\")"
|
| 160 |
+
]
|
| 161 |
+
},
|
| 162 |
+
{
|
| 163 |
+
"cell_type": "markdown",
|
| 164 |
+
"id": "1a65b268",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"source": [
|
| 167 |
+
"## SQuAD Data\n",
|
| 168 |
+
"In the following, we download the SQuAD dataset from huggingface (https://huggingface.co/datasets/squad). It was initially provided by Rajpurkar et al. from Stanford University.\n",
|
| 169 |
+
"\n",
|
| 170 |
+
"We again load the dataset and store it in chunks of 1000 into files."
|
| 171 |
+
]
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
"cell_type": "code",
|
| 175 |
+
"execution_count": null,
|
| 176 |
+
"id": "6750ce6e",
|
| 177 |
+
"metadata": {},
|
| 178 |
+
"outputs": [],
|
| 179 |
+
"source": [
|
| 180 |
+
"dataset = load_dataset(\"squad\")"
|
| 181 |
+
]
|
| 182 |
+
},
|
| 183 |
+
{
|
| 184 |
+
"cell_type": "code",
|
| 185 |
+
"execution_count": null,
|
| 186 |
+
"id": "65a7ee23",
|
| 187 |
+
"metadata": {},
|
| 188 |
+
"outputs": [],
|
| 189 |
+
"source": [
|
| 190 |
+
"os.mkdir(\"data/training_squad\")\n",
|
| 191 |
+
"os.mkdir(\"data/test_squad\")"
|
| 192 |
+
]
|
| 193 |
+
},
|
| 194 |
+
{
|
| 195 |
+
"cell_type": "code",
|
| 196 |
+
"execution_count": null,
|
| 197 |
+
"id": "f6ebf63e",
|
| 198 |
+
"metadata": {},
|
| 199 |
+
"outputs": [],
|
| 200 |
+
"source": [
|
| 201 |
+
"# we already have a training and test split. Each sample has an id, title, context, question and answers.\n",
|
| 202 |
+
"dataset"
|
| 203 |
+
]
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"cell_type": "code",
|
| 207 |
+
"execution_count": null,
|
| 208 |
+
"id": "f67ae448",
|
| 209 |
+
"metadata": {},
|
| 210 |
+
"outputs": [],
|
| 211 |
+
"source": [
|
| 212 |
+
"# answers are provided like that - we need to extract answer_end for the model\n",
|
| 213 |
+
"dataset['train']['answers'][0]"
|
| 214 |
+
]
|
| 215 |
+
},
|
| 216 |
+
{
|
| 217 |
+
"cell_type": "code",
|
| 218 |
+
"execution_count": null,
|
| 219 |
+
"id": "101cd650",
|
| 220 |
+
"metadata": {},
|
| 221 |
+
"outputs": [],
|
| 222 |
+
"source": [
|
| 223 |
+
"# column contains the split (either train or validation), save_dir is the directory\n",
|
| 224 |
+
"def save_samples(column, save_dir):\n",
|
| 225 |
+
" text = []\n",
|
| 226 |
+
" i = 0\n",
|
| 227 |
+
"\n",
|
| 228 |
+
" for sample in tqdm(dataset[column]):\n",
|
| 229 |
+
" \n",
|
| 230 |
+
" # preprocess the context and question by removing the newlines\n",
|
| 231 |
+
" context = sample['context'].replace('\\n','')\n",
|
| 232 |
+
" question = sample['question'].replace('\\n','')\n",
|
| 233 |
+
"\n",
|
| 234 |
+
" # get the answer as text and start character index\n",
|
| 235 |
+
" answer_text = sample['answers']['text'][0]\n",
|
| 236 |
+
" answer_start = str(sample['answers']['answer_start'][0])\n",
|
| 237 |
+
" \n",
|
| 238 |
+
" text.append([context, question, answer_text, answer_start])\n",
|
| 239 |
+
"\n",
|
| 240 |
+
" # we choose chunks of 1000\n",
|
| 241 |
+
" if len(text) == 1000:\n",
|
| 242 |
+
" with open(f\"data/{save_dir}/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 243 |
+
" f.write(\"\\n\".join([\"\\t\".join(t) for t in text]))\n",
|
| 244 |
+
" text = []\n",
|
| 245 |
+
" i += 1\n",
|
| 246 |
+
"\n",
|
| 247 |
+
" # save remaining\n",
|
| 248 |
+
" with open(f\"data/{save_dir}/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 249 |
+
" f.write(\"\\n\".join([\"\\t\".join(t) for t in text]))\n",
|
| 250 |
+
"\n",
|
| 251 |
+
"save_samples(\"train\", \"training_squad\")\n",
|
| 252 |
+
"save_samples(\"validation\", \"test_squad\")\n",
|
| 253 |
+
" "
|
| 254 |
+
]
|
| 255 |
+
},
|
| 256 |
+
{
|
| 257 |
+
"cell_type": "markdown",
|
| 258 |
+
"id": "67044d13",
|
| 259 |
+
"metadata": {
|
| 260 |
+
"collapsed": false,
|
| 261 |
+
"jupyter": {
|
| 262 |
+
"outputs_hidden": false
|
| 263 |
+
}
|
| 264 |
+
},
|
| 265 |
+
"source": [
|
| 266 |
+
"### Testing\n",
|
| 267 |
+
"If we load a file, we should get a file with 10000 lines and 4 columns\n",
|
| 268 |
+
"\n",
|
| 269 |
+
"Also, we want to assure the correct interval. Hence, the second test."
|
| 270 |
+
]
|
| 271 |
+
},
|
| 272 |
+
{
|
| 273 |
+
"cell_type": "code",
|
| 274 |
+
"execution_count": null,
|
| 275 |
+
"id": "446281cf",
|
| 276 |
+
"metadata": {},
|
| 277 |
+
"outputs": [],
|
| 278 |
+
"source": [
|
| 279 |
+
"with open(\"data/training_squad/text_0.txt\", 'r', encoding='utf-8') as f:\n",
|
| 280 |
+
" lines = f.read().split('\\n')\n",
|
| 281 |
+
" \n",
|
| 282 |
+
"lines = pd.DataFrame([line.split(\"\\t\") for line in lines], columns=[\"context\", \"question\", \"answer\", \"answer_start\"])"
|
| 283 |
+
]
|
| 284 |
+
},
|
| 285 |
+
{
|
| 286 |
+
"cell_type": "code",
|
| 287 |
+
"execution_count": null,
|
| 288 |
+
"id": "ccd5c650",
|
| 289 |
+
"metadata": {},
|
| 290 |
+
"outputs": [],
|
| 291 |
+
"source": [
|
| 292 |
+
"assert lines.shape==(1000,4)\n",
|
| 293 |
+
"print(\"Passed\")"
|
| 294 |
+
]
|
| 295 |
+
},
|
| 296 |
+
{
|
| 297 |
+
"cell_type": "code",
|
| 298 |
+
"execution_count": null,
|
| 299 |
+
"id": "2c9e4b70",
|
| 300 |
+
"metadata": {},
|
| 301 |
+
"outputs": [],
|
| 302 |
+
"source": [
|
| 303 |
+
"# we assert that we have the right interval\n",
|
| 304 |
+
"for ind, line in lines.iterrows():\n",
|
| 305 |
+
" sample = line\n",
|
| 306 |
+
" answer_start = int(sample['answer_start'])\n",
|
| 307 |
+
" assert sample['context'][answer_start:answer_start+len(sample['answer'])] == sample['answer']\n",
|
| 308 |
+
"print(\"Passed\")"
|
| 309 |
+
]
|
| 310 |
+
},
|
| 311 |
+
{
|
| 312 |
+
"cell_type": "markdown",
|
| 313 |
+
"id": "02265ace",
|
| 314 |
+
"metadata": {},
|
| 315 |
+
"source": [
|
| 316 |
+
"## Natural Questions Dataset\n",
|
| 317 |
+
"* Download from https://ai.google.com/research/NaturalQuestions via gsutil (the one from huggingface has 134.92GB, the one from google cloud is in archives)\n",
|
| 318 |
+
"* Use gunzip to get some samples - we then get `.jsonl`files\n",
|
| 319 |
+
"* The dataset is a lot more messy, as it is just wikipedia articles with all web artifacts\n",
|
| 320 |
+
" * I cleaned the html tags\n",
|
| 321 |
+
" * Also I chose a random interval (containing the answer) from the dataset\n",
|
| 322 |
+
" * We can't send the whole text into the model anyways"
|
| 323 |
+
]
|
| 324 |
+
},
|
| 325 |
+
{
|
| 326 |
+
"cell_type": "code",
|
| 327 |
+
"execution_count": null,
|
| 328 |
+
"id": "f3bce0c1",
|
| 329 |
+
"metadata": {},
|
| 330 |
+
"outputs": [],
|
| 331 |
+
"source": [
|
| 332 |
+
"from pathlib import Path\n",
|
| 333 |
+
"paths = [str(x) for x in Path('data/natural_questions/v1.0/train/').glob('**/*.jsonl')]"
|
| 334 |
+
]
|
| 335 |
+
},
|
| 336 |
+
{
|
| 337 |
+
"cell_type": "code",
|
| 338 |
+
"execution_count": null,
|
| 339 |
+
"id": "e9c58c00",
|
| 340 |
+
"metadata": {},
|
| 341 |
+
"outputs": [],
|
| 342 |
+
"source": [
|
| 343 |
+
"os.mkdir(\"data/natural_questions_train\")"
|
| 344 |
+
]
|
| 345 |
+
},
|
| 346 |
+
{
|
| 347 |
+
"cell_type": "code",
|
| 348 |
+
"execution_count": null,
|
| 349 |
+
"id": "0ed7ba6c",
|
| 350 |
+
"metadata": {},
|
| 351 |
+
"outputs": [],
|
| 352 |
+
"source": [
|
| 353 |
+
"import re\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"# clean html tags\n",
|
| 356 |
+
"CLEANR = re.compile('<.+?>')\n",
|
| 357 |
+
"# clean multiple spaces\n",
|
| 358 |
+
"CLEANMULTSPACE = re.compile('(\\s)+')\n",
|
| 359 |
+
"\n",
|
| 360 |
+
"# the function takes an html documents and removes artifacts\n",
|
| 361 |
+
"def cleanhtml(raw_html):\n",
|
| 362 |
+
" # tags\n",
|
| 363 |
+
" cleantext = re.sub(CLEANR, '', raw_html)\n",
|
| 364 |
+
" # newlines\n",
|
| 365 |
+
" cleantext = cleantext.replace(\"\\n\", '')\n",
|
| 366 |
+
" # tabs\n",
|
| 367 |
+
" cleantext = cleantext.replace(\"\\t\", '')\n",
|
| 368 |
+
" # character encodings\n",
|
| 369 |
+
" cleantext = cleantext.replace(\"'\", \"'\")\n",
|
| 370 |
+
" cleantext = cleantext.replace(\"&\", \"'\")\n",
|
| 371 |
+
" cleantext = cleantext.replace(\""\", '\"')\n",
|
| 372 |
+
" # multiple spaces\n",
|
| 373 |
+
" cleantext = re.sub(CLEANMULTSPACE, ' ', cleantext)\n",
|
| 374 |
+
" # documents end with this tags, if it is present in the string, cut it off\n",
|
| 375 |
+
" idx = cleantext.find(\"<!-- NewPP limit\")\n",
|
| 376 |
+
" if idx > -1:\n",
|
| 377 |
+
" cleantext = cleantext[:idx]\n",
|
| 378 |
+
" return cleantext.strip()"
|
| 379 |
+
]
|
| 380 |
+
},
|
| 381 |
+
{
|
| 382 |
+
"cell_type": "code",
|
| 383 |
+
"execution_count": null,
|
| 384 |
+
"id": "66ca19ac",
|
| 385 |
+
"metadata": {},
|
| 386 |
+
"outputs": [],
|
| 387 |
+
"source": [
|
| 388 |
+
"import json\n",
|
| 389 |
+
"\n",
|
| 390 |
+
"# file count\n",
|
| 391 |
+
"i = 0\n",
|
| 392 |
+
"data = []\n",
|
| 393 |
+
"\n",
|
| 394 |
+
"# iterate over all json files\n",
|
| 395 |
+
"for path in paths:\n",
|
| 396 |
+
" print(path)\n",
|
| 397 |
+
" # read file and store as list (this requires much memory, as the files are huge)\n",
|
| 398 |
+
" with open(path, 'r') as json_file:\n",
|
| 399 |
+
" json_list = list(json_file)\n",
|
| 400 |
+
" \n",
|
| 401 |
+
" # process every context, question, answer pair\n",
|
| 402 |
+
" for json_str in json_list:\n",
|
| 403 |
+
" result = json.loads(json_str)\n",
|
| 404 |
+
"\n",
|
| 405 |
+
" # append a question mark - SQuAD questions end with a qm too\n",
|
| 406 |
+
" question = result['question_text'] + \"?\"\n",
|
| 407 |
+
" \n",
|
| 408 |
+
" # some question do not contain an answer - we do not need them\n",
|
| 409 |
+
" if(len(result['annotations'][0]['short_answers'])==0):\n",
|
| 410 |
+
" continue\n",
|
| 411 |
+
"\n",
|
| 412 |
+
" # get true start/end byte\n",
|
| 413 |
+
" true_start = result['annotations'][0]['short_answers'][0]['start_byte']\n",
|
| 414 |
+
" true_end = result['annotations'][0]['short_answers'][0]['end_byte']\n",
|
| 415 |
+
"\n",
|
| 416 |
+
" # convert to bytes\n",
|
| 417 |
+
" byte_encoding = bytes(result['document_html'], encoding='utf-8')\n",
|
| 418 |
+
" \n",
|
| 419 |
+
" # the document is the whole wikipedia article, we randomly choose an appropriate part (containing the\n",
|
| 420 |
+
" # answer): we have 512 tokens as the input for the model - 4000 bytes lead to a good length\n",
|
| 421 |
+
" max_back = 3500 if true_start >= 3500 else true_start\n",
|
| 422 |
+
" first = random.randint(int(true_start)-max_back, int(true_start))\n",
|
| 423 |
+
" end = first + 3500 + true_end - true_start\n",
|
| 424 |
+
" \n",
|
| 425 |
+
" # get chosen context\n",
|
| 426 |
+
" cleanbytes = byte_encoding[first:end]\n",
|
| 427 |
+
" # decode back to text - if our end byte is the middle of a word, we ignore it and cut it off\n",
|
| 428 |
+
" cleantext = bytes.decode(cleanbytes, errors='ignore')\n",
|
| 429 |
+
" # clean html tags\n",
|
| 430 |
+
" cleantext = cleanhtml(cleantext)\n",
|
| 431 |
+
"\n",
|
| 432 |
+
" # find the true answer\n",
|
| 433 |
+
" answer_start = cleanbytes.find(byte_encoding[true_start:true_end])\n",
|
| 434 |
+
" true_answer = bytes.decode(cleanbytes[answer_start:answer_start+(true_end-true_start)])\n",
|
| 435 |
+
" \n",
|
| 436 |
+
" # clean html tags\n",
|
| 437 |
+
" true_answer = cleanhtml(true_answer)\n",
|
| 438 |
+
" \n",
|
| 439 |
+
" start_ind = cleantext.find(true_answer)\n",
|
| 440 |
+
" \n",
|
| 441 |
+
" # If cleaning the string makes the answer not findable skip it\n",
|
| 442 |
+
" # this hardly ever happens, except if there is an emense amount of web artifacts\n",
|
| 443 |
+
" if start_ind == -1:\n",
|
| 444 |
+
" continue\n",
|
| 445 |
+
" \n",
|
| 446 |
+
" data.append([cleantext, question, true_answer, str(start_ind)])\n",
|
| 447 |
+
"\n",
|
| 448 |
+
" if len(data) == 1000:\n",
|
| 449 |
+
" with open(f\"data/natural_questions_train/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 450 |
+
" f.write(\"\\n\".join([\"\\t\".join(t) for t in data]))\n",
|
| 451 |
+
" i += 1\n",
|
| 452 |
+
" data = []\n",
|
| 453 |
+
"with open(f\"data/natural_questions_train/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 454 |
+
" f.write(\"\\n\".join([\"\\t\".join(t) for t in data]))"
|
| 455 |
+
]
|
| 456 |
+
},
|
| 457 |
+
{
|
| 458 |
+
"cell_type": "markdown",
|
| 459 |
+
"id": "30f26b4e",
|
| 460 |
+
"metadata": {},
|
| 461 |
+
"source": [
|
| 462 |
+
"### Testing\n",
|
| 463 |
+
"In the following, we first check if the shape of the file is correct.\n",
|
| 464 |
+
"\n",
|
| 465 |
+
"Then we iterate over the file and check if the answers according to the file are the same as in the original file."
|
| 466 |
+
]
|
| 467 |
+
},
|
| 468 |
+
{
|
| 469 |
+
"cell_type": "code",
|
| 470 |
+
"execution_count": null,
|
| 471 |
+
"id": "490ac0db",
|
| 472 |
+
"metadata": {},
|
| 473 |
+
"outputs": [],
|
| 474 |
+
"source": [
|
| 475 |
+
"with open(\"data/natural_questions_train/text_0.txt\", 'r', encoding='utf-8') as f:\n",
|
| 476 |
+
" lines = f.read().split('\\n')\n",
|
| 477 |
+
" \n",
|
| 478 |
+
"lines = pd.DataFrame([line.split(\"\\t\") for line in lines], columns=[\"context\", \"question\", \"answer\", \"answer_start\"])"
|
| 479 |
+
]
|
| 480 |
+
},
|
| 481 |
+
{
|
| 482 |
+
"cell_type": "code",
|
| 483 |
+
"execution_count": null,
|
| 484 |
+
"id": "0d7cc3ee",
|
| 485 |
+
"metadata": {},
|
| 486 |
+
"outputs": [],
|
| 487 |
+
"source": [
|
| 488 |
+
"assert lines.shape == (1000, 4)\n",
|
| 489 |
+
"print(\"Passed\")"
|
| 490 |
+
]
|
| 491 |
+
},
|
| 492 |
+
{
|
| 493 |
+
"cell_type": "code",
|
| 494 |
+
"execution_count": null,
|
| 495 |
+
"id": "0fd8a854",
|
| 496 |
+
"metadata": {},
|
| 497 |
+
"outputs": [],
|
| 498 |
+
"source": [
|
| 499 |
+
"with open(\"data/natural_questions/v1.0/train/nq-train-00.jsonl\", 'r') as json_file:\n",
|
| 500 |
+
" json_list = list(json_file)[:500]\n",
|
| 501 |
+
"del json_file"
|
| 502 |
+
]
|
| 503 |
+
},
|
| 504 |
+
{
|
| 505 |
+
"cell_type": "code",
|
| 506 |
+
"execution_count": null,
|
| 507 |
+
"id": "170bff30",
|
| 508 |
+
"metadata": {},
|
| 509 |
+
"outputs": [],
|
| 510 |
+
"source": [
|
| 511 |
+
"lines_index = 0\n",
|
| 512 |
+
"for i in range(len(json_list)):\n",
|
| 513 |
+
" result = json.loads(json_list[i])\n",
|
| 514 |
+
" \n",
|
| 515 |
+
" if(len(result['annotations'][0]['short_answers'])==0):\n",
|
| 516 |
+
" pass\n",
|
| 517 |
+
" else: \n",
|
| 518 |
+
" # assert that the question text is the same\n",
|
| 519 |
+
" assert result['question_text'] + \"?\" == lines.loc[lines_index, 'question']\n",
|
| 520 |
+
" true_start = result['annotations'][0]['short_answers'][0]['start_byte']\n",
|
| 521 |
+
" true_end = result['annotations'][0]['short_answers'][0]['end_byte']\n",
|
| 522 |
+
" true_answer = bytes.decode(bytes(result['document_html'], encoding='utf-8')[true_start:true_end])\n",
|
| 523 |
+
" \n",
|
| 524 |
+
" processed_answer = lines.loc[lines_index, 'answer']\n",
|
| 525 |
+
" # assert that the answer is the same\n",
|
| 526 |
+
" assert cleanhtml(true_answer) == processed_answer\n",
|
| 527 |
+
" \n",
|
| 528 |
+
" start_ind = int(lines.loc[lines_index, 'answer_start'])\n",
|
| 529 |
+
" # assert that the answer (according to the index) is the same\n",
|
| 530 |
+
" assert cleanhtml(true_answer) == lines.loc[lines_index, 'context'][start_ind:start_ind+len(processed_answer)]\n",
|
| 531 |
+
" \n",
|
| 532 |
+
" lines_index += 1\n",
|
| 533 |
+
" \n",
|
| 534 |
+
" if lines_index == len(lines):\n",
|
| 535 |
+
" break\n",
|
| 536 |
+
"print(\"Passed\")"
|
| 537 |
+
]
|
| 538 |
+
},
|
| 539 |
+
{
|
| 540 |
+
"cell_type": "markdown",
|
| 541 |
+
"id": "78e6e737",
|
| 542 |
+
"metadata": {},
|
| 543 |
+
"source": [
|
| 544 |
+
"## Hotpot QA"
|
| 545 |
+
]
|
| 546 |
+
},
|
| 547 |
+
{
|
| 548 |
+
"cell_type": "code",
|
| 549 |
+
"execution_count": null,
|
| 550 |
+
"id": "27efcc8c",
|
| 551 |
+
"metadata": {},
|
| 552 |
+
"outputs": [],
|
| 553 |
+
"source": [
|
| 554 |
+
"ds = load_dataset(\"hotpot_qa\", 'fullwiki')"
|
| 555 |
+
]
|
| 556 |
+
},
|
| 557 |
+
{
|
| 558 |
+
"cell_type": "code",
|
| 559 |
+
"execution_count": null,
|
| 560 |
+
"id": "1493f21f",
|
| 561 |
+
"metadata": {},
|
| 562 |
+
"outputs": [],
|
| 563 |
+
"source": [
|
| 564 |
+
"ds"
|
| 565 |
+
]
|
| 566 |
+
},
|
| 567 |
+
{
|
| 568 |
+
"cell_type": "code",
|
| 569 |
+
"execution_count": null,
|
| 570 |
+
"id": "2a047946",
|
| 571 |
+
"metadata": {},
|
| 572 |
+
"outputs": [],
|
| 573 |
+
"source": [
|
| 574 |
+
"os.mkdir('data/hotpotqa_training')\n",
|
| 575 |
+
"os.mkdir('data/hotpotqa_test')"
|
| 576 |
+
]
|
| 577 |
+
},
|
| 578 |
+
{
|
| 579 |
+
"cell_type": "code",
|
| 580 |
+
"execution_count": null,
|
| 581 |
+
"id": "e65b6485",
|
| 582 |
+
"metadata": {},
|
| 583 |
+
"outputs": [
|
| 584 |
+
{
|
| 585 |
+
"ename": "",
|
| 586 |
+
"evalue": "",
|
| 587 |
+
"output_type": "error",
|
| 588 |
+
"traceback": [
|
| 589 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 590 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 591 |
+
]
|
| 592 |
+
},
|
| 593 |
+
{
|
| 594 |
+
"ename": "",
|
| 595 |
+
"evalue": "",
|
| 596 |
+
"output_type": "error",
|
| 597 |
+
"traceback": [
|
| 598 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 599 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 600 |
+
]
|
| 601 |
+
}
|
| 602 |
+
],
|
| 603 |
+
"source": [
|
| 604 |
+
"# column contains the split (either train or validation), save_dir is the directory\n",
|
| 605 |
+
"def save_samples(column, save_dir):\n",
|
| 606 |
+
" text = []\n",
|
| 607 |
+
" i = 0\n",
|
| 608 |
+
"\n",
|
| 609 |
+
" for sample in tqdm(ds[column]):\n",
|
| 610 |
+
" \n",
|
| 611 |
+
" # preprocess the context and question by removing the newlines\n",
|
| 612 |
+
" context = sample['context']['sentences']\n",
|
| 613 |
+
" context = \" \".join([\"\".join(sentence) for sentence in context])\n",
|
| 614 |
+
" question = sample['question'].replace('\\n','')\n",
|
| 615 |
+
" \n",
|
| 616 |
+
" # get the answer as text and start character index\n",
|
| 617 |
+
" answer_text = sample['answer']\n",
|
| 618 |
+
" answer_start = context.find(answer_text)\n",
|
| 619 |
+
" if answer_start == -1:\n",
|
| 620 |
+
" continue\n",
|
| 621 |
+
" \n",
|
| 622 |
+
" \n",
|
| 623 |
+
" \n",
|
| 624 |
+
" if answer_start > 1500:\n",
|
| 625 |
+
" first = random.randint(answer_start-1500, answer_start)\n",
|
| 626 |
+
" end = first + 1500 + len(answer_text)\n",
|
| 627 |
+
" \n",
|
| 628 |
+
" context = context[first:end+1]\n",
|
| 629 |
+
" answer_start = context.find(answer_text)\n",
|
| 630 |
+
" \n",
|
| 631 |
+
" if answer_start == -1:continue\n",
|
| 632 |
+
" \n",
|
| 633 |
+
" text.append([context, question, answer_text, str(answer_start)])\n",
|
| 634 |
+
"\n",
|
| 635 |
+
" # we choose chunks of 1000\n",
|
| 636 |
+
" if len(text) == 1000:\n",
|
| 637 |
+
" with open(f\"data/{save_dir}/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 638 |
+
" f.write(\"\\n\".join([\"\\t\".join(t) for t in text]))\n",
|
| 639 |
+
" text = []\n",
|
| 640 |
+
" i += 1\n",
|
| 641 |
+
"\n",
|
| 642 |
+
" # save remaining\n",
|
| 643 |
+
" with open(f\"data/{save_dir}/text_{i}.txt\", 'w', encoding='utf-8') as f:\n",
|
| 644 |
+
" f.write(\"\\n\".join([\"\\t\".join(t) for t in text]))\n",
|
| 645 |
+
"\n",
|
| 646 |
+
"save_samples(\"train\", \"hotpotqa_training\")\n",
|
| 647 |
+
"save_samples(\"validation\", \"hotpotqa_test\")"
|
| 648 |
+
]
|
| 649 |
+
},
|
| 650 |
+
{
|
| 651 |
+
"cell_type": "markdown",
|
| 652 |
+
"id": "97cc358f",
|
| 653 |
+
"metadata": {},
|
| 654 |
+
"source": [
|
| 655 |
+
"## Testing"
|
| 656 |
+
]
|
| 657 |
+
},
|
| 658 |
+
{
|
| 659 |
+
"cell_type": "code",
|
| 660 |
+
"execution_count": null,
|
| 661 |
+
"id": "f321483c",
|
| 662 |
+
"metadata": {},
|
| 663 |
+
"outputs": [
|
| 664 |
+
{
|
| 665 |
+
"ename": "",
|
| 666 |
+
"evalue": "",
|
| 667 |
+
"output_type": "error",
|
| 668 |
+
"traceback": [
|
| 669 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 670 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 671 |
+
]
|
| 672 |
+
},
|
| 673 |
+
{
|
| 674 |
+
"ename": "",
|
| 675 |
+
"evalue": "",
|
| 676 |
+
"output_type": "error",
|
| 677 |
+
"traceback": [
|
| 678 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 679 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 680 |
+
]
|
| 681 |
+
}
|
| 682 |
+
],
|
| 683 |
+
"source": [
|
| 684 |
+
"with open(\"data/hotpotqa_training/text_0.txt\", 'r', encoding='utf-8') as f:\n",
|
| 685 |
+
" lines = f.read().split('\\n')\n",
|
| 686 |
+
" \n",
|
| 687 |
+
"lines = pd.DataFrame([line.split(\"\\t\") for line in lines], columns=[\"context\", \"question\", \"answer\", \"answer_start\"])"
|
| 688 |
+
]
|
| 689 |
+
},
|
| 690 |
+
{
|
| 691 |
+
"cell_type": "code",
|
| 692 |
+
"execution_count": null,
|
| 693 |
+
"id": "72a96e78",
|
| 694 |
+
"metadata": {},
|
| 695 |
+
"outputs": [
|
| 696 |
+
{
|
| 697 |
+
"ename": "",
|
| 698 |
+
"evalue": "",
|
| 699 |
+
"output_type": "error",
|
| 700 |
+
"traceback": [
|
| 701 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 702 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 703 |
+
]
|
| 704 |
+
},
|
| 705 |
+
{
|
| 706 |
+
"ename": "",
|
| 707 |
+
"evalue": "",
|
| 708 |
+
"output_type": "error",
|
| 709 |
+
"traceback": [
|
| 710 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 711 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 712 |
+
]
|
| 713 |
+
}
|
| 714 |
+
],
|
| 715 |
+
"source": [
|
| 716 |
+
"assert lines.shape == (1000, 4)\n",
|
| 717 |
+
"print(\"Passed\")"
|
| 718 |
+
]
|
| 719 |
+
},
|
| 720 |
+
{
|
| 721 |
+
"cell_type": "code",
|
| 722 |
+
"execution_count": null,
|
| 723 |
+
"id": "c32c2f16",
|
| 724 |
+
"metadata": {},
|
| 725 |
+
"outputs": [
|
| 726 |
+
{
|
| 727 |
+
"ename": "",
|
| 728 |
+
"evalue": "",
|
| 729 |
+
"output_type": "error",
|
| 730 |
+
"traceback": [
|
| 731 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 732 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 733 |
+
]
|
| 734 |
+
},
|
| 735 |
+
{
|
| 736 |
+
"ename": "",
|
| 737 |
+
"evalue": "",
|
| 738 |
+
"output_type": "error",
|
| 739 |
+
"traceback": [
|
| 740 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 741 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 742 |
+
]
|
| 743 |
+
}
|
| 744 |
+
],
|
| 745 |
+
"source": [
|
| 746 |
+
"# we assert that we have the right interval\n",
|
| 747 |
+
"for ind, line in lines.iterrows():\n",
|
| 748 |
+
" sample = line\n",
|
| 749 |
+
" answer_start = int(sample['answer_start'])\n",
|
| 750 |
+
" assert sample['context'][answer_start:answer_start+len(sample['answer'])] == sample['answer']\n",
|
| 751 |
+
"print(\"Passed\")"
|
| 752 |
+
]
|
| 753 |
+
},
|
| 754 |
+
{
|
| 755 |
+
"cell_type": "code",
|
| 756 |
+
"execution_count": null,
|
| 757 |
+
"id": "bc36fe7d",
|
| 758 |
+
"metadata": {},
|
| 759 |
+
"outputs": [
|
| 760 |
+
{
|
| 761 |
+
"ename": "",
|
| 762 |
+
"evalue": "",
|
| 763 |
+
"output_type": "error",
|
| 764 |
+
"traceback": [
|
| 765 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 766 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 767 |
+
]
|
| 768 |
+
},
|
| 769 |
+
{
|
| 770 |
+
"ename": "",
|
| 771 |
+
"evalue": "",
|
| 772 |
+
"output_type": "error",
|
| 773 |
+
"traceback": [
|
| 774 |
+
"\u001b[1;31mnotebook controller is DISPOSED. \n",
|
| 775 |
+
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 776 |
+
]
|
| 777 |
+
}
|
| 778 |
+
],
|
| 779 |
+
"source": []
|
| 780 |
+
}
|
| 781 |
+
],
|
| 782 |
+
"metadata": {
|
| 783 |
+
"kernelspec": {
|
| 784 |
+
"display_name": "Python 3 (ipykernel)",
|
| 785 |
+
"language": "python",
|
| 786 |
+
"name": "python3"
|
| 787 |
+
},
|
| 788 |
+
"language_info": {
|
| 789 |
+
"codemirror_mode": {
|
| 790 |
+
"name": "ipython",
|
| 791 |
+
"version": 3
|
| 792 |
+
},
|
| 793 |
+
"file_extension": ".py",
|
| 794 |
+
"mimetype": "text/x-python",
|
| 795 |
+
"name": "python",
|
| 796 |
+
"nbconvert_exporter": "python",
|
| 797 |
+
"pygments_lexer": "ipython3",
|
| 798 |
+
"version": "3.10.16"
|
| 799 |
+
},
|
| 800 |
+
"toc": {
|
| 801 |
+
"base_numbering": 1,
|
| 802 |
+
"nav_menu": {},
|
| 803 |
+
"number_sections": true,
|
| 804 |
+
"sideBar": true,
|
| 805 |
+
"skip_h1_title": false,
|
| 806 |
+
"title_cell": "Table of Contents",
|
| 807 |
+
"title_sidebar": "Contents",
|
| 808 |
+
"toc_cell": false,
|
| 809 |
+
"toc_position": {},
|
| 810 |
+
"toc_section_display": true,
|
| 811 |
+
"toc_window_display": false
|
| 812 |
+
},
|
| 813 |
+
"varInspector": {
|
| 814 |
+
"cols": {
|
| 815 |
+
"lenName": 16,
|
| 816 |
+
"lenType": 16,
|
| 817 |
+
"lenVar": 40
|
| 818 |
+
},
|
| 819 |
+
"kernels_config": {
|
| 820 |
+
"python": {
|
| 821 |
+
"delete_cmd_postfix": "",
|
| 822 |
+
"delete_cmd_prefix": "del ",
|
| 823 |
+
"library": "var_list.py",
|
| 824 |
+
"varRefreshCmd": "print(var_dic_list())"
|
| 825 |
+
},
|
| 826 |
+
"r": {
|
| 827 |
+
"delete_cmd_postfix": ") ",
|
| 828 |
+
"delete_cmd_prefix": "rm(",
|
| 829 |
+
"library": "var_list.r",
|
| 830 |
+
"varRefreshCmd": "cat(var_dic_list()) "
|
| 831 |
+
}
|
| 832 |
+
},
|
| 833 |
+
"types_to_exclude": [
|
| 834 |
+
"module",
|
| 835 |
+
"function",
|
| 836 |
+
"builtin_function_or_method",
|
| 837 |
+
"instance",
|
| 838 |
+
"_Feature"
|
| 839 |
+
],
|
| 840 |
+
"window_display": false
|
| 841 |
+
},
|
| 842 |
+
"vscode": {
|
| 843 |
+
"interpreter": {
|
| 844 |
+
"hash": "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ata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Question Answering\n",
|
| 9 |
+
"The following notebook contains different question answering models. We will start by introducing a representation for the dataset and corresponding DataLoader and then evaluate different models."
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": 50,
|
| 15 |
+
"id": "49bf46c6",
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [],
|
| 18 |
+
"source": [
|
| 19 |
+
"from transformers import DistilBertModel, DistilBertForMaskedLM, DistilBertConfig, \\\n",
|
| 20 |
+
" DistilBertTokenizerFast, AutoTokenizer, BertModel, BertForMaskedLM, BertTokenizerFast, BertConfig\n",
|
| 21 |
+
"from torch import nn\n",
|
| 22 |
+
"from pathlib import Path\n",
|
| 23 |
+
"import torch\n",
|
| 24 |
+
"import pandas as pd\n",
|
| 25 |
+
"from typing import Optional \n",
|
| 26 |
+
"from tqdm.auto import tqdm\n",
|
| 27 |
+
"from util import eval_test_set, count_parameters\n",
|
| 28 |
+
"from torch.optim import AdamW, RMSprop\n",
|
| 29 |
+
"\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"from qa_model import QuestionDistilBERT, SimpleQuestionDistilBERT, ReuseQuestionDistilBERT, Dataset, test_model"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "markdown",
|
| 36 |
+
"id": "3ea47820",
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"source": [
|
| 39 |
+
"## Data\n",
|
| 40 |
+
"Processing the data correctly is partly based on the Huggingface Tutorial (https://huggingface.co/course/chapter7/7?fw=pt)"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "code",
|
| 45 |
+
"execution_count": 51,
|
| 46 |
+
"id": "7b1b2b3e",
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"outputs": [],
|
| 49 |
+
"source": [
|
| 50 |
+
"tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"execution_count": 52,
|
| 56 |
+
"id": "f276eba7",
|
| 57 |
+
"metadata": {
|
| 58 |
+
"scrolled": false
|
| 59 |
+
},
|
| 60 |
+
"outputs": [],
|
| 61 |
+
"source": [
|
| 62 |
+
" \n",
|
| 63 |
+
"# create datasets and loaders for training and test set\n",
|
| 64 |
+
"squad_paths = [str(x) for x in Path('data/training_squad/').glob('**/*.txt')]\n",
|
| 65 |
+
"nat_paths = [str(x) for x in Path('data/natural_questions_train/').glob('**/*.txt')]\n",
|
| 66 |
+
"hotpotqa_paths = [str(x) for x in Path('data/hotpotqa_training/').glob('**/*.txt')]"
|
| 67 |
+
]
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"cell_type": "markdown",
|
| 71 |
+
"id": "ad8d532a",
|
| 72 |
+
"metadata": {},
|
| 73 |
+
"source": [
|
| 74 |
+
"## POC Model\n",
|
| 75 |
+
"* Works very well:\n",
|
| 76 |
+
" * Dropout 0.1 is too small (overfitting after first epoch) - changed to 0.15\n",
|
| 77 |
+
" * Difference between AdamW and RMSprop minimal\n",
|
| 78 |
+
" \n",
|
| 79 |
+
"### Results:\n",
|
| 80 |
+
"Dropout = 0.15\n",
|
| 81 |
+
"* Mean EM: 0.5374\n",
|
| 82 |
+
"* Mean F-1: 0.6826317532406944\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"Dropout = 0.2 (overfitting realtively similar to first, but seems to be too high)\n",
|
| 85 |
+
"* Mean EM: 0.5044\n",
|
| 86 |
+
"* Mean F-1: 0.6437359169276439"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": 54,
|
| 92 |
+
"id": "703e7f38",
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"dataset = Dataset(squad_paths = squad_paths, natural_question_paths=None, hotpotqa_paths=hotpotqa_paths, tokenizer=tokenizer)\n",
|
| 97 |
+
"loader = torch.utils.data.DataLoader(dataset, batch_size=8)\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"test_dataset = Dataset(squad_paths = [str(x) for x in Path('data/test_squad/').glob('**/*.txt')], \n",
|
| 100 |
+
" natural_question_paths=None, \n",
|
| 101 |
+
" hotpotqa_paths = None, tokenizer=tokenizer)\n",
|
| 102 |
+
"test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4)"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 55,
|
| 108 |
+
"id": "6672f614",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"model = DistilBertForMaskedLM.from_pretrained(\"distilbert-base-uncased\")\n",
|
| 113 |
+
"config = DistilBertConfig.from_pretrained(\"distilbert-base-uncased\")\n",
|
| 114 |
+
"mod = model.distilbert"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": 56,
|
| 120 |
+
"id": "dec15198",
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"outputs": [
|
| 123 |
+
{
|
| 124 |
+
"data": {
|
| 125 |
+
"text/plain": [
|
| 126 |
+
"SimpleQuestionDistilBERT(\n",
|
| 127 |
+
" (distilbert): DistilBertModel(\n",
|
| 128 |
+
" (embeddings): Embeddings(\n",
|
| 129 |
+
" (word_embeddings): Embedding(30522, 768, padding_idx=0)\n",
|
| 130 |
+
" (position_embeddings): Embedding(512, 768)\n",
|
| 131 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 132 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 133 |
+
" )\n",
|
| 134 |
+
" (transformer): Transformer(\n",
|
| 135 |
+
" (layer): ModuleList(\n",
|
| 136 |
+
" (0): TransformerBlock(\n",
|
| 137 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 138 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 139 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 140 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 141 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 142 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 143 |
+
" )\n",
|
| 144 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 145 |
+
" (ffn): FFN(\n",
|
| 146 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 147 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 148 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 149 |
+
" (activation): GELUActivation()\n",
|
| 150 |
+
" )\n",
|
| 151 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 152 |
+
" )\n",
|
| 153 |
+
" (1): TransformerBlock(\n",
|
| 154 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 155 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 156 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 157 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 158 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 159 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 160 |
+
" )\n",
|
| 161 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 162 |
+
" (ffn): FFN(\n",
|
| 163 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 164 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 165 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 166 |
+
" (activation): GELUActivation()\n",
|
| 167 |
+
" )\n",
|
| 168 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 169 |
+
" )\n",
|
| 170 |
+
" (2): TransformerBlock(\n",
|
| 171 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 172 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 173 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 174 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 175 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 176 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 177 |
+
" )\n",
|
| 178 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 179 |
+
" (ffn): FFN(\n",
|
| 180 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 181 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 182 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 183 |
+
" (activation): GELUActivation()\n",
|
| 184 |
+
" )\n",
|
| 185 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 186 |
+
" )\n",
|
| 187 |
+
" (3): TransformerBlock(\n",
|
| 188 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 189 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 190 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 191 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 192 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 193 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 194 |
+
" )\n",
|
| 195 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 196 |
+
" (ffn): FFN(\n",
|
| 197 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 198 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 199 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 200 |
+
" (activation): GELUActivation()\n",
|
| 201 |
+
" )\n",
|
| 202 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 203 |
+
" )\n",
|
| 204 |
+
" (4): TransformerBlock(\n",
|
| 205 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 206 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 207 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 208 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 209 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 210 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 211 |
+
" )\n",
|
| 212 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 213 |
+
" (ffn): FFN(\n",
|
| 214 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 215 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 216 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 217 |
+
" (activation): GELUActivation()\n",
|
| 218 |
+
" )\n",
|
| 219 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 220 |
+
" )\n",
|
| 221 |
+
" (5): TransformerBlock(\n",
|
| 222 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 223 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 224 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 225 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 226 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 227 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 228 |
+
" )\n",
|
| 229 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 230 |
+
" (ffn): FFN(\n",
|
| 231 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 232 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 233 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 234 |
+
" (activation): GELUActivation()\n",
|
| 235 |
+
" )\n",
|
| 236 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 237 |
+
" )\n",
|
| 238 |
+
" )\n",
|
| 239 |
+
" )\n",
|
| 240 |
+
" )\n",
|
| 241 |
+
" (dropout): Dropout(p=0.5, inplace=False)\n",
|
| 242 |
+
" (classifier): Linear(in_features=768, out_features=2, bias=True)\n",
|
| 243 |
+
")"
|
| 244 |
+
]
|
| 245 |
+
},
|
| 246 |
+
"execution_count": 56,
|
| 247 |
+
"metadata": {},
|
| 248 |
+
"output_type": "execute_result"
|
| 249 |
+
}
|
| 250 |
+
],
|
| 251 |
+
"source": [
|
| 252 |
+
"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
|
| 253 |
+
"model = SimpleQuestionDistilBERT(mod)\n",
|
| 254 |
+
"model.to(device)"
|
| 255 |
+
]
|
| 256 |
+
},
|
| 257 |
+
{
|
| 258 |
+
"cell_type": "code",
|
| 259 |
+
"execution_count": 57,
|
| 260 |
+
"id": "9def3c83",
|
| 261 |
+
"metadata": {},
|
| 262 |
+
"outputs": [
|
| 263 |
+
{
|
| 264 |
+
"name": "stdout",
|
| 265 |
+
"output_type": "stream",
|
| 266 |
+
"text": [
|
| 267 |
+
"+---------------------------------------------------------+------------+\n",
|
| 268 |
+
"| Modules | Parameters |\n",
|
| 269 |
+
"+---------------------------------------------------------+------------+\n",
|
| 270 |
+
"| distilbert.embeddings.word_embeddings.weight | 23440896 |\n",
|
| 271 |
+
"| distilbert.embeddings.position_embeddings.weight | 393216 |\n",
|
| 272 |
+
"| distilbert.embeddings.LayerNorm.weight | 768 |\n",
|
| 273 |
+
"| distilbert.embeddings.LayerNorm.bias | 768 |\n",
|
| 274 |
+
"| distilbert.transformer.layer.0.attention.q_lin.weight | 589824 |\n",
|
| 275 |
+
"| distilbert.transformer.layer.0.attention.q_lin.bias | 768 |\n",
|
| 276 |
+
"| distilbert.transformer.layer.0.attention.k_lin.weight | 589824 |\n",
|
| 277 |
+
"| distilbert.transformer.layer.0.attention.k_lin.bias | 768 |\n",
|
| 278 |
+
"| distilbert.transformer.layer.0.attention.v_lin.weight | 589824 |\n",
|
| 279 |
+
"| distilbert.transformer.layer.0.attention.v_lin.bias | 768 |\n",
|
| 280 |
+
"| distilbert.transformer.layer.0.attention.out_lin.weight | 589824 |\n",
|
| 281 |
+
"| distilbert.transformer.layer.0.attention.out_lin.bias | 768 |\n",
|
| 282 |
+
"| distilbert.transformer.layer.0.sa_layer_norm.weight | 768 |\n",
|
| 283 |
+
"| distilbert.transformer.layer.0.sa_layer_norm.bias | 768 |\n",
|
| 284 |
+
"| distilbert.transformer.layer.0.ffn.lin1.weight | 2359296 |\n",
|
| 285 |
+
"| distilbert.transformer.layer.0.ffn.lin1.bias | 3072 |\n",
|
| 286 |
+
"| distilbert.transformer.layer.0.ffn.lin2.weight | 2359296 |\n",
|
| 287 |
+
"| distilbert.transformer.layer.0.ffn.lin2.bias | 768 |\n",
|
| 288 |
+
"| distilbert.transformer.layer.0.output_layer_norm.weight | 768 |\n",
|
| 289 |
+
"| distilbert.transformer.layer.0.output_layer_norm.bias | 768 |\n",
|
| 290 |
+
"| distilbert.transformer.layer.1.attention.q_lin.weight | 589824 |\n",
|
| 291 |
+
"| distilbert.transformer.layer.1.attention.q_lin.bias | 768 |\n",
|
| 292 |
+
"| distilbert.transformer.layer.1.attention.k_lin.weight | 589824 |\n",
|
| 293 |
+
"| distilbert.transformer.layer.1.attention.k_lin.bias | 768 |\n",
|
| 294 |
+
"| distilbert.transformer.layer.1.attention.v_lin.weight | 589824 |\n",
|
| 295 |
+
"| distilbert.transformer.layer.1.attention.v_lin.bias | 768 |\n",
|
| 296 |
+
"| distilbert.transformer.layer.1.attention.out_lin.weight | 589824 |\n",
|
| 297 |
+
"| distilbert.transformer.layer.1.attention.out_lin.bias | 768 |\n",
|
| 298 |
+
"| distilbert.transformer.layer.1.sa_layer_norm.weight | 768 |\n",
|
| 299 |
+
"| distilbert.transformer.layer.1.sa_layer_norm.bias | 768 |\n",
|
| 300 |
+
"| distilbert.transformer.layer.1.ffn.lin1.weight | 2359296 |\n",
|
| 301 |
+
"| distilbert.transformer.layer.1.ffn.lin1.bias | 3072 |\n",
|
| 302 |
+
"| distilbert.transformer.layer.1.ffn.lin2.weight | 2359296 |\n",
|
| 303 |
+
"| distilbert.transformer.layer.1.ffn.lin2.bias | 768 |\n",
|
| 304 |
+
"| distilbert.transformer.layer.1.output_layer_norm.weight | 768 |\n",
|
| 305 |
+
"| distilbert.transformer.layer.1.output_layer_norm.bias | 768 |\n",
|
| 306 |
+
"| distilbert.transformer.layer.2.attention.q_lin.weight | 589824 |\n",
|
| 307 |
+
"| distilbert.transformer.layer.2.attention.q_lin.bias | 768 |\n",
|
| 308 |
+
"| distilbert.transformer.layer.2.attention.k_lin.weight | 589824 |\n",
|
| 309 |
+
"| distilbert.transformer.layer.2.attention.k_lin.bias | 768 |\n",
|
| 310 |
+
"| distilbert.transformer.layer.2.attention.v_lin.weight | 589824 |\n",
|
| 311 |
+
"| distilbert.transformer.layer.2.attention.v_lin.bias | 768 |\n",
|
| 312 |
+
"| distilbert.transformer.layer.2.attention.out_lin.weight | 589824 |\n",
|
| 313 |
+
"| distilbert.transformer.layer.2.attention.out_lin.bias | 768 |\n",
|
| 314 |
+
"| distilbert.transformer.layer.2.sa_layer_norm.weight | 768 |\n",
|
| 315 |
+
"| distilbert.transformer.layer.2.sa_layer_norm.bias | 768 |\n",
|
| 316 |
+
"| distilbert.transformer.layer.2.ffn.lin1.weight | 2359296 |\n",
|
| 317 |
+
"| distilbert.transformer.layer.2.ffn.lin1.bias | 3072 |\n",
|
| 318 |
+
"| distilbert.transformer.layer.2.ffn.lin2.weight | 2359296 |\n",
|
| 319 |
+
"| distilbert.transformer.layer.2.ffn.lin2.bias | 768 |\n",
|
| 320 |
+
"| distilbert.transformer.layer.2.output_layer_norm.weight | 768 |\n",
|
| 321 |
+
"| distilbert.transformer.layer.2.output_layer_norm.bias | 768 |\n",
|
| 322 |
+
"| distilbert.transformer.layer.3.attention.q_lin.weight | 589824 |\n",
|
| 323 |
+
"| distilbert.transformer.layer.3.attention.q_lin.bias | 768 |\n",
|
| 324 |
+
"| distilbert.transformer.layer.3.attention.k_lin.weight | 589824 |\n",
|
| 325 |
+
"| distilbert.transformer.layer.3.attention.k_lin.bias | 768 |\n",
|
| 326 |
+
"| distilbert.transformer.layer.3.attention.v_lin.weight | 589824 |\n",
|
| 327 |
+
"| distilbert.transformer.layer.3.attention.v_lin.bias | 768 |\n",
|
| 328 |
+
"| distilbert.transformer.layer.3.attention.out_lin.weight | 589824 |\n",
|
| 329 |
+
"| distilbert.transformer.layer.3.attention.out_lin.bias | 768 |\n",
|
| 330 |
+
"| distilbert.transformer.layer.3.sa_layer_norm.weight | 768 |\n",
|
| 331 |
+
"| distilbert.transformer.layer.3.sa_layer_norm.bias | 768 |\n",
|
| 332 |
+
"| distilbert.transformer.layer.3.ffn.lin1.weight | 2359296 |\n",
|
| 333 |
+
"| distilbert.transformer.layer.3.ffn.lin1.bias | 3072 |\n",
|
| 334 |
+
"| distilbert.transformer.layer.3.ffn.lin2.weight | 2359296 |\n",
|
| 335 |
+
"| distilbert.transformer.layer.3.ffn.lin2.bias | 768 |\n",
|
| 336 |
+
"| distilbert.transformer.layer.3.output_layer_norm.weight | 768 |\n",
|
| 337 |
+
"| distilbert.transformer.layer.3.output_layer_norm.bias | 768 |\n",
|
| 338 |
+
"| distilbert.transformer.layer.4.attention.q_lin.weight | 589824 |\n",
|
| 339 |
+
"| distilbert.transformer.layer.4.attention.q_lin.bias | 768 |\n",
|
| 340 |
+
"| distilbert.transformer.layer.4.attention.k_lin.weight | 589824 |\n",
|
| 341 |
+
"| distilbert.transformer.layer.4.attention.k_lin.bias | 768 |\n",
|
| 342 |
+
"| distilbert.transformer.layer.4.attention.v_lin.weight | 589824 |\n",
|
| 343 |
+
"| distilbert.transformer.layer.4.attention.v_lin.bias | 768 |\n",
|
| 344 |
+
"| distilbert.transformer.layer.4.attention.out_lin.weight | 589824 |\n",
|
| 345 |
+
"| distilbert.transformer.layer.4.attention.out_lin.bias | 768 |\n",
|
| 346 |
+
"| distilbert.transformer.layer.4.sa_layer_norm.weight | 768 |\n",
|
| 347 |
+
"| distilbert.transformer.layer.4.sa_layer_norm.bias | 768 |\n",
|
| 348 |
+
"| distilbert.transformer.layer.4.ffn.lin1.weight | 2359296 |\n",
|
| 349 |
+
"| distilbert.transformer.layer.4.ffn.lin1.bias | 3072 |\n",
|
| 350 |
+
"| distilbert.transformer.layer.4.ffn.lin2.weight | 2359296 |\n",
|
| 351 |
+
"| distilbert.transformer.layer.4.ffn.lin2.bias | 768 |\n",
|
| 352 |
+
"| distilbert.transformer.layer.4.output_layer_norm.weight | 768 |\n",
|
| 353 |
+
"| distilbert.transformer.layer.4.output_layer_norm.bias | 768 |\n",
|
| 354 |
+
"| distilbert.transformer.layer.5.attention.q_lin.weight | 589824 |\n",
|
| 355 |
+
"| distilbert.transformer.layer.5.attention.q_lin.bias | 768 |\n",
|
| 356 |
+
"| distilbert.transformer.layer.5.attention.k_lin.weight | 589824 |\n",
|
| 357 |
+
"| distilbert.transformer.layer.5.attention.k_lin.bias | 768 |\n",
|
| 358 |
+
"| distilbert.transformer.layer.5.attention.v_lin.weight | 589824 |\n",
|
| 359 |
+
"| distilbert.transformer.layer.5.attention.v_lin.bias | 768 |\n",
|
| 360 |
+
"| distilbert.transformer.layer.5.attention.out_lin.weight | 589824 |\n",
|
| 361 |
+
"| distilbert.transformer.layer.5.attention.out_lin.bias | 768 |\n",
|
| 362 |
+
"| distilbert.transformer.layer.5.sa_layer_norm.weight | 768 |\n",
|
| 363 |
+
"| distilbert.transformer.layer.5.sa_layer_norm.bias | 768 |\n",
|
| 364 |
+
"| distilbert.transformer.layer.5.ffn.lin1.weight | 2359296 |\n",
|
| 365 |
+
"| distilbert.transformer.layer.5.ffn.lin1.bias | 3072 |\n",
|
| 366 |
+
"| distilbert.transformer.layer.5.ffn.lin2.weight | 2359296 |\n",
|
| 367 |
+
"| distilbert.transformer.layer.5.ffn.lin2.bias | 768 |\n",
|
| 368 |
+
"| distilbert.transformer.layer.5.output_layer_norm.weight | 768 |\n",
|
| 369 |
+
"| distilbert.transformer.layer.5.output_layer_norm.bias | 768 |\n",
|
| 370 |
+
"| classifier.weight | 1536 |\n",
|
| 371 |
+
"| classifier.bias | 2 |\n",
|
| 372 |
+
"+---------------------------------------------------------+------------+\n",
|
| 373 |
+
"Total Trainable Params: 66364418\n"
|
| 374 |
+
]
|
| 375 |
+
},
|
| 376 |
+
{
|
| 377 |
+
"data": {
|
| 378 |
+
"text/plain": [
|
| 379 |
+
"66364418"
|
| 380 |
+
]
|
| 381 |
+
},
|
| 382 |
+
"execution_count": 57,
|
| 383 |
+
"metadata": {},
|
| 384 |
+
"output_type": "execute_result"
|
| 385 |
+
}
|
| 386 |
+
],
|
| 387 |
+
"source": [
|
| 388 |
+
"count_parameters(model)"
|
| 389 |
+
]
|
| 390 |
+
},
|
| 391 |
+
{
|
| 392 |
+
"cell_type": "markdown",
|
| 393 |
+
"id": "426a6311",
|
| 394 |
+
"metadata": {},
|
| 395 |
+
"source": [
|
| 396 |
+
"### Testing the model"
|
| 397 |
+
]
|
| 398 |
+
},
|
| 399 |
+
{
|
| 400 |
+
"cell_type": "code",
|
| 401 |
+
"execution_count": 58,
|
| 402 |
+
"id": "6151c201",
|
| 403 |
+
"metadata": {},
|
| 404 |
+
"outputs": [],
|
| 405 |
+
"source": [
|
| 406 |
+
"# get smaller dataset\n",
|
| 407 |
+
"batch_size = 8\n",
|
| 408 |
+
"test_ds = Dataset(squad_paths = squad_paths[:2], natural_question_paths=None, hotpotqa_paths=None, tokenizer=tokenizer)\n",
|
| 409 |
+
"test_ds_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size)\n",
|
| 410 |
+
"optim = RMSprop(model.parameters(), lr=1e-4)"
|
| 411 |
+
]
|
| 412 |
+
},
|
| 413 |
+
{
|
| 414 |
+
"cell_type": "code",
|
| 415 |
+
"execution_count": 59,
|
| 416 |
+
"id": "aeae0c56",
|
| 417 |
+
"metadata": {},
|
| 418 |
+
"outputs": [
|
| 419 |
+
{
|
| 420 |
+
"name": "stdout",
|
| 421 |
+
"output_type": "stream",
|
| 422 |
+
"text": [
|
| 423 |
+
"Passed\n"
|
| 424 |
+
]
|
| 425 |
+
}
|
| 426 |
+
],
|
| 427 |
+
"source": [
|
| 428 |
+
"test_model(model, optim, test_ds_loader, device)"
|
| 429 |
+
]
|
| 430 |
+
},
|
| 431 |
+
{
|
| 432 |
+
"cell_type": "markdown",
|
| 433 |
+
"id": "59928d34",
|
| 434 |
+
"metadata": {},
|
| 435 |
+
"source": [
|
| 436 |
+
"### Model Training"
|
| 437 |
+
]
|
| 438 |
+
},
|
| 439 |
+
{
|
| 440 |
+
"cell_type": "code",
|
| 441 |
+
"execution_count": 60,
|
| 442 |
+
"id": "a8017b8c",
|
| 443 |
+
"metadata": {},
|
| 444 |
+
"outputs": [
|
| 445 |
+
{
|
| 446 |
+
"data": {
|
| 447 |
+
"text/plain": [
|
| 448 |
+
"SimpleQuestionDistilBERT(\n",
|
| 449 |
+
" (distilbert): DistilBertModel(\n",
|
| 450 |
+
" (embeddings): Embeddings(\n",
|
| 451 |
+
" (word_embeddings): Embedding(30522, 768, padding_idx=0)\n",
|
| 452 |
+
" (position_embeddings): Embedding(512, 768)\n",
|
| 453 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 454 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 455 |
+
" )\n",
|
| 456 |
+
" (transformer): Transformer(\n",
|
| 457 |
+
" (layer): ModuleList(\n",
|
| 458 |
+
" (0): TransformerBlock(\n",
|
| 459 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 460 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 461 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 462 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 463 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 464 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 465 |
+
" )\n",
|
| 466 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 467 |
+
" (ffn): FFN(\n",
|
| 468 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 469 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 470 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 471 |
+
" (activation): GELUActivation()\n",
|
| 472 |
+
" )\n",
|
| 473 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 474 |
+
" )\n",
|
| 475 |
+
" (1): TransformerBlock(\n",
|
| 476 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 477 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 478 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 479 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 480 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 481 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 482 |
+
" )\n",
|
| 483 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 484 |
+
" (ffn): FFN(\n",
|
| 485 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 486 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 487 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 488 |
+
" (activation): GELUActivation()\n",
|
| 489 |
+
" )\n",
|
| 490 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 491 |
+
" )\n",
|
| 492 |
+
" (2): TransformerBlock(\n",
|
| 493 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 494 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 495 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 496 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 497 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 498 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 499 |
+
" )\n",
|
| 500 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 501 |
+
" (ffn): FFN(\n",
|
| 502 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 503 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 504 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 505 |
+
" (activation): GELUActivation()\n",
|
| 506 |
+
" )\n",
|
| 507 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 508 |
+
" )\n",
|
| 509 |
+
" (3): TransformerBlock(\n",
|
| 510 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 511 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 512 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 513 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 514 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 515 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 516 |
+
" )\n",
|
| 517 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 518 |
+
" (ffn): FFN(\n",
|
| 519 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 520 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 521 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 522 |
+
" (activation): GELUActivation()\n",
|
| 523 |
+
" )\n",
|
| 524 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 525 |
+
" )\n",
|
| 526 |
+
" (4): TransformerBlock(\n",
|
| 527 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 528 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 529 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 530 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 531 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 532 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 533 |
+
" )\n",
|
| 534 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 535 |
+
" (ffn): FFN(\n",
|
| 536 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 537 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 538 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 539 |
+
" (activation): GELUActivation()\n",
|
| 540 |
+
" )\n",
|
| 541 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 542 |
+
" )\n",
|
| 543 |
+
" (5): TransformerBlock(\n",
|
| 544 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 545 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 546 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 547 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 548 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 549 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 550 |
+
" )\n",
|
| 551 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 552 |
+
" (ffn): FFN(\n",
|
| 553 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 554 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 555 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 556 |
+
" (activation): GELUActivation()\n",
|
| 557 |
+
" )\n",
|
| 558 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 559 |
+
" )\n",
|
| 560 |
+
" )\n",
|
| 561 |
+
" )\n",
|
| 562 |
+
" )\n",
|
| 563 |
+
" (dropout): Dropout(p=0.5, inplace=False)\n",
|
| 564 |
+
" (classifier): Linear(in_features=768, out_features=2, bias=True)\n",
|
| 565 |
+
")"
|
| 566 |
+
]
|
| 567 |
+
},
|
| 568 |
+
"execution_count": 60,
|
| 569 |
+
"metadata": {},
|
| 570 |
+
"output_type": "execute_result"
|
| 571 |
+
}
|
| 572 |
+
],
|
| 573 |
+
"source": [
|
| 574 |
+
"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
|
| 575 |
+
"model = SimpleQuestionDistilBERT(mod)\n",
|
| 576 |
+
"model.to(device)"
|
| 577 |
+
]
|
| 578 |
+
},
|
| 579 |
+
{
|
| 580 |
+
"cell_type": "code",
|
| 581 |
+
"execution_count": 61,
|
| 582 |
+
"id": "f13c12dc",
|
| 583 |
+
"metadata": {},
|
| 584 |
+
"outputs": [],
|
| 585 |
+
"source": [
|
| 586 |
+
"model.train()\n",
|
| 587 |
+
"optim = RMSprop(model.parameters(), lr=1e-4)"
|
| 588 |
+
]
|
| 589 |
+
},
|
| 590 |
+
{
|
| 591 |
+
"cell_type": "code",
|
| 592 |
+
"execution_count": null,
|
| 593 |
+
"id": "e4fa54d9",
|
| 594 |
+
"metadata": {},
|
| 595 |
+
"outputs": [],
|
| 596 |
+
"source": [
|
| 597 |
+
"epochs = 5\n",
|
| 598 |
+
"\n",
|
| 599 |
+
"for epoch in range(epochs):\n",
|
| 600 |
+
" loop = tqdm(loader, leave=True)\n",
|
| 601 |
+
" model.train()\n",
|
| 602 |
+
" mean_training_error = []\n",
|
| 603 |
+
" for batch in loop:\n",
|
| 604 |
+
" optim.zero_grad()\n",
|
| 605 |
+
" \n",
|
| 606 |
+
" input_ids = batch['input_ids'].to(device)\n",
|
| 607 |
+
" attention_mask = batch['attention_mask'].to(device)\n",
|
| 608 |
+
" start = batch['start_positions'].to(device)\n",
|
| 609 |
+
" end = batch['end_positions'].to(device)\n",
|
| 610 |
+
" \n",
|
| 611 |
+
" outputs = model(input_ids, attention_mask=attention_mask, start_positions=start, end_positions=end)\n",
|
| 612 |
+
" # print(torch.argmax(outputs['start_logits'],axis=1), torch.argmax(outputs['end_logits'], axis=1), start, end)\n",
|
| 613 |
+
" loss = outputs['loss']\n",
|
| 614 |
+
" loss.backward()\n",
|
| 615 |
+
" # torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1)\n",
|
| 616 |
+
" optim.step()\n",
|
| 617 |
+
" mean_training_error.append(loss.item())\n",
|
| 618 |
+
" loop.set_description(f'Epoch {epoch}')\n",
|
| 619 |
+
" loop.set_postfix(loss=loss.item())\n",
|
| 620 |
+
" print(\"Mean Training Error\", np.mean(mean_training_error))\n",
|
| 621 |
+
" \n",
|
| 622 |
+
" \n",
|
| 623 |
+
" loop = tqdm(test_loader, leave=True)\n",
|
| 624 |
+
" model.eval()\n",
|
| 625 |
+
" mean_test_error = []\n",
|
| 626 |
+
" for batch in loop:\n",
|
| 627 |
+
" \n",
|
| 628 |
+
" input_ids = batch['input_ids'].to(device)\n",
|
| 629 |
+
" attention_mask = batch['attention_mask'].to(device)\n",
|
| 630 |
+
" start = batch['start_positions'].to(device)\n",
|
| 631 |
+
" end = batch['end_positions'].to(device)\n",
|
| 632 |
+
" \n",
|
| 633 |
+
" outputs = model(input_ids, attention_mask=attention_mask, start_positions=start, end_positions=end)\n",
|
| 634 |
+
" # print(torch.argmax(outputs['start_logits'],axis=1), torch.argmax(outputs['end_logits'], axis=1), start, end)\n",
|
| 635 |
+
" loss = outputs['loss']\n",
|
| 636 |
+
" \n",
|
| 637 |
+
" mean_test_error.append(loss.item())\n",
|
| 638 |
+
" loop.set_description(f'Epoch {epoch} Testset')\n",
|
| 639 |
+
" loop.set_postfix(loss=loss.item())\n",
|
| 640 |
+
" print(\"Mean Test Error\", np.mean(mean_test_error))"
|
| 641 |
+
]
|
| 642 |
+
},
|
| 643 |
+
{
|
| 644 |
+
"cell_type": "code",
|
| 645 |
+
"execution_count": 19,
|
| 646 |
+
"id": "6ff26fb4",
|
| 647 |
+
"metadata": {},
|
| 648 |
+
"outputs": [],
|
| 649 |
+
"source": [
|
| 650 |
+
"torch.save(model.state_dict(), \"simple_distilbert_qa.model\")"
|
| 651 |
+
]
|
| 652 |
+
},
|
| 653 |
+
{
|
| 654 |
+
"cell_type": "code",
|
| 655 |
+
"execution_count": 20,
|
| 656 |
+
"id": "a5e7abeb",
|
| 657 |
+
"metadata": {},
|
| 658 |
+
"outputs": [
|
| 659 |
+
{
|
| 660 |
+
"data": {
|
| 661 |
+
"text/plain": [
|
| 662 |
+
"<All keys matched successfully>"
|
| 663 |
+
]
|
| 664 |
+
},
|
| 665 |
+
"execution_count": 20,
|
| 666 |
+
"metadata": {},
|
| 667 |
+
"output_type": "execute_result"
|
| 668 |
+
}
|
| 669 |
+
],
|
| 670 |
+
"source": [
|
| 671 |
+
"model = SimpleQuestionDistilBERT(mod)\n",
|
| 672 |
+
"model.load_state_dict(torch.load(\"simple_distilbert_qa.model\"))"
|
| 673 |
+
]
|
| 674 |
+
},
|
| 675 |
+
{
|
| 676 |
+
"cell_type": "code",
|
| 677 |
+
"execution_count": 18,
|
| 678 |
+
"id": "f5ad7bee",
|
| 679 |
+
"metadata": {},
|
| 680 |
+
"outputs": [
|
| 681 |
+
{
|
| 682 |
+
"name": "stderr",
|
| 683 |
+
"output_type": "stream",
|
| 684 |
+
"text": [
|
| 685 |
+
"100%|鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 2500/2500 [02:09<00:00, 19.37it/s]"
|
| 686 |
+
]
|
| 687 |
+
},
|
| 688 |
+
{
|
| 689 |
+
"name": "stdout",
|
| 690 |
+
"output_type": "stream",
|
| 691 |
+
"text": [
|
| 692 |
+
"Mean EM: 0.5374\n",
|
| 693 |
+
"Mean F-1: 0.6826317532406944\n"
|
| 694 |
+
]
|
| 695 |
+
},
|
| 696 |
+
{
|
| 697 |
+
"name": "stderr",
|
| 698 |
+
"output_type": "stream",
|
| 699 |
+
"text": [
|
| 700 |
+
"\n"
|
| 701 |
+
]
|
| 702 |
+
}
|
| 703 |
+
],
|
| 704 |
+
"source": [
|
| 705 |
+
"eval_test_set(model, tokenizer, test_loader, device)"
|
| 706 |
+
]
|
| 707 |
+
},
|
| 708 |
+
{
|
| 709 |
+
"cell_type": "markdown",
|
| 710 |
+
"id": "fa6017a8",
|
| 711 |
+
"metadata": {},
|
| 712 |
+
"source": [
|
| 713 |
+
"## Freeze baseline and train new head\n",
|
| 714 |
+
"This was my initial idea, to freeze the layers and add a completely new head, which we train from scratch. I tried a lot of different configurations, but nothing really worked, I usually stayed at a CrossEntropyLoss of about 3 the whole time. Below, you can see the different heads I have tried.\n",
|
| 715 |
+
"\n",
|
| 716 |
+
"Furthermore, I experimented with different data, because I though it might not be enough data all in all. I would conclude that this didn't work because (1) Transformers are very data-hungry and I probably still used too little data (one epoch took about 1h though, so it wasn't possible to use even more). (2) We train the layers completely new, which means they contain absolutely no structure about the problem and task beforehand. I do not think that this way of training leads to better results / less energy used all in all, because it would be too resource intense.\n",
|
| 717 |
+
"\n",
|
| 718 |
+
"The following setup is partly based on the HuggingFace implementation of the question answering model (https://github.com/huggingface/transformers/blob/v4.23.1/src/transformers/models/distilbert/modeling_distilbert.py#L805)"
|
| 719 |
+
]
|
| 720 |
+
},
|
| 721 |
+
{
|
| 722 |
+
"cell_type": "code",
|
| 723 |
+
"execution_count": 62,
|
| 724 |
+
"id": "92b21967",
|
| 725 |
+
"metadata": {},
|
| 726 |
+
"outputs": [],
|
| 727 |
+
"source": [
|
| 728 |
+
"model = DistilBertForMaskedLM.from_pretrained(\"distilbert-base-uncased\")"
|
| 729 |
+
]
|
| 730 |
+
},
|
| 731 |
+
{
|
| 732 |
+
"cell_type": "code",
|
| 733 |
+
"execution_count": 63,
|
| 734 |
+
"id": "1d7b3a8c",
|
| 735 |
+
"metadata": {},
|
| 736 |
+
"outputs": [],
|
| 737 |
+
"source": [
|
| 738 |
+
"config = DistilBertConfig.from_pretrained(\"distilbert-base-uncased\")"
|
| 739 |
+
]
|
| 740 |
+
},
|
| 741 |
+
{
|
| 742 |
+
"cell_type": "code",
|
| 743 |
+
"execution_count": 64,
|
| 744 |
+
"id": "91444894",
|
| 745 |
+
"metadata": {},
|
| 746 |
+
"outputs": [],
|
| 747 |
+
"source": [
|
| 748 |
+
"# only take base model, we do not need the classification head\n",
|
| 749 |
+
"mod = model.distilbert"
|
| 750 |
+
]
|
| 751 |
+
},
|
| 752 |
+
{
|
| 753 |
+
"cell_type": "code",
|
| 754 |
+
"execution_count": 65,
|
| 755 |
+
"id": "74ca6c07",
|
| 756 |
+
"metadata": {},
|
| 757 |
+
"outputs": [
|
| 758 |
+
{
|
| 759 |
+
"data": {
|
| 760 |
+
"text/plain": [
|
| 761 |
+
"QuestionDistilBERT(\n",
|
| 762 |
+
" (distilbert): DistilBertModel(\n",
|
| 763 |
+
" (embeddings): Embeddings(\n",
|
| 764 |
+
" (word_embeddings): Embedding(30522, 768, padding_idx=0)\n",
|
| 765 |
+
" (position_embeddings): Embedding(512, 768)\n",
|
| 766 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 767 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 768 |
+
" )\n",
|
| 769 |
+
" (transformer): Transformer(\n",
|
| 770 |
+
" (layer): ModuleList(\n",
|
| 771 |
+
" (0): TransformerBlock(\n",
|
| 772 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 773 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 774 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 775 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 776 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 777 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 778 |
+
" )\n",
|
| 779 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 780 |
+
" (ffn): FFN(\n",
|
| 781 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 782 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 783 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 784 |
+
" (activation): GELUActivation()\n",
|
| 785 |
+
" )\n",
|
| 786 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 787 |
+
" )\n",
|
| 788 |
+
" (1): TransformerBlock(\n",
|
| 789 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 790 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 791 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 792 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 793 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 794 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 795 |
+
" )\n",
|
| 796 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 797 |
+
" (ffn): FFN(\n",
|
| 798 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 799 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 800 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 801 |
+
" (activation): GELUActivation()\n",
|
| 802 |
+
" )\n",
|
| 803 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 804 |
+
" )\n",
|
| 805 |
+
" (2): TransformerBlock(\n",
|
| 806 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 807 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 808 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 809 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 810 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 811 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 812 |
+
" )\n",
|
| 813 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 814 |
+
" (ffn): FFN(\n",
|
| 815 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 816 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 817 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 818 |
+
" (activation): GELUActivation()\n",
|
| 819 |
+
" )\n",
|
| 820 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 821 |
+
" )\n",
|
| 822 |
+
" (3): TransformerBlock(\n",
|
| 823 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 824 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 825 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 826 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 827 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 828 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 829 |
+
" )\n",
|
| 830 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 831 |
+
" (ffn): FFN(\n",
|
| 832 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 833 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 834 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 835 |
+
" (activation): GELUActivation()\n",
|
| 836 |
+
" )\n",
|
| 837 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 838 |
+
" )\n",
|
| 839 |
+
" (4): TransformerBlock(\n",
|
| 840 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 841 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 842 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 843 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 844 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 845 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 846 |
+
" )\n",
|
| 847 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 848 |
+
" (ffn): FFN(\n",
|
| 849 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 850 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 851 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 852 |
+
" (activation): GELUActivation()\n",
|
| 853 |
+
" )\n",
|
| 854 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 855 |
+
" )\n",
|
| 856 |
+
" (5): TransformerBlock(\n",
|
| 857 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 858 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 859 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 860 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 861 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 862 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 863 |
+
" )\n",
|
| 864 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 865 |
+
" (ffn): FFN(\n",
|
| 866 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 867 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 868 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 869 |
+
" (activation): GELUActivation()\n",
|
| 870 |
+
" )\n",
|
| 871 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 872 |
+
" )\n",
|
| 873 |
+
" )\n",
|
| 874 |
+
" )\n",
|
| 875 |
+
" )\n",
|
| 876 |
+
" (relu): ReLU()\n",
|
| 877 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 878 |
+
" (te): TransformerEncoder(\n",
|
| 879 |
+
" (layers): ModuleList(\n",
|
| 880 |
+
" (0): TransformerEncoderLayer(\n",
|
| 881 |
+
" (self_attn): MultiheadAttention(\n",
|
| 882 |
+
" (out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True)\n",
|
| 883 |
+
" )\n",
|
| 884 |
+
" (linear1): Linear(in_features=768, out_features=2048, bias=True)\n",
|
| 885 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 886 |
+
" (linear2): Linear(in_features=2048, out_features=768, bias=True)\n",
|
| 887 |
+
" (norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
|
| 888 |
+
" (norm2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
|
| 889 |
+
" (dropout1): Dropout(p=0.1, inplace=False)\n",
|
| 890 |
+
" (dropout2): Dropout(p=0.1, inplace=False)\n",
|
| 891 |
+
" )\n",
|
| 892 |
+
" (1): TransformerEncoderLayer(\n",
|
| 893 |
+
" (self_attn): MultiheadAttention(\n",
|
| 894 |
+
" (out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True)\n",
|
| 895 |
+
" )\n",
|
| 896 |
+
" (linear1): Linear(in_features=768, out_features=2048, bias=True)\n",
|
| 897 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 898 |
+
" (linear2): Linear(in_features=2048, out_features=768, bias=True)\n",
|
| 899 |
+
" (norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
|
| 900 |
+
" (norm2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
|
| 901 |
+
" (dropout1): Dropout(p=0.1, inplace=False)\n",
|
| 902 |
+
" (dropout2): Dropout(p=0.1, inplace=False)\n",
|
| 903 |
+
" )\n",
|
| 904 |
+
" (2): TransformerEncoderLayer(\n",
|
| 905 |
+
" (self_attn): MultiheadAttention(\n",
|
| 906 |
+
" (out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True)\n",
|
| 907 |
+
" )\n",
|
| 908 |
+
" (linear1): Linear(in_features=768, out_features=2048, bias=True)\n",
|
| 909 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 910 |
+
" (linear2): Linear(in_features=2048, out_features=768, bias=True)\n",
|
| 911 |
+
" (norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
|
| 912 |
+
" (norm2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
|
| 913 |
+
" (dropout1): Dropout(p=0.1, inplace=False)\n",
|
| 914 |
+
" (dropout2): Dropout(p=0.1, inplace=False)\n",
|
| 915 |
+
" )\n",
|
| 916 |
+
" )\n",
|
| 917 |
+
" )\n",
|
| 918 |
+
" (classifier): Sequential(\n",
|
| 919 |
+
" (0): Dropout(p=0.1, inplace=False)\n",
|
| 920 |
+
" (1): ReLU()\n",
|
| 921 |
+
" (2): Linear(in_features=768, out_features=512, bias=True)\n",
|
| 922 |
+
" (3): Dropout(p=0.1, inplace=False)\n",
|
| 923 |
+
" (4): ReLU()\n",
|
| 924 |
+
" (5): Linear(in_features=512, out_features=256, bias=True)\n",
|
| 925 |
+
" (6): Dropout(p=0.1, inplace=False)\n",
|
| 926 |
+
" (7): ReLU()\n",
|
| 927 |
+
" (8): Linear(in_features=256, out_features=128, bias=True)\n",
|
| 928 |
+
" (9): Dropout(p=0.1, inplace=False)\n",
|
| 929 |
+
" (10): ReLU()\n",
|
| 930 |
+
" (11): Linear(in_features=128, out_features=64, bias=True)\n",
|
| 931 |
+
" (12): Dropout(p=0.1, inplace=False)\n",
|
| 932 |
+
" (13): ReLU()\n",
|
| 933 |
+
" (14): Linear(in_features=64, out_features=2, bias=True)\n",
|
| 934 |
+
" )\n",
|
| 935 |
+
")"
|
| 936 |
+
]
|
| 937 |
+
},
|
| 938 |
+
"execution_count": 65,
|
| 939 |
+
"metadata": {},
|
| 940 |
+
"output_type": "execute_result"
|
| 941 |
+
}
|
| 942 |
+
],
|
| 943 |
+
"source": [
|
| 944 |
+
"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
|
| 945 |
+
"model = QuestionDistilBERT(mod)\n",
|
| 946 |
+
"model.to(device)"
|
| 947 |
+
]
|
| 948 |
+
},
|
| 949 |
+
{
|
| 950 |
+
"cell_type": "code",
|
| 951 |
+
"execution_count": 66,
|
| 952 |
+
"id": "340857f9",
|
| 953 |
+
"metadata": {},
|
| 954 |
+
"outputs": [
|
| 955 |
+
{
|
| 956 |
+
"name": "stdout",
|
| 957 |
+
"output_type": "stream",
|
| 958 |
+
"text": [
|
| 959 |
+
"+---------------------------------------+------------+\n",
|
| 960 |
+
"| Modules | Parameters |\n",
|
| 961 |
+
"+---------------------------------------+------------+\n",
|
| 962 |
+
"| te.layers.0.self_attn.in_proj_weight | 1769472 |\n",
|
| 963 |
+
"| te.layers.0.self_attn.in_proj_bias | 2304 |\n",
|
| 964 |
+
"| te.layers.0.self_attn.out_proj.weight | 589824 |\n",
|
| 965 |
+
"| te.layers.0.self_attn.out_proj.bias | 768 |\n",
|
| 966 |
+
"| te.layers.0.linear1.weight | 1572864 |\n",
|
| 967 |
+
"| te.layers.0.linear1.bias | 2048 |\n",
|
| 968 |
+
"| te.layers.0.linear2.weight | 1572864 |\n",
|
| 969 |
+
"| te.layers.0.linear2.bias | 768 |\n",
|
| 970 |
+
"| te.layers.0.norm1.weight | 768 |\n",
|
| 971 |
+
"| te.layers.0.norm1.bias | 768 |\n",
|
| 972 |
+
"| te.layers.0.norm2.weight | 768 |\n",
|
| 973 |
+
"| te.layers.0.norm2.bias | 768 |\n",
|
| 974 |
+
"| te.layers.1.self_attn.in_proj_weight | 1769472 |\n",
|
| 975 |
+
"| te.layers.1.self_attn.in_proj_bias | 2304 |\n",
|
| 976 |
+
"| te.layers.1.self_attn.out_proj.weight | 589824 |\n",
|
| 977 |
+
"| te.layers.1.self_attn.out_proj.bias | 768 |\n",
|
| 978 |
+
"| te.layers.1.linear1.weight | 1572864 |\n",
|
| 979 |
+
"| te.layers.1.linear1.bias | 2048 |\n",
|
| 980 |
+
"| te.layers.1.linear2.weight | 1572864 |\n",
|
| 981 |
+
"| te.layers.1.linear2.bias | 768 |\n",
|
| 982 |
+
"| te.layers.1.norm1.weight | 768 |\n",
|
| 983 |
+
"| te.layers.1.norm1.bias | 768 |\n",
|
| 984 |
+
"| te.layers.1.norm2.weight | 768 |\n",
|
| 985 |
+
"| te.layers.1.norm2.bias | 768 |\n",
|
| 986 |
+
"| te.layers.2.self_attn.in_proj_weight | 1769472 |\n",
|
| 987 |
+
"| te.layers.2.self_attn.in_proj_bias | 2304 |\n",
|
| 988 |
+
"| te.layers.2.self_attn.out_proj.weight | 589824 |\n",
|
| 989 |
+
"| te.layers.2.self_attn.out_proj.bias | 768 |\n",
|
| 990 |
+
"| te.layers.2.linear1.weight | 1572864 |\n",
|
| 991 |
+
"| te.layers.2.linear1.bias | 2048 |\n",
|
| 992 |
+
"| te.layers.2.linear2.weight | 1572864 |\n",
|
| 993 |
+
"| te.layers.2.linear2.bias | 768 |\n",
|
| 994 |
+
"| te.layers.2.norm1.weight | 768 |\n",
|
| 995 |
+
"| te.layers.2.norm1.bias | 768 |\n",
|
| 996 |
+
"| te.layers.2.norm2.weight | 768 |\n",
|
| 997 |
+
"| te.layers.2.norm2.bias | 768 |\n",
|
| 998 |
+
"| classifier.2.weight | 393216 |\n",
|
| 999 |
+
"| classifier.2.bias | 512 |\n",
|
| 1000 |
+
"| classifier.5.weight | 131072 |\n",
|
| 1001 |
+
"| classifier.5.bias | 256 |\n",
|
| 1002 |
+
"| classifier.8.weight | 32768 |\n",
|
| 1003 |
+
"| classifier.8.bias | 128 |\n",
|
| 1004 |
+
"| classifier.11.weight | 8192 |\n",
|
| 1005 |
+
"| classifier.11.bias | 64 |\n",
|
| 1006 |
+
"| classifier.14.weight | 128 |\n",
|
| 1007 |
+
"| classifier.14.bias | 2 |\n",
|
| 1008 |
+
"+---------------------------------------+------------+\n",
|
| 1009 |
+
"Total Trainable Params: 17108290\n"
|
| 1010 |
+
]
|
| 1011 |
+
},
|
| 1012 |
+
{
|
| 1013 |
+
"data": {
|
| 1014 |
+
"text/plain": [
|
| 1015 |
+
"17108290"
|
| 1016 |
+
]
|
| 1017 |
+
},
|
| 1018 |
+
"execution_count": 66,
|
| 1019 |
+
"metadata": {},
|
| 1020 |
+
"output_type": "execute_result"
|
| 1021 |
+
}
|
| 1022 |
+
],
|
| 1023 |
+
"source": [
|
| 1024 |
+
"count_parameters(model)"
|
| 1025 |
+
]
|
| 1026 |
+
},
|
| 1027 |
+
{
|
| 1028 |
+
"cell_type": "markdown",
|
| 1029 |
+
"id": "9babd013",
|
| 1030 |
+
"metadata": {},
|
| 1031 |
+
"source": [
|
| 1032 |
+
"### Testing the model\n",
|
| 1033 |
+
"This is the same procedure as in `distilbert.ipynb`. "
|
| 1034 |
+
]
|
| 1035 |
+
},
|
| 1036 |
+
{
|
| 1037 |
+
"cell_type": "code",
|
| 1038 |
+
"execution_count": 67,
|
| 1039 |
+
"id": "694c828b",
|
| 1040 |
+
"metadata": {},
|
| 1041 |
+
"outputs": [],
|
| 1042 |
+
"source": [
|
| 1043 |
+
"# get smaller dataset\n",
|
| 1044 |
+
"batch_size = 8\n",
|
| 1045 |
+
"test_ds = Dataset(squad_paths = squad_paths[:2], natural_question_paths=None, hotpotqa_paths=None, tokenizer=tokenizer)\n",
|
| 1046 |
+
"test_ds_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size)\n",
|
| 1047 |
+
"optim=torch.optim.Adam(model.parameters())"
|
| 1048 |
+
]
|
| 1049 |
+
},
|
| 1050 |
+
{
|
| 1051 |
+
"cell_type": "code",
|
| 1052 |
+
"execution_count": 68,
|
| 1053 |
+
"id": "a76587df",
|
| 1054 |
+
"metadata": {},
|
| 1055 |
+
"outputs": [
|
| 1056 |
+
{
|
| 1057 |
+
"name": "stdout",
|
| 1058 |
+
"output_type": "stream",
|
| 1059 |
+
"text": [
|
| 1060 |
+
"Passed\n"
|
| 1061 |
+
]
|
| 1062 |
+
}
|
| 1063 |
+
],
|
| 1064 |
+
"source": [
|
| 1065 |
+
"test_model(model, optim, test_ds_loader, device)"
|
| 1066 |
+
]
|
| 1067 |
+
},
|
| 1068 |
+
{
|
| 1069 |
+
"cell_type": "markdown",
|
| 1070 |
+
"id": "7c326e8e",
|
| 1071 |
+
"metadata": {},
|
| 1072 |
+
"source": [
|
| 1073 |
+
"### Training the model\n",
|
| 1074 |
+
"* Parameter Tuning:\n",
|
| 1075 |
+
" * Learning Rate: I experimented with several values, 1e-4 seemed to work best for me. 1e-3 was very unstable and 1e-5 was too small.\n",
|
| 1076 |
+
" * Gradient Clipping: I experimented with this, but the difference was only minimal\n",
|
| 1077 |
+
"\n",
|
| 1078 |
+
"Data:\n",
|
| 1079 |
+
"* I first used only the SQuAD dataset, but generalisation is a problem\n",
|
| 1080 |
+
" * The dataset is realtively small and we often have entries with the same context but different questions\n",
|
| 1081 |
+
" * I believe, the diversity is not big enough to train a fully functional model\n",
|
| 1082 |
+
"* Hence, I included the Natural Questions dataset too\n",
|
| 1083 |
+
" * It is however a lot more messy - I elaborated a bit more on this in `load_data.ipynb`\n",
|
| 1084 |
+
"* Also the hotpotqa data was used\n",
|
| 1085 |
+
"\n",
|
| 1086 |
+
"Tested with: \n",
|
| 1087 |
+
"* 3 Linear Layers\n",
|
| 1088 |
+
" * Training Error high - needed more layers\n",
|
| 1089 |
+
" * Already expected - this was mostly a Proof of Concept\n",
|
| 1090 |
+
"* 1 TransformerEncoder with 4 attention heads + 1 Linear Layer:\n",
|
| 1091 |
+
" * Training Error was high, still too simple\n",
|
| 1092 |
+
"* 1 TransformerEncoder with 8 heads + 1 Linear Layer:\n",
|
| 1093 |
+
" * Training Error gets lower, however stagnates at some point\n",
|
| 1094 |
+
" * Probably still too simple, it doesn't generalise either\n",
|
| 1095 |
+
"* 2 TransformerEncoder with 8 and 4 heads + 1 Linear Layer:\n",
|
| 1096 |
+
" * Loss gets down but doesn't go further after some time\n"
|
| 1097 |
+
]
|
| 1098 |
+
},
|
| 1099 |
+
{
|
| 1100 |
+
"cell_type": "code",
|
| 1101 |
+
"execution_count": null,
|
| 1102 |
+
"id": "2e9f4bd3",
|
| 1103 |
+
"metadata": {},
|
| 1104 |
+
"outputs": [],
|
| 1105 |
+
"source": [
|
| 1106 |
+
"dataset = Dataset(squad_paths = squad_paths, natural_question_paths=nat_paths, hotpotqa_paths=hotpotqa_paths, tokenizer=tokenizer)\n",
|
| 1107 |
+
"loader = torch.utils.data.DataLoader(dataset, batch_size=8)\n",
|
| 1108 |
+
"\n",
|
| 1109 |
+
"test_dataset = Dataset(squad_paths = [str(x) for x in Path('data/test_squad/').glob('**/*.txt')], \n",
|
| 1110 |
+
" natural_question_paths=None, \n",
|
| 1111 |
+
" hotpotqa_paths = None, tokenizer=tokenizer)\n",
|
| 1112 |
+
"test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4)"
|
| 1113 |
+
]
|
| 1114 |
+
},
|
| 1115 |
+
{
|
| 1116 |
+
"cell_type": "code",
|
| 1117 |
+
"execution_count": 26,
|
| 1118 |
+
"id": "03a6de37",
|
| 1119 |
+
"metadata": {},
|
| 1120 |
+
"outputs": [],
|
| 1121 |
+
"source": [
|
| 1122 |
+
"model = QuestionDistilBERT(mod)"
|
| 1123 |
+
]
|
| 1124 |
+
},
|
| 1125 |
+
{
|
| 1126 |
+
"cell_type": "code",
|
| 1127 |
+
"execution_count": 41,
|
| 1128 |
+
"id": "ed854b73",
|
| 1129 |
+
"metadata": {},
|
| 1130 |
+
"outputs": [],
|
| 1131 |
+
"source": [
|
| 1132 |
+
"from torch.optim import AdamW, RMSprop\n",
|
| 1133 |
+
"\n",
|
| 1134 |
+
"model.train()\n",
|
| 1135 |
+
"optim = RMSprop(model.parameters(), lr=1e-4)"
|
| 1136 |
+
]
|
| 1137 |
+
},
|
| 1138 |
+
{
|
| 1139 |
+
"cell_type": "code",
|
| 1140 |
+
"execution_count": 42,
|
| 1141 |
+
"id": "79fdfcc9",
|
| 1142 |
+
"metadata": {},
|
| 1143 |
+
"outputs": [],
|
| 1144 |
+
"source": [
|
| 1145 |
+
"from torch.utils.tensorboard import SummaryWriter\n",
|
| 1146 |
+
"writer = SummaryWriter()"
|
| 1147 |
+
]
|
| 1148 |
+
},
|
| 1149 |
+
{
|
| 1150 |
+
"cell_type": "code",
|
| 1151 |
+
"execution_count": null,
|
| 1152 |
+
"id": "f7bddb43",
|
| 1153 |
+
"metadata": {},
|
| 1154 |
+
"outputs": [],
|
| 1155 |
+
"source": [
|
| 1156 |
+
"epochs = 20\n",
|
| 1157 |
+
"\n",
|
| 1158 |
+
"for epoch in range(epochs):\n",
|
| 1159 |
+
" loop = tqdm(loader, leave=True)\n",
|
| 1160 |
+
" model.train()\n",
|
| 1161 |
+
" mean_training_error = []\n",
|
| 1162 |
+
" for batch in loop:\n",
|
| 1163 |
+
" optim.zero_grad()\n",
|
| 1164 |
+
" \n",
|
| 1165 |
+
" input_ids = batch['input_ids'].to(device)\n",
|
| 1166 |
+
" attention_mask = batch['attention_mask'].to(device)\n",
|
| 1167 |
+
" start = batch['start_positions'].to(device)\n",
|
| 1168 |
+
" end = batch['end_positions'].to(device)\n",
|
| 1169 |
+
" \n",
|
| 1170 |
+
" outputs = model(input_ids, attention_mask=attention_mask, start_positions=start, end_positions=end)\n",
|
| 1171 |
+
" \n",
|
| 1172 |
+
" loss = outputs['loss']\n",
|
| 1173 |
+
" loss.backward()\n",
|
| 1174 |
+
" \n",
|
| 1175 |
+
" optim.step()\n",
|
| 1176 |
+
" mean_training_error.append(loss.item())\n",
|
| 1177 |
+
" loop.set_description(f'Epoch {epoch}')\n",
|
| 1178 |
+
" loop.set_postfix(loss=loss.item())\n",
|
| 1179 |
+
" print(\"Mean Training Error\", np.mean(mean_training_error))\n",
|
| 1180 |
+
" writer.add_scalar(\"Loss/train\", np.mean(mean_training_error), epoch)\n",
|
| 1181 |
+
" \n",
|
| 1182 |
+
" loop = tqdm(test_loader, leave=True)\n",
|
| 1183 |
+
" model.eval()\n",
|
| 1184 |
+
" mean_test_error = []\n",
|
| 1185 |
+
" for batch in loop:\n",
|
| 1186 |
+
" \n",
|
| 1187 |
+
" input_ids = batch['input_ids'].to(device)\n",
|
| 1188 |
+
" attention_mask = batch['attention_mask'].to(device)\n",
|
| 1189 |
+
" start = batch['start_positions'].to(device)\n",
|
| 1190 |
+
" end = batch['end_positions'].to(device)\n",
|
| 1191 |
+
" \n",
|
| 1192 |
+
" outputs = model(input_ids, attention_mask=attention_mask, start_positions=start, end_positions=end)\n",
|
| 1193 |
+
" # print(torch.argmax(outputs['start_logits'],axis=1), torch.argmax(outputs['end_logits'], axis=1), start, end)\n",
|
| 1194 |
+
" loss = outputs['loss']\n",
|
| 1195 |
+
" \n",
|
| 1196 |
+
" mean_test_error.append(loss.item())\n",
|
| 1197 |
+
" loop.set_description(f'Epoch {epoch} Testset')\n",
|
| 1198 |
+
" loop.set_postfix(loss=loss.item())\n",
|
| 1199 |
+
" print(\"Mean Test Error\", np.mean(mean_test_error))\n",
|
| 1200 |
+
" writer.add_scalar(\"Loss/test\", np.mean(mean_test_error), epoch)"
|
| 1201 |
+
]
|
| 1202 |
+
},
|
| 1203 |
+
{
|
| 1204 |
+
"cell_type": "code",
|
| 1205 |
+
"execution_count": 238,
|
| 1206 |
+
"id": "a9d6af2e",
|
| 1207 |
+
"metadata": {},
|
| 1208 |
+
"outputs": [],
|
| 1209 |
+
"source": [
|
| 1210 |
+
"writer.close()"
|
| 1211 |
+
]
|
| 1212 |
+
},
|
| 1213 |
+
{
|
| 1214 |
+
"cell_type": "code",
|
| 1215 |
+
"execution_count": 33,
|
| 1216 |
+
"id": "ba43447e",
|
| 1217 |
+
"metadata": {},
|
| 1218 |
+
"outputs": [],
|
| 1219 |
+
"source": [
|
| 1220 |
+
"torch.save(model.state_dict(), \"distilbert_qa.model\")"
|
| 1221 |
+
]
|
| 1222 |
+
},
|
| 1223 |
+
{
|
| 1224 |
+
"cell_type": "code",
|
| 1225 |
+
"execution_count": 34,
|
| 1226 |
+
"id": "ffc49aca",
|
| 1227 |
+
"metadata": {},
|
| 1228 |
+
"outputs": [
|
| 1229 |
+
{
|
| 1230 |
+
"data": {
|
| 1231 |
+
"text/plain": [
|
| 1232 |
+
"<All keys matched successfully>"
|
| 1233 |
+
]
|
| 1234 |
+
},
|
| 1235 |
+
"execution_count": 34,
|
| 1236 |
+
"metadata": {},
|
| 1237 |
+
"output_type": "execute_result"
|
| 1238 |
+
}
|
| 1239 |
+
],
|
| 1240 |
+
"source": [
|
| 1241 |
+
"model = QuestionDistilBERT(mod)\n",
|
| 1242 |
+
"model.load_state_dict(torch.load(\"distilbert_qa.model\"))"
|
| 1243 |
+
]
|
| 1244 |
+
},
|
| 1245 |
+
{
|
| 1246 |
+
"cell_type": "code",
|
| 1247 |
+
"execution_count": 35,
|
| 1248 |
+
"id": "730a86c1",
|
| 1249 |
+
"metadata": {},
|
| 1250 |
+
"outputs": [
|
| 1251 |
+
{
|
| 1252 |
+
"name": "stderr",
|
| 1253 |
+
"output_type": "stream",
|
| 1254 |
+
"text": [
|
| 1255 |
+
"100%|鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 2500/2500 [02:57<00:00, 14.09it/s]"
|
| 1256 |
+
]
|
| 1257 |
+
},
|
| 1258 |
+
{
|
| 1259 |
+
"name": "stdout",
|
| 1260 |
+
"output_type": "stream",
|
| 1261 |
+
"text": [
|
| 1262 |
+
"Mean EM: 0.0479\n",
|
| 1263 |
+
"Mean F-1: 0.08989175857485086\n"
|
| 1264 |
+
]
|
| 1265 |
+
},
|
| 1266 |
+
{
|
| 1267 |
+
"name": "stderr",
|
| 1268 |
+
"output_type": "stream",
|
| 1269 |
+
"text": [
|
| 1270 |
+
"\n"
|
| 1271 |
+
]
|
| 1272 |
+
}
|
| 1273 |
+
],
|
| 1274 |
+
"source": [
|
| 1275 |
+
"eval_test_set(model, tokenizer, test_loader, device)"
|
| 1276 |
+
]
|
| 1277 |
+
},
|
| 1278 |
+
{
|
| 1279 |
+
"cell_type": "markdown",
|
| 1280 |
+
"id": "bd1c7076",
|
| 1281 |
+
"metadata": {},
|
| 1282 |
+
"source": [
|
| 1283 |
+
"## Reuse Layer\n",
|
| 1284 |
+
"This was inspired by how well the original model with just one classification head worked. I felt like the main problem with the previous model was the lack of structure which was already in the layers, combined with the massive amount of resources needed for a Transformer.\n",
|
| 1285 |
+
"\n",
|
| 1286 |
+
"Hence, I tried cloning the last (and then last two) layers of the DistilBERT model, putting a classifier on top and using this as the head. The base DistilBERT model is completely frozen. This worked extremely well, while we only fine-tune about 21% of the parameters (14 Mio as opposed to 66 Mio!) we did before. Below you can see the results.\n",
|
| 1287 |
+
"\n",
|
| 1288 |
+
"### Last DistilBERT layer\n",
|
| 1289 |
+
"\n",
|
| 1290 |
+
"Dropout 0.1 and RMSprop 1e-4:\n",
|
| 1291 |
+
"* Mean EM: 0.3888\n",
|
| 1292 |
+
"* Mean F-1: 0.5122932744694068\n",
|
| 1293 |
+
"\n",
|
| 1294 |
+
"Dropout 0.25: very early stagnating\n",
|
| 1295 |
+
"* Mean EM: 0.3552\n",
|
| 1296 |
+
"* Mean F-1: 0.4711235721312687\n",
|
| 1297 |
+
"\n",
|
| 1298 |
+
"Dropout 0.15: seems to work well - training and test error stagnate around 1.7 and 1.8 but good generalisation (need to add more layers)\n",
|
| 1299 |
+
"* Mean EM: 0.4119\n",
|
| 1300 |
+
"* Mean F-1: 0.5296387232893214\n",
|
| 1301 |
+
"\n",
|
| 1302 |
+
"### Last DitilBERT layer + more Dense layers\n",
|
| 1303 |
+
"Dropout 0.15 + 4 dense layers((786-512)-(512-256)-(256-128)-(128-2)) & ReLU: doesn't work too well - stagnates at around 2.4\n",
|
| 1304 |
+
"\n",
|
| 1305 |
+
"### Last two DistilBERT layers\n",
|
| 1306 |
+
"Dropout 0.1 but last 2 DistilBERT layers: works very well, but early overfitting - maybe use more data\n",
|
| 1307 |
+
"* Mean EM: 0.458\n",
|
| 1308 |
+
"* Mean F-1: 0.6003368353673634\n",
|
| 1309 |
+
"\n",
|
| 1310 |
+
"Dropout 0.1 - last 2 distilbert layers: all data\n",
|
| 1311 |
+
"* Mean EM: 0.484\n",
|
| 1312 |
+
"* Mean F-1: 0.6344960035215299\n",
|
| 1313 |
+
"\n",
|
| 1314 |
+
"Dropout 0.15 - **BEST**\n",
|
| 1315 |
+
"* Mean EM: 0.5178\n",
|
| 1316 |
+
"* Mean F-1: 0.6671140689626448\n",
|
| 1317 |
+
"\n",
|
| 1318 |
+
"Dropout 0.2 - doesn't work too well\n",
|
| 1319 |
+
"* Mean EM: 0.4353\n",
|
| 1320 |
+
"* Mean F-1: 0.5776847879304647\n"
|
| 1321 |
+
]
|
| 1322 |
+
},
|
| 1323 |
+
{
|
| 1324 |
+
"cell_type": "code",
|
| 1325 |
+
"execution_count": 69,
|
| 1326 |
+
"id": "654e09e8",
|
| 1327 |
+
"metadata": {},
|
| 1328 |
+
"outputs": [],
|
| 1329 |
+
"source": [
|
| 1330 |
+
"dataset = Dataset(squad_paths = squad_paths, natural_question_paths=None, hotpotqa_paths=hotpotqa_paths, tokenizer=tokenizer)\n",
|
| 1331 |
+
"loader = torch.utils.data.DataLoader(dataset, batch_size=8)\n",
|
| 1332 |
+
"\n",
|
| 1333 |
+
"test_dataset = Dataset(squad_paths = [str(x) for x in Path('data/test_squad/').glob('**/*.txt')], \n",
|
| 1334 |
+
" natural_question_paths=None, \n",
|
| 1335 |
+
" hotpotqa_paths = None, tokenizer=tokenizer)\n",
|
| 1336 |
+
"test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4)"
|
| 1337 |
+
]
|
| 1338 |
+
},
|
| 1339 |
+
{
|
| 1340 |
+
"cell_type": "code",
|
| 1341 |
+
"execution_count": 70,
|
| 1342 |
+
"id": "707c0cb5",
|
| 1343 |
+
"metadata": {},
|
| 1344 |
+
"outputs": [
|
| 1345 |
+
{
|
| 1346 |
+
"data": {
|
| 1347 |
+
"text/plain": [
|
| 1348 |
+
"ReuseQuestionDistilBERT(\n",
|
| 1349 |
+
" (te): ModuleList(\n",
|
| 1350 |
+
" (0): TransformerBlock(\n",
|
| 1351 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1352 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1353 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1354 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1355 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1356 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1357 |
+
" )\n",
|
| 1358 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1359 |
+
" (ffn): FFN(\n",
|
| 1360 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1361 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1362 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1363 |
+
" (activation): GELUActivation()\n",
|
| 1364 |
+
" )\n",
|
| 1365 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1366 |
+
" )\n",
|
| 1367 |
+
" (1): TransformerBlock(\n",
|
| 1368 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1369 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1370 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1371 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1372 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1373 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1374 |
+
" )\n",
|
| 1375 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1376 |
+
" (ffn): FFN(\n",
|
| 1377 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1378 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1379 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1380 |
+
" (activation): GELUActivation()\n",
|
| 1381 |
+
" )\n",
|
| 1382 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1383 |
+
" )\n",
|
| 1384 |
+
" )\n",
|
| 1385 |
+
" (distilbert): DistilBertModel(\n",
|
| 1386 |
+
" (embeddings): Embeddings(\n",
|
| 1387 |
+
" (word_embeddings): Embedding(30522, 768, padding_idx=0)\n",
|
| 1388 |
+
" (position_embeddings): Embedding(512, 768)\n",
|
| 1389 |
+
" (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1390 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1391 |
+
" )\n",
|
| 1392 |
+
" (transformer): Transformer(\n",
|
| 1393 |
+
" (layer): ModuleList(\n",
|
| 1394 |
+
" (0): TransformerBlock(\n",
|
| 1395 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1396 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1397 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1398 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1399 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1400 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1401 |
+
" )\n",
|
| 1402 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1403 |
+
" (ffn): FFN(\n",
|
| 1404 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1405 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1406 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1407 |
+
" (activation): GELUActivation()\n",
|
| 1408 |
+
" )\n",
|
| 1409 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1410 |
+
" )\n",
|
| 1411 |
+
" (1): TransformerBlock(\n",
|
| 1412 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1413 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1414 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1415 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1416 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1417 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1418 |
+
" )\n",
|
| 1419 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1420 |
+
" (ffn): FFN(\n",
|
| 1421 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1422 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1423 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1424 |
+
" (activation): GELUActivation()\n",
|
| 1425 |
+
" )\n",
|
| 1426 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1427 |
+
" )\n",
|
| 1428 |
+
" (2): TransformerBlock(\n",
|
| 1429 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1430 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1431 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1432 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1433 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1434 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1435 |
+
" )\n",
|
| 1436 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1437 |
+
" (ffn): FFN(\n",
|
| 1438 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1439 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1440 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1441 |
+
" (activation): GELUActivation()\n",
|
| 1442 |
+
" )\n",
|
| 1443 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1444 |
+
" )\n",
|
| 1445 |
+
" (3): TransformerBlock(\n",
|
| 1446 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1447 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1448 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1449 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1450 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1451 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1452 |
+
" )\n",
|
| 1453 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1454 |
+
" (ffn): FFN(\n",
|
| 1455 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1456 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1457 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1458 |
+
" (activation): GELUActivation()\n",
|
| 1459 |
+
" )\n",
|
| 1460 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1461 |
+
" )\n",
|
| 1462 |
+
" (4): TransformerBlock(\n",
|
| 1463 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1464 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1465 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1466 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1467 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1468 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1469 |
+
" )\n",
|
| 1470 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1471 |
+
" (ffn): FFN(\n",
|
| 1472 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1473 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1474 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1475 |
+
" (activation): GELUActivation()\n",
|
| 1476 |
+
" )\n",
|
| 1477 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1478 |
+
" )\n",
|
| 1479 |
+
" (5): TransformerBlock(\n",
|
| 1480 |
+
" (attention): MultiHeadSelfAttention(\n",
|
| 1481 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1482 |
+
" (q_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1483 |
+
" (k_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1484 |
+
" (v_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1485 |
+
" (out_lin): Linear(in_features=768, out_features=768, bias=True)\n",
|
| 1486 |
+
" )\n",
|
| 1487 |
+
" (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1488 |
+
" (ffn): FFN(\n",
|
| 1489 |
+
" (dropout): Dropout(p=0.1, inplace=False)\n",
|
| 1490 |
+
" (lin1): Linear(in_features=768, out_features=3072, bias=True)\n",
|
| 1491 |
+
" (lin2): Linear(in_features=3072, out_features=768, bias=True)\n",
|
| 1492 |
+
" (activation): GELUActivation()\n",
|
| 1493 |
+
" )\n",
|
| 1494 |
+
" (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
|
| 1495 |
+
" )\n",
|
| 1496 |
+
" )\n",
|
| 1497 |
+
" )\n",
|
| 1498 |
+
" )\n",
|
| 1499 |
+
" (relu): ReLU()\n",
|
| 1500 |
+
" (dropout): Dropout(p=0.15, inplace=False)\n",
|
| 1501 |
+
" (classifier): Linear(in_features=768, out_features=2, bias=True)\n",
|
| 1502 |
+
")"
|
| 1503 |
+
]
|
| 1504 |
+
},
|
| 1505 |
+
"execution_count": 70,
|
| 1506 |
+
"metadata": {},
|
| 1507 |
+
"output_type": "execute_result"
|
| 1508 |
+
}
|
| 1509 |
+
],
|
| 1510 |
+
"source": [
|
| 1511 |
+
"model = DistilBertForMaskedLM.from_pretrained(\"distilbert-base-uncased\")\n",
|
| 1512 |
+
"config = DistilBertConfig.from_pretrained(\"distilbert-base-uncased\")\n",
|
| 1513 |
+
"mod = model.distilbert\n",
|
| 1514 |
+
"\n",
|
| 1515 |
+
"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
|
| 1516 |
+
"model = ReuseQuestionDistilBERT(mod)\n",
|
| 1517 |
+
"model.to(device)"
|
| 1518 |
+
]
|
| 1519 |
+
},
|
| 1520 |
+
{
|
| 1521 |
+
"cell_type": "code",
|
| 1522 |
+
"execution_count": 71,
|
| 1523 |
+
"id": "d2c6bff5",
|
| 1524 |
+
"metadata": {},
|
| 1525 |
+
"outputs": [
|
| 1526 |
+
{
|
| 1527 |
+
"name": "stdout",
|
| 1528 |
+
"output_type": "stream",
|
| 1529 |
+
"text": [
|
| 1530 |
+
"+-------------------------------+------------+\n",
|
| 1531 |
+
"| Modules | Parameters |\n",
|
| 1532 |
+
"+-------------------------------+------------+\n",
|
| 1533 |
+
"| te.0.attention.q_lin.weight | 589824 |\n",
|
| 1534 |
+
"| te.0.attention.q_lin.bias | 768 |\n",
|
| 1535 |
+
"| te.0.attention.k_lin.weight | 589824 |\n",
|
| 1536 |
+
"| te.0.attention.k_lin.bias | 768 |\n",
|
| 1537 |
+
"| te.0.attention.v_lin.weight | 589824 |\n",
|
| 1538 |
+
"| te.0.attention.v_lin.bias | 768 |\n",
|
| 1539 |
+
"| te.0.attention.out_lin.weight | 589824 |\n",
|
| 1540 |
+
"| te.0.attention.out_lin.bias | 768 |\n",
|
| 1541 |
+
"| te.0.sa_layer_norm.weight | 768 |\n",
|
| 1542 |
+
"| te.0.sa_layer_norm.bias | 768 |\n",
|
| 1543 |
+
"| te.0.ffn.lin1.weight | 2359296 |\n",
|
| 1544 |
+
"| te.0.ffn.lin1.bias | 3072 |\n",
|
| 1545 |
+
"| te.0.ffn.lin2.weight | 2359296 |\n",
|
| 1546 |
+
"| te.0.ffn.lin2.bias | 768 |\n",
|
| 1547 |
+
"| te.0.output_layer_norm.weight | 768 |\n",
|
| 1548 |
+
"| te.0.output_layer_norm.bias | 768 |\n",
|
| 1549 |
+
"| te.1.attention.q_lin.weight | 589824 |\n",
|
| 1550 |
+
"| te.1.attention.q_lin.bias | 768 |\n",
|
| 1551 |
+
"| te.1.attention.k_lin.weight | 589824 |\n",
|
| 1552 |
+
"| te.1.attention.k_lin.bias | 768 |\n",
|
| 1553 |
+
"| te.1.attention.v_lin.weight | 589824 |\n",
|
| 1554 |
+
"| te.1.attention.v_lin.bias | 768 |\n",
|
| 1555 |
+
"| te.1.attention.out_lin.weight | 589824 |\n",
|
| 1556 |
+
"| te.1.attention.out_lin.bias | 768 |\n",
|
| 1557 |
+
"| te.1.sa_layer_norm.weight | 768 |\n",
|
| 1558 |
+
"| te.1.sa_layer_norm.bias | 768 |\n",
|
| 1559 |
+
"| te.1.ffn.lin1.weight | 2359296 |\n",
|
| 1560 |
+
"| te.1.ffn.lin1.bias | 3072 |\n",
|
| 1561 |
+
"| te.1.ffn.lin2.weight | 2359296 |\n",
|
| 1562 |
+
"| te.1.ffn.lin2.bias | 768 |\n",
|
| 1563 |
+
"| te.1.output_layer_norm.weight | 768 |\n",
|
| 1564 |
+
"| te.1.output_layer_norm.bias | 768 |\n",
|
| 1565 |
+
"| classifier.weight | 1536 |\n",
|
| 1566 |
+
"| classifier.bias | 2 |\n",
|
| 1567 |
+
"+-------------------------------+------------+\n",
|
| 1568 |
+
"Total Trainable Params: 14177282\n"
|
| 1569 |
+
]
|
| 1570 |
+
},
|
| 1571 |
+
{
|
| 1572 |
+
"data": {
|
| 1573 |
+
"text/plain": [
|
| 1574 |
+
"14177282"
|
| 1575 |
+
]
|
| 1576 |
+
},
|
| 1577 |
+
"execution_count": 71,
|
| 1578 |
+
"metadata": {},
|
| 1579 |
+
"output_type": "execute_result"
|
| 1580 |
+
}
|
| 1581 |
+
],
|
| 1582 |
+
"source": [
|
| 1583 |
+
"count_parameters(model)"
|
| 1584 |
+
]
|
| 1585 |
+
},
|
| 1586 |
+
{
|
| 1587 |
+
"cell_type": "markdown",
|
| 1588 |
+
"id": "c386c2eb",
|
| 1589 |
+
"metadata": {},
|
| 1590 |
+
"source": [
|
| 1591 |
+
"### Testing the Model"
|
| 1592 |
+
]
|
| 1593 |
+
},
|
| 1594 |
+
{
|
| 1595 |
+
"cell_type": "code",
|
| 1596 |
+
"execution_count": 72,
|
| 1597 |
+
"id": "818deed3",
|
| 1598 |
+
"metadata": {},
|
| 1599 |
+
"outputs": [],
|
| 1600 |
+
"source": [
|
| 1601 |
+
"# get smaller dataset\n",
|
| 1602 |
+
"batch_size = 8\n",
|
| 1603 |
+
"test_ds = Dataset(squad_paths = squad_paths[:2], natural_question_paths=None, hotpotqa_paths=None, tokenizer=tokenizer)\n",
|
| 1604 |
+
"test_ds_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size)\n",
|
| 1605 |
+
"optim=torch.optim.Adam(model.parameters())"
|
| 1606 |
+
]
|
| 1607 |
+
},
|
| 1608 |
+
{
|
| 1609 |
+
"cell_type": "code",
|
| 1610 |
+
"execution_count": 73,
|
| 1611 |
+
"id": "9da40760",
|
| 1612 |
+
"metadata": {},
|
| 1613 |
+
"outputs": [
|
| 1614 |
+
{
|
| 1615 |
+
"name": "stdout",
|
| 1616 |
+
"output_type": "stream",
|
| 1617 |
+
"text": [
|
| 1618 |
+
"Passed\n"
|
| 1619 |
+
]
|
| 1620 |
+
}
|
| 1621 |
+
],
|
| 1622 |
+
"source": [
|
| 1623 |
+
"test_model(model, optim, test_ds_loader, device)"
|
| 1624 |
+
]
|
| 1625 |
+
},
|
| 1626 |
+
{
|
| 1627 |
+
"cell_type": "markdown",
|
| 1628 |
+
"id": "c3f80248",
|
| 1629 |
+
"metadata": {},
|
| 1630 |
+
"source": [
|
| 1631 |
+
"### Model Training"
|
| 1632 |
+
]
|
| 1633 |
+
},
|
| 1634 |
+
{
|
| 1635 |
+
"cell_type": "code",
|
| 1636 |
+
"execution_count": 24,
|
| 1637 |
+
"id": "e1adabe6",
|
| 1638 |
+
"metadata": {},
|
| 1639 |
+
"outputs": [],
|
| 1640 |
+
"source": [
|
| 1641 |
+
"from torch.optim import AdamW, RMSprop\n",
|
| 1642 |
+
"\n",
|
| 1643 |
+
"model.train()\n",
|
| 1644 |
+
"optim = AdamW(model.parameters(), lr=1e-4)"
|
| 1645 |
+
]
|
| 1646 |
+
},
|
| 1647 |
+
{
|
| 1648 |
+
"cell_type": "code",
|
| 1649 |
+
"execution_count": null,
|
| 1650 |
+
"id": "efe1cbd5",
|
| 1651 |
+
"metadata": {},
|
| 1652 |
+
"outputs": [],
|
| 1653 |
+
"source": [
|
| 1654 |
+
"epochs = 16\n",
|
| 1655 |
+
"\n",
|
| 1656 |
+
"for epoch in range(epochs):\n",
|
| 1657 |
+
" loop = tqdm(loader, leave=True)\n",
|
| 1658 |
+
" model.train()\n",
|
| 1659 |
+
" mean_training_error = []\n",
|
| 1660 |
+
" for batch in loop:\n",
|
| 1661 |
+
" optim.zero_grad()\n",
|
| 1662 |
+
" \n",
|
| 1663 |
+
" input_ids = batch['input_ids'].to(device)\n",
|
| 1664 |
+
" attention_mask = batch['attention_mask'].to(device)\n",
|
| 1665 |
+
" start = batch['start_positions'].to(device)\n",
|
| 1666 |
+
" end = batch['end_positions'].to(device)\n",
|
| 1667 |
+
" \n",
|
| 1668 |
+
" outputs = model(input_ids, attention_mask=attention_mask, start_positions=start, end_positions=end)\n",
|
| 1669 |
+
" # print(torch.argmax(outputs['start_logits'],axis=1), torch.argmax(outputs['end_logits'], axis=1), start, end)\n",
|
| 1670 |
+
" loss = outputs['loss']\n",
|
| 1671 |
+
" loss.backward()\n",
|
| 1672 |
+
" # torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1)\n",
|
| 1673 |
+
" optim.step()\n",
|
| 1674 |
+
" mean_training_error.append(loss.item())\n",
|
| 1675 |
+
" loop.set_description(f'Epoch {epoch}')\n",
|
| 1676 |
+
" loop.set_postfix(loss=loss.item())\n",
|
| 1677 |
+
" print(\"Mean Training Error\", np.mean(mean_training_error))\n",
|
| 1678 |
+
" \n",
|
| 1679 |
+
" loop = tqdm(test_loader, leave=True)\n",
|
| 1680 |
+
" model.eval()\n",
|
| 1681 |
+
" mean_test_error = []\n",
|
| 1682 |
+
" for batch in loop:\n",
|
| 1683 |
+
" \n",
|
| 1684 |
+
" input_ids = batch['input_ids'].to(device)\n",
|
| 1685 |
+
" attention_mask = batch['attention_mask'].to(device)\n",
|
| 1686 |
+
" start = batch['start_positions'].to(device)\n",
|
| 1687 |
+
" end = batch['end_positions'].to(device)\n",
|
| 1688 |
+
" \n",
|
| 1689 |
+
" outputs = model(input_ids, attention_mask=attention_mask, start_positions=start, end_positions=end)\n",
|
| 1690 |
+
" # print(torch.argmax(outputs['start_logits'],axis=1), torch.argmax(outputs['end_logits'], axis=1), start, end)\n",
|
| 1691 |
+
" loss = outputs['loss']\n",
|
| 1692 |
+
" \n",
|
| 1693 |
+
" mean_test_error.append(loss.item())\n",
|
| 1694 |
+
" loop.set_description(f'Epoch {epoch} Testset')\n",
|
| 1695 |
+
" loop.set_postfix(loss=loss.item())\n",
|
| 1696 |
+
" print(\"Mean Test Error\", np.mean(mean_test_error))\n",
|
| 1697 |
+
" torch.save(model.state_dict(), \"distilbert_reuse_{}\".format(epoch))"
|
| 1698 |
+
]
|
| 1699 |
+
},
|
| 1700 |
+
{
|
| 1701 |
+
"cell_type": "code",
|
| 1702 |
+
"execution_count": 48,
|
| 1703 |
+
"id": "fdf37d18",
|
| 1704 |
+
"metadata": {},
|
| 1705 |
+
"outputs": [],
|
| 1706 |
+
"source": [
|
| 1707 |
+
"torch.save(model.state_dict(), \"distilbert_reuse.model\")"
|
| 1708 |
+
]
|
| 1709 |
+
},
|
| 1710 |
+
{
|
| 1711 |
+
"cell_type": "code",
|
| 1712 |
+
"execution_count": 49,
|
| 1713 |
+
"id": "d1cfded4",
|
| 1714 |
+
"metadata": {},
|
| 1715 |
+
"outputs": [],
|
| 1716 |
+
"source": [
|
| 1717 |
+
"m = ReuseQuestionDistilBERT(mod)\n",
|
| 1718 |
+
"m.load_state_dict(torch.load(\"distilbert_reuse.model\"))\n",
|
| 1719 |
+
"model = m"
|
| 1720 |
+
]
|
| 1721 |
+
},
|
| 1722 |
+
{
|
| 1723 |
+
"cell_type": "code",
|
| 1724 |
+
"execution_count": 47,
|
| 1725 |
+
"id": "233bdc18",
|
| 1726 |
+
"metadata": {},
|
| 1727 |
+
"outputs": [
|
| 1728 |
+
{
|
| 1729 |
+
"name": "stderr",
|
| 1730 |
+
"output_type": "stream",
|
| 1731 |
+
"text": [
|
| 1732 |
+
"100%|鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 2500/2500 [02:51<00:00, 14.59it/s]"
|
| 1733 |
+
]
|
| 1734 |
+
},
|
| 1735 |
+
{
|
| 1736 |
+
"name": "stdout",
|
| 1737 |
+
"output_type": "stream",
|
| 1738 |
+
"text": [
|
| 1739 |
+
"Mean EM: 0.5178\n",
|
| 1740 |
+
"Mean F-1: 0.6671140689626448\n"
|
| 1741 |
+
]
|
| 1742 |
+
},
|
| 1743 |
+
{
|
| 1744 |
+
"name": "stderr",
|
| 1745 |
+
"output_type": "stream",
|
| 1746 |
+
"text": [
|
| 1747 |
+
"\n"
|
| 1748 |
+
]
|
| 1749 |
+
}
|
| 1750 |
+
],
|
| 1751 |
+
"source": [
|
| 1752 |
+
"eval_test_set(model, tokenizer, test_loader, device)"
|
| 1753 |
+
]
|
| 1754 |
+
},
|
| 1755 |
+
{
|
| 1756 |
+
"cell_type": "code",
|
| 1757 |
+
"execution_count": null,
|
| 1758 |
+
"id": "0fb1ce9e",
|
| 1759 |
+
"metadata": {},
|
| 1760 |
+
"outputs": [],
|
| 1761 |
+
"source": []
|
| 1762 |
+
}
|
| 1763 |
+
],
|
| 1764 |
+
"metadata": {
|
| 1765 |
+
"kernelspec": {
|
| 1766 |
+
"display_name": "Python 3.10.8 ('venv': venv)",
|
| 1767 |
+
"language": "python",
|
| 1768 |
+
"name": "python3"
|
| 1769 |
+
},
|
| 1770 |
+
"language_info": {
|
| 1771 |
+
"codemirror_mode": {
|
| 1772 |
+
"name": "ipython",
|
| 1773 |
+
"version": 3
|
| 1774 |
+
},
|
| 1775 |
+
"file_extension": ".py",
|
| 1776 |
+
"mimetype": "text/x-python",
|
| 1777 |
+
"name": "python",
|
| 1778 |
+
"nbconvert_exporter": "python",
|
| 1779 |
+
"pygments_lexer": "ipython3",
|
| 1780 |
+
"version": "3.10.8"
|
| 1781 |
+
},
|
| 1782 |
+
"toc": {
|
| 1783 |
+
"base_numbering": 1,
|
| 1784 |
+
"nav_menu": {},
|
| 1785 |
+
"number_sections": true,
|
| 1786 |
+
"sideBar": true,
|
| 1787 |
+
"skip_h1_title": false,
|
| 1788 |
+
"title_cell": "Table of Contents",
|
| 1789 |
+
"title_sidebar": "Contents",
|
| 1790 |
+
"toc_cell": false,
|
| 1791 |
+
"toc_position": {},
|
| 1792 |
+
"toc_section_display": true,
|
| 1793 |
+
"toc_window_display": false
|
| 1794 |
+
},
|
| 1795 |
+
"varInspector": {
|
| 1796 |
+
"cols": {
|
| 1797 |
+
"lenName": 16,
|
| 1798 |
+
"lenType": 16,
|
| 1799 |
+
"lenVar": 40
|
| 1800 |
+
},
|
| 1801 |
+
"kernels_config": {
|
| 1802 |
+
"python": {
|
| 1803 |
+
"delete_cmd_postfix": "",
|
| 1804 |
+
"delete_cmd_prefix": "del ",
|
| 1805 |
+
"library": "var_list.py",
|
| 1806 |
+
"varRefreshCmd": "print(var_dic_list())"
|
| 1807 |
+
},
|
| 1808 |
+
"r": {
|
| 1809 |
+
"delete_cmd_postfix": ") ",
|
| 1810 |
+
"delete_cmd_prefix": "rm(",
|
| 1811 |
+
"library": "var_list.r",
|
| 1812 |
+
"varRefreshCmd": "cat(var_dic_list()) "
|
| 1813 |
+
}
|
| 1814 |
+
},
|
| 1815 |
+
"types_to_exclude": [
|
| 1816 |
+
"module",
|
| 1817 |
+
"function",
|
| 1818 |
+
"builtin_function_or_method",
|
| 1819 |
+
"instance",
|
| 1820 |
+
"_Feature"
|
| 1821 |
+
],
|
| 1822 |
+
"window_display": false
|
| 1823 |
+
},
|
| 1824 |
+
"vscode": {
|
| 1825 |
+
"interpreter": {
|
| 1826 |
+
"hash": "85bf9c14e9ba73b783ed1274d522bec79eb0b2b739090180d8ce17bb11aff4aa"
|
| 1827 |
+
}
|
| 1828 |
+
}
|
| 1829 |
+
},
|
| 1830 |
+
"nbformat": 4,
|
| 1831 |
+
"nbformat_minor": 5
|
| 1832 |
+
}
|