-
Notifications
You must be signed in to change notification settings - Fork 3
/
dpr.py
276 lines (206 loc) · 10.1 KB
/
dpr.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
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
import os
import json
import random
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
from pprint import pprint
import torch
from torch.utils.data import DataLoader, TensorDataset
import torch.nn.functional as F
from transformers import (
AutoTokenizer,
BertModel, BertPreTrainedModel,
AdamW, get_linear_schedule_with_warmup,
TrainingArguments,
)
def set_seed(random_seed):
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed) # if use multi-GPU
random.seed(random_seed)
np.random.seed(random_seed)
# Anwer
class DenseRetrieval:
def __init__(self, args, dataset, num_neg, tokenizer, p_encoder, q_encoder):
'''
학습과 추론에 사용될 여러 셋업을 마쳐봅시다.
'''
self.args = args
self.dataset = dataset
self.num_neg = num_neg
self.tokenizer = tokenizer
self.p_encoder = p_encoder
self.q_encoder = q_encoder
self.prepare_in_batch_negative(num_neg=num_neg)
def prepare_in_batch_negative(self, dataset=None, num_neg=2, tokenizer=None):
if dataset is None:
dataset = self.dataset
if tokenizer is None:
tokenizer = self.tokenizer
# 1. In-Batch-Negative 만들기
# CORPUS를 np.array로 변환해줍니다.
corpus = np.array(list(set([example for example in dataset['context']])))
p_with_neg = []
for c in tqdm(dataset['context'], desc='In-batch-Negative'):
while True:
neg_idxs = np.random.randint(len(corpus), size=num_neg)
if not c in corpus[neg_idxs]:
p_neg = corpus[neg_idxs]
p_with_neg.append(c)
p_with_neg.extend(p_neg)
break
# 2. (Question, Passage) 데이터셋 만들어주기
q_seqs = tokenizer(dataset['question'].tolist(), padding="max_length", truncation=True, return_tensors='pt')
p_seqs = tokenizer(p_with_neg, padding="max_length", truncation=True, return_tensors='pt')
max_len = p_seqs['input_ids'].size(-1)
p_seqs['input_ids'] = p_seqs['input_ids'].view(-1, num_neg+1, max_len)
p_seqs['attention_mask'] = p_seqs['attention_mask'].view(-1, num_neg+1, max_len)
p_seqs['token_type_ids'] = p_seqs['token_type_ids'].view(-1, num_neg+1, max_len)
train_dataset = TensorDataset(
p_seqs['input_ids'], p_seqs['attention_mask'], p_seqs['token_type_ids'],
q_seqs['input_ids'], q_seqs['attention_mask'], q_seqs['token_type_ids']
)
self.train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=self.args.per_device_train_batch_size)
valid_seqs = tokenizer(dataset['context'].tolist(), padding="max_length", truncation=True, return_tensors='pt')
passage_dataset = TensorDataset(
valid_seqs['input_ids'], valid_seqs['attention_mask'], valid_seqs['token_type_ids']
)
self.passage_dataloader = DataLoader(passage_dataset, batch_size=self.args.per_device_train_batch_size)
def train(self, args=None):
if args is None:
args = self.args
batch_size = args.per_device_train_batch_size
# Optimizer
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in self.p_encoder.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': args.weight_decay},
{'params': [p for n, p in self.p_encoder.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0},
{'params': [p for n, p in self.q_encoder.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': args.weight_decay},
{'params': [p for n, p in self.q_encoder.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
t_total = len(self.train_dataloader) // args.gradient_accumulation_steps * args.num_train_epochs
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=t_total)
# Start training!
global_step = 0
self.p_encoder.zero_grad()
self.q_encoder.zero_grad()
torch.cuda.empty_cache()
train_iterator = tqdm(range(int(args.num_train_epochs)), desc="Epoch")
# for _ in range(int(args.num_train_epochs)):
for _ in train_iterator:
with tqdm(self.train_dataloader, unit="batch") as tepoch:
for batch in tepoch:
p_encoder.train()
q_encoder.train()
targets = torch.zeros(batch_size).long() # positive example은 전부 첫 번째에 위치하므로
targets = targets.to(args.device)
p_inputs = {
'input_ids': batch[0].view(batch_size * (self.num_neg + 1), -1).to(args.device),
'attention_mask': batch[1].view(batch_size * (self.num_neg + 1), -1).to(args.device),
'token_type_ids': batch[2].view(batch_size * (self.num_neg + 1), -1).to(args.device)
}
q_inputs = {
'input_ids': batch[3].to(args.device),
'attention_mask': batch[4].to(args.device),
'token_type_ids': batch[5].to(args.device)
}
p_outputs = self.p_encoder(**p_inputs) # (batch_size*(num_neg+1), emb_dim)
q_outputs = self.q_encoder(**q_inputs) # (batch_size*, emb_dim)
# Calculate similarity score & loss
p_outputs = p_outputs.view(batch_size, self.num_neg + 1, -1)
q_outputs = q_outputs.view(batch_size, 1, -1)
sim_scores = torch.bmm(q_outputs, torch.transpose(p_outputs, 1, 2)).squeeze() #(batch_size, num_neg + 1)
sim_scores = sim_scores.view(batch_size, -1)
sim_scores = F.log_softmax(sim_scores, dim=1)
loss = F.nll_loss(sim_scores, targets)
tepoch.set_postfix(loss=f'{str(loss.item())}')
loss.backward()
optimizer.step()
scheduler.step()
self.p_encoder.zero_grad()
self.q_encoder.zero_grad()
global_step += 1
torch.cuda.empty_cache()
del p_inputs, q_inputs
def get_relevant_doc(self, query, k=1, args=None, p_encoder=None, q_encoder=None):
if args is None:
args = self.args
if p_encoder is None:
p_encoder = self.p_encoder
if q_encoder is None:
q_encoder = self.q_encoder
with torch.no_grad():
p_encoder.eval()
q_encoder.eval()
q_seqs_val = self.tokenizer([query], padding="max_length", truncation=True, return_tensors='pt').to(args.device)
q_emb = q_encoder(**q_seqs_val).to('cpu') # (num_query=1, emb_dim)
p_embs = []
for batch in self.passage_dataloader:
batch = tuple(t.to(args.device) for t in batch)
p_inputs = {
'input_ids': batch[0],
'attention_mask': batch[1],
'token_type_ids': batch[2]
}
p_emb = p_encoder(**p_inputs).to('cpu')
p_embs.append(p_emb)
p_embs = torch.stack(p_embs, dim=0).view(len(self.passage_dataloader.dataset), -1) # (num_passage, emb_dim)
dot_prod_scores = torch.matmul(q_emb, torch.transpose(p_embs, 0, 1))
rank = torch.argsort(dot_prod_scores, dim=1, descending=True).squeeze()
return rank[:k]
class BertEncoder(BertPreTrainedModel):
def __init__(self, config):
super(BertEncoder, self).__init__(config)
self.bert = BertModel(config)
self.init_weights()
def forward(
self,
input_ids,
attention_mask=None,
token_type_ids=None
):
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids
)
pooled_output = outputs[1]
return pooled_output
if __name__ == "__main__":
set_seed(42)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# 데이터셋과 모델은 아래와 같이 불러옵니다.
data_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "csv_data")
train_dataset = pd.read_csv(os.path.join(data_path, "squad_kor_v1.csv"))
train_dataset = train_dataset[:11]
# 메모리가 부족한 경우 일부만 사용하세요 !
# num_sample = 1500
# sample_idx = np.random.choice(range(len(train_dataset)), num_sample)
# train_dataset = train_dataset[sample_idx]
args = TrainingArguments(
output_dir=os.path.join(os.path.abspath(os.path.dirname(__file__)), "dense_retireval"),
evaluation_strategy="epoch",
save_strategy="epoch",
learning_rate=3e-4,
per_device_train_batch_size=2,
per_device_eval_batch_size=2,
num_train_epochs=2,
weight_decay=0.01
)
model_checkpoint = 'klue/bert-base'
# model_checkpoint = "klue/roberta-large"
###
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
p_encoder = BertEncoder.from_pretrained(model_checkpoint).to(args.device)
q_encoder = BertEncoder.from_pretrained(model_checkpoint).to(args.device)
retriever = DenseRetrieval(args=args, dataset=train_dataset, num_neg=2, tokenizer=tokenizer, p_encoder=p_encoder, q_encoder=q_encoder)
retriever.train()
# query = '제주도 시청의 주소는 뭐야?'
# results = retriever.get_relevant_doc(query=query, k=5)
# print(f"[Search Query] {query}\n")
# indices = results.tolist()
# for i, idx in enumerate(indices):
# print(f"Top-{i + 1}th Passage (Index {idx})")
# pprint(retriever.dataset['context'][idx])