-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_original.py
229 lines (175 loc) · 7.55 KB
/
main_original.py
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
import torch
from torch.optim import Adam
from torch.utils.data import DataLoader
import transformers
from kobert_tokenizer import KoBERTTokenizer
from tqdm import tqdm
import os
import numpy as np
import wandb
import json
from torch.optim.lr_scheduler import ReduceLROnPlateau
from models import RetrieverEncoder
from datasets import RetrieverDataset, KorQuadSampler, korquad_collator
# os.environ['CUDA_LAUNCH_BLOCKING'] = "0, 1"
# os.environ["CUDA_VISIBLE_DEVICES"] = "1"
model_name = "retriever"
save_path = "result/0922_labelsmoothing"
gpu = "cuda:2" ##########
device = torch.device(gpu)
batch_size = 96 ############
lr =1e-5 ############
eps = 1e-8
epoch = 130
valid_every = 979 ##########
seed = 42
torch.manual_seed(seed)
#######################################################################################################
#Data
train_data_path = "/home/nlplab/hdd1/yoo/KorDPR/dataset/KorQuAD_v1.0_train_processed.p"
val_data_path = "/home/nlplab/hdd1/yoo/KorDPR/dataset/KorQuAD_v1.0_dev_processed.p"
train_dataset = RetrieverDataset(train_data_path)
val_dataset =RetrieverDataset(val_data_path)
train_dataloader = DataLoader(
dataset=train_dataset.dataset, # 93926개의 (question-id-passage-answer)
batch_sampler=KorQuadSampler(train_dataset.dataset, batch_size=batch_size, drop_last=False),
collate_fn=lambda x: korquad_collator(x, padding_value=train_dataset.pad_token_id), #x: batch
#num_workers=4,
)
val_dataloader = DataLoader(
dataset=val_dataset.dataset, # 9927개의 (question-id-passage-answer)
batch_sampler=KorQuadSampler(val_dataset.dataset, batch_size=batch_size, drop_last=False),
collate_fn=lambda x: korquad_collator(x, padding_value=val_dataset.pad_token_id), #x: batch
#num_workers=4,
)
tokenizer = KoBERTTokenizer.from_pretrained("skt/kobert-base-v1")
#######################################################################################################
#Loss/Acc
class LabelSmoothingCrossEntropy(torch.nn.Module):
def __init__(self):
super(LabelSmoothingCrossEntropy, self).__init__()
def forward(self, x, target, smoothing=0.1):
confidence=1.-smoothing
logprobs=torch.nn.functional.log_softmax(x,dim=-1)
nll_loss=-logprobs.gather(dim=-1, index=target.unsqueeze(1))
nll_loss=nll_loss.squeeze(1)
smooth_loss=-logprobs.mean(dim=-1)
loss=confidence*nll_loss+smoothing*smooth_loss
return loss.mean()
ls_loss=LabelSmoothingCrossEntropy()
def ibn_loss(pred):
batch_size = pred.size(0)
#print(pred) # torch.Size([96, 96])
target = torch.arange(batch_size).to(device)
#print(target) # torch.Size([96])
return ls_loss(pred,target,0.1)
#return torch.nn.functional.cross_entropy(pred, target) ##### to(device)?
def batch_acc(pred):
batch_size = pred.size(0)
target = torch.arange(batch_size)
return (pred.detach().cpu().max(1).indices == target).sum().float() / batch_size
#######################################################################################################
#model,optimizer,scheduler
model = RetrieverEncoder()
#model.load_state_dict(torch.load("/home/nlplab/hdd1/yoo/KorDPR_retriever/result/0201_base/57.model",map_location='cuda:0'))
model.to(device)
optimizer = Adam(model.parameters(), lr=lr, eps=eps)
#scheduler = transformers.get_linear_schedule_with_warmup(optimizer, 1000,10000) ##########
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=5, verbose=1)
os.makedirs(save_path, exist_ok=True)
#######################################################################################################
#wandb
# wandb.init(
# project="Kor Retriever",
# config={
# "batch_size": batch_size,
# "lr": lr,
# "eps": eps,
# "num_warmup_steps": 1000, ##########
# "num_training_steps": 100000, ##########
# "valid_every": 30, ##########
# },
# )
#######################################################################################################
#train
def train():
global_step_cnt = 0
prev_best = None
for e in range(epoch):
for step, batch in enumerate(tqdm(train_dataloader, desc="Epoch {}".format(str(e)))):
global_step_cnt += 1
model.train()
optimizer.zero_grad()
######################################################################
q, q_mask, _, p, p_mask, _,_ = batch
q, q_mask, p, p_mask = (
q.to(device),
q_mask.to(device),
p.to(device),
p_mask.to(device),
)
######################################################################
q_emb= model(q, q_mask, "query")
p_emb = model(p, p_mask, "passage")
pred = torch.matmul(q_emb, p_emb.T) # bs*bs
loss =ibn_loss(pred)
acc = batch_acc(pred)
######################################################################
loss.backward()
optimizer.step()
#scheduler.step()
######################################################################
log = {
"epoch": e,
"step": step,
"global_step": global_step_cnt,
"train_step_loss": loss.cpu().item(),
"current_lr": lr, #float(scheduler.get_last_lr()[0]),
"step_acc": acc,
}
if global_step_cnt % valid_every == 0:
val_dict = validation()
#log.update(val_dict)
print(val_dict)
if (prev_best is None or val_dict["val_loss"] < prev_best):
torch.save(model.state_dict(), os.path.join(save_path, '{}.model'.format(e)))
scheduler.step(val_dict["val_loss"])
print("best epoch:",e)
print("best epoch:",e)
print("best epoch:",e)
#wandb.log(log)
print(log)
#######################################################################################################
#validation
def validation():
model.eval()
loss_list = []
sample_cnt = 0
val_acc = 0
with torch.no_grad():
for batch in val_dataloader:
q, q_mask, _, p, p_mask,_,_ = batch
q, q_mask, p, p_mask = (
q.to(device),
q_mask.to(device),
p.to(device),
p_mask.to(device),
)
######################################################################
q_emb = model(q, q_mask, "query")
p_emb = model(p, p_mask, "passage")
pred = torch.matmul(q_emb, p_emb.T)
loss =ibn_loss(pred)
step_acc = batch_acc(pred)
######################################################################
batch_size = q.size(0)
sample_cnt += batch_size
val_acc += step_acc * batch_size
loss_list.append(loss.cpu().item() * batch_size)
return {
"val_loss": np.array(loss_list).sum() / float(sample_cnt),
"val_acc": val_acc / float(sample_cnt),
}
#######################################################################################################
#main
train()