-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
427 lines (332 loc) · 14.7 KB
/
train.py
File metadata and controls
427 lines (332 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import os
import random
import math
import csv
import json
from statistics import mean
from typing import List, Tuple, Dict, Any
import uuid
import pickle
from tqdm import tqdm
from easydict import EasyDict as edict
import numpy as np
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import wandb
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence
# from datasets import load_dataset, test_load_model
import torch.nn.functional as F
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, AutoModel, AutoConfig
from model import load_model
from cal_distance import calc_distance, val_calc
from test import test_model
#-------------------------Wandb----------------------
args = edict({'w_project': 'project_2', #goorm_project_2
'w_entity': 'goorm_team_1',
'learning_rate': 6e-5,
'batch_size': {'train': 1024,
'eval': 128,
'test': 256},
'accumulate': 16,
'epochs': 1,
'seed': 42,
'model_name': 'monologg/kobigbird-bert-base',
'max_length': 1024,
"data": "1,2"})
wandb.login()
wandb.init(project = args.w_project, entity = args.w_entity)
args['NAME'] = ''f'kobigbird_v2_ep{args.epochs}_max{args.max_length}_lr{args.learning_rate}_data{args.data}_{random.randrange(0, 1024)}'
wandb.run.name = args.NAME
wandb.config.learning_rate = args.learning_rate
wandb.config.epochs = args.epochs
wandb.config.batch_size = args.batch_size
#-----------------------------Model-------------------------
tokenizer, model = load_model(args.model_name)
model.cuda()
optimizer = torch.optim.AdamW(model.parameters(), lr=args.learning_rate)
#------------------------------dataset---------------------------------
class KoMRC:
def __init__(self, data, indices: List[Tuple[int, int, int]]):
self._data = data
self._indices = indices
# Json을 불러오는 메소드
@classmethod
def load(cls, file_path: str):
with open(file_path, 'r', encoding='utf-8') as fd:
data = json.load(fd)
indices = []
for d_id, document in enumerate(data['data']):
for p_id, paragraph in enumerate(document['paragraphs']):
for q_id, _ in enumerate(paragraph['qas']):
indices.append((d_id, p_id, q_id))
return cls(data, indices)
# Json을 불러오는 메소드
@classmethod
def loads(cls, *file_path: str):
datas = {'data': []}
indices = []
for f in file_path:
with open(f, 'r', encoding='utf-8') as fd:
data = json.load(fd)
datas['data'] += data['data']
for d_id, document in enumerate(datas['data']):
for p_id, paragraph in enumerate(document['paragraphs']):
for q_id, _ in enumerate(paragraph['qas']):
indices.append((d_id, p_id, q_id))
return cls(datas, indices)
# 데이터 셋을 잘라내는 메소드
@classmethod
def split(cls, dataset, eval_ratio: float=.1):
indices = list(dataset._indices)
random.shuffle(indices)
train_indices = indices[int(len(indices) * eval_ratio):]
eval_indices = indices[:int(len(indices) * eval_ratio)]
return cls(dataset._data, train_indices), cls(dataset._data, eval_indices)
def __getitem__(self, index: int) -> Dict[str, Any]:
d_id, p_id, q_id = self._indices[index]
paragraph = self._data['data'][d_id]['paragraphs'][p_id]
qa = paragraph['qas'][q_id]
if 'guid' in qa:
guid = qa['guid']
else:
guid = uuid.uuid4().hex
context = paragraph['context'].replace('\n', 'n').replace('\xad', ' ').replace('\xa0', ' ').replace('\u200b', ' ')
question = qa['question'].replace('\n', 'n').replace('\xad', ' ').replace('\xa0', ' ').replace('\u200b', ' ')
answers = qa['answers']
if answers != None:
for a in answers:
a['text'] = a['text'].replace('\n', 'n').replace('\xad', ' ').replace('\xa0', ' ').replace('\u200b', ' ')
else:
answers = None
return {'guid': guid,
'context': context,
'question': question,
'answers': answers
}
def __len__(self) -> int:
return len(self._indices)
class TokenizedKoMRC(KoMRC):
def __init__(self, data, indices: List[Tuple[int, int, int]]) -> None:
super().__init__(data, indices)
self._tokenizer = tokenizer
def _tokenize_with_position(self, sentence: str) -> List[Tuple[str, Tuple[int, int]]]:
position = 0
tokens = []
sentence_tokens = []
for word in sentence.split():
if '[UNK]' in tokenizer.tokenize(word):
sentence_tokens.append(word)
else:
sentence_tokens += tokenizer.tokenize(word)
for morph in sentence_tokens:
if len(morph) > 2:
if morph[:2] == '##':
morph = morph[2:]
position = sentence.find(morph, position)
tokens.append((morph, (position, position + len(morph))))
position += len(morph)
return tokens
def __getitem__(self, index: int) -> Dict[str, Any]:
sample = super().__getitem__(index)
# sample = {'guid': guid, 'context': context, 'question': question, 'answers': answers}
context, position = zip(*self._tokenize_with_position(sample['context']))
context, position = list(context), list(position)
question = self._tokenizer.tokenize(sample['question'])
if sample['answers'] is not None:
answers = []
for answer in sample['answers']:
for start, (position_start, position_end) in enumerate(position):
if position_start <= answer['answer_start'] < position_end:
break
else:
print(context, answer)
# print(answer['guid'])
print(answer['answer_start'])
raise ValueError("No mathced start position")
target = ''.join(answer['text'].split(' '))
source = ''
for end, morph in enumerate(context[start:], start):
source += morph
if target in source:
break
else:
print(context, answer)
#print(answer['guid'])
print(answer['answer_start'])
raise ValueError("No Matched end position")
answers.append({'start': start, 'end': end})
answer_text = sample['answers'][0]['text']
else:
answers = None
answer_text = None
return {
'guid': sample['guid'],
'context_original': sample['context'],
'context_position': position,
'question_original': sample['question'],
'context': context,
'question': question,
'answers': answers,
'answers_text': answer_text
}
class Indexer:
def __init__(self, vocabs: List[str], max_length: int=args.max_length):
self.max_length = max_length
self.vocabs = vocabs
@property
def vocab_size(self):
return len(self.vocabs)
@property
def pad_id(self):
return tokenizer.vocab['[PAD]']
@property
def unk_id(self):
return tokenizer.vocab['[UNK]']
@property
def cls_id(self):
return tokenizer.vocab['[CLS]']
@property
def sep_id(self):
return tokenizer.vocab['[SEP]']
def sample2ids(self, sample: Dict[str, Any],) -> Dict[str, Any]:
context = [tokenizer.convert_tokens_to_ids(token) for token in sample['context']]
question = [tokenizer.convert_tokens_to_ids(token) for token in sample['question']]
context = context[:self.max_length-len(question)-3] # Truncate context
input_ids = [self.cls_id] + question + [self.sep_id] + context + [self.sep_id]
token_type_ids = [0] * (len(question) + 1) + [1] * (len(context) + 2)
if sample['answers'] is not None:
answer = sample['answers'][0]
start = min(len(question) + 2 + answer['start'], self.max_length - 1)
end = min(len(question) + 2 + answer['end'], self.max_length - 1)
else:
start = None
end = None
return {
'guid': sample['guid'],
'context': sample['context_original'],
'question': sample['question_original'],
'position': sample['context_position'],
'input_ids': input_ids,
'token_type_ids': token_type_ids,
'start': start,
'end': end
}
class IndexerWrappedDataset:
def __init__(self, dataset: TokenizedKoMRC, indexer: Indexer) -> None:
self._dataset = dataset
self._indexer = indexer
def __len__(self) -> int:
return len(self._dataset)
def __getitem__(self, index: int) -> Dict[str, Any]:
sample = self._indexer.sample2ids(self._dataset[index])
sample['attention_mask'] = [1] * len(sample['input_ids'])
return sample
class Collator:
def __init__(self, indexer: Indexer) -> None:
self._indexer = indexer
def __call__(self, samples: List[Dict[str, Any]]) -> Dict[str, torch.Tensor]:
samples = {key: [sample[key] for sample in samples] for key in samples[0]}
for key in 'start', 'end':
if samples[key][0] is None:
samples[key] = None
else:
samples[key] = torch.tensor(samples[key], dtype=torch.long)
for key in 'input_ids', 'attention_mask', 'token_type_ids':
samples[key] = pad_sequence([torch.tensor(sample, dtype=torch.long) for sample in samples[key]],
batch_first=True,
padding_value=self._indexer.pad_id)
return samples
dataset = TokenizedKoMRC.loads('/data/jw/goorm_project2/Data/train.json',
'/data/jw/goorm_project2/Data/ko_nia_normal_squad_all.json')
# "/data/jw/Data/ko_wiki_v1_squad.json")
train_dataset, dev_dataset = TokenizedKoMRC.split(dataset)
# dev_answers = [dev_dataset[i]['answers_text'] for i in range(len(dev_dataset))]
# with open(f'/data/jw/{args.NAME}.pkl', 'wb') as f:
# pickle.dump(dev_answers, f)
indexer = Indexer(list(tokenizer.vocab.keys()))
indexed_train_dataset = IndexerWrappedDataset(train_dataset, indexer)
indexed_dev_dataset = IndexerWrappedDataset(dev_dataset, indexer)
#------------------------------fixed seed-------------------------------
def seed_everything(seed):
random.seed(seed)
np.random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed) # type: ignore
torch.backends.cudnn.deterministic = True # type: ignore
torch.backends.cudnn.benchmark = True # type: ignore
seed_everything(args.seed)
#--------------------------DataLoader---------------------------
collator = Collator(indexer)
train_loader = DataLoader(indexed_train_dataset,
batch_size = args.batch_size.train // args.accumulate,
shuffle = True,
collate_fn = collator,
num_workers = 2)
dev_loader = DataLoader(indexed_dev_dataset,
batch_size = args.batch_size.eval,
shuffle = False,
collate_fn = collator,
num_workers = 2)
#------------------------------train----------------------------------------
train_losses = []
dev_losses = []
train_loss = []
dev_loss = []
loss_accumulate = 0.
best_model = [-1, int(1e9)]
for epoch in range(args.epochs):
print("Epoch", epoch, '========================================================================================================')
# Train
progress_bar_train = tqdm(train_loader, desc='Train')
for i, batch in enumerate(progress_bar_train, 1):
del batch['guid'], batch['context'], batch['question'], batch['position']
batch = {key: value.cuda() for key, value in batch.items()}
start = batch.pop('start')
end = batch.pop('end')
output = model(**batch)
start_logits, end_logits = output.start_logits, output.end_logits
loss = (F.cross_entropy(start_logits, start) + F.cross_entropy(end_logits, end)) / args.accumulate
loss.backward()
loss_accumulate += loss.item()
del batch, start, end, start_logits, end_logits, loss
if i % args.accumulate == 0:
optimizer.step()
optimizer.zero_grad(set_to_none=False)
train_loss.append(loss_accumulate)
progress_bar_train.set_description(f"Train - Loss: {loss_accumulate:.3f}")
loss_accumulate = 0.
else:
continue
if i % int(len(train_loader) / (args.accumulate * 10)) == 0:
# Evaluation
for batch in tqdm(dev_loader, desc='eval'):
del batch['guid'], batch['context'], batch['question'], batch['position']
batch = {key: value.cuda() for key, value in batch.items()}
start = batch.pop('start')
end = batch.pop('end')
model.eval()
with torch.no_grad():
output = model(**batch)
start_logits = output.start_logits
end_logits = output.end_logits
model.train()
loss = F.cross_entropy(start_logits, start) + F.cross_entropy(end_logits, end)
dev_loss.append(loss.item())
del batch, start, end, start_logits, end_logits, loss
train_losses.append(mean(train_loss))
dev_losses.append(mean(dev_loss))
train_loss = []
dev_loss = []
if dev_losses[-1] <= best_model[1]:
best_model = (epoch, dev_losses[-1])
model.save_pretrained(f'/data/jw/goorm_project2/model/{args.NAME}_{epoch}')
wandb.log({"train_loss": train_losses[-1],
"valid_loss": dev_losses[-1]})
print(f"Train Loss: {train_losses[-1]:.3f}")
print(f"Valid Loss: {dev_losses[-1]:.3f}")
print('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
wandb.finish()