forked from wuch15/PLM4NewsRec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
381 lines (311 loc) · 13.1 KB
/
run.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
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
import numpy as np
import torch
import pickle
import hashlib
import logging
from tqdm.auto import tqdm
import torch.optim as optim
from pathlib import Path
import utils
import os
import sys
import logging
from dataloader import DataLoaderTrain, DataLoaderTest
from torch.utils.data import Dataset, DataLoader
from preprocess import read_news, read_news_bert, get_doc_input, get_doc_input_bert
from model_bert import ModelBert
from parameters import parse_args
from transformers import AutoTokenizer, AutoModel, AutoConfig
finetuneset={
'encoder.layer.6.attention.self.query.weight',
'encoder.layer.6.attention.self.query.bias',
'encoder.layer.6.attention.self.key.weight',
'encoder.layer.6.attention.self.key.bias',
'encoder.layer.6.attention.self.value.weight',
'encoder.layer.6.attention.self.value.bias',
'encoder.layer.6.attention.output.dense.weight',
'encoder.layer.6.attention.output.dense.bias',
'encoder.layer.6.attention.output.LayerNorm.weight',
'encoder.layer.6.attention.output.LayerNorm.bias',
'encoder.layer.6.intermediate.dense.weight',
'encoder.layer.6.intermediate.dense.bias',
'encoder.layer.6.output.dense.weight',
'encoder.layer.6.output.dense.bias',
'encoder.layer.6.output.LayerNorm.weight',
'encoder.layer.6.output.LayerNorm.bias',
'encoder.layer.7.attention.self.query.weight',
'encoder.layer.7.attention.self.query.bias',
'encoder.layer.7.attention.self.key.weight',
'encoder.layer.7.attention.self.key.bias',
'encoder.layer.7.attention.self.value.weight',
'encoder.layer.7.attention.self.value.bias',
'encoder.layer.7.attention.output.dense.weight',
'encoder.layer.7.attention.output.dense.bias',
'encoder.layer.7.attention.output.LayerNorm.weight',
'encoder.layer.7.attention.output.LayerNorm.bias',
'encoder.layer.7.intermediate.dense.weight',
'encoder.layer.7.intermediate.dense.bias',
'encoder.layer.7.output.dense.weight',
'encoder.layer.7.output.dense.bias',
'encoder.layer.7.output.LayerNorm.weight',
'encoder.layer.7.output.LayerNorm.bias',
'pooler.dense.weight',
'pooler.dense.bias',
'rel_pos_bias.weight',
'classifier.weight',
'classifier.bias'}
def train(args):
# Only support title Turing now
assert args.enable_hvd # TODO
if args.enable_hvd:
import horovod.torch as hvd
if args.load_ckpt_name is not None:
#TODO: choose ckpt_path
ckpt_path = utils.get_checkpoint(args.model_dir, args.load_ckpt_name)
else:
ckpt_path = utils.latest_checkpoint(args.model_dir)
hvd_size, hvd_rank, hvd_local_rank = utils.init_hvd_cuda(
args.enable_hvd, args.enable_gpu)
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
config = AutoConfig.from_pretrained("bert-base-uncased", output_hidden_states=True)
config.num_hidden_layers = 8
bert_model = AutoModel.from_pretrained("bert-base-uncased",config=config)
#bert_model.load_state_dict(torch.load('../bert_encoder_part.pkl'))
# freeze parameters
for name,param in bert_model.named_parameters():
if name not in finetuneset:
param.requires_grad = False
news, news_index, category_dict, domain_dict, subcategory_dict = read_news_bert(
os.path.join(args.root_data_dir,
f'{args.dataset}/{args.train_dir}/news.tsv'),
args,
tokenizer
)
news_title, news_title_type, news_title_attmask, \
news_abstract, news_abstract_type, news_abstract_attmask, \
news_body, news_body_type, news_body_attmask, \
news_category, news_domain, news_subcategory = get_doc_input_bert(
news, news_index, category_dict, domain_dict, subcategory_dict, args)
news_combined = np.concatenate([
x for x in
[news_title, news_title_type, news_title_attmask, \
news_abstract, news_abstract_type, news_abstract_attmask, \
news_body, news_body_type, news_body_attmask, \
news_category, news_domain, news_subcategory]
if x is not None], axis=1)
model = ModelBert(args, bert_model, len(category_dict), len(domain_dict), len(subcategory_dict))
word_dict = None
if args.enable_gpu:
model = model.cuda()
lr_scaler = hvd.local_size()
optimizer = optim.Adam(model.parameters(), lr=args.lr)
if args.enable_hvd:
hvd.broadcast_parameters(model.state_dict(), root_rank=0)
hvd.broadcast_optimizer_state(optimizer, root_rank=0)
compression = hvd.Compression.none
optimizer = hvd.DistributedOptimizer(
optimizer,
named_parameters=model.named_parameters(),
compression=compression,
op=hvd.Average)
dataloader = DataLoaderTrain(
news_index=news_index,
news_combined=news_combined,
word_dict=word_dict,
data_dir=os.path.join(args.root_data_dir,
f'{args.market}/{args.train_dir}'),
filename_pat=args.filename_pat,
args=args,
world_size=hvd_size,
worker_rank=hvd_rank,
cuda_device_idx=hvd_local_rank,
enable_prefetch=True,
enable_shuffle=True,
enable_gpu=args.enable_gpu,
)
logging.info('Training...')
for ep in range(args.epochs):
loss = 0.0
accuary = 0.0
for cnt, (log_ids, log_mask, input_ids, targets) in enumerate(dataloader):
if cnt > args.max_steps_per_epoch:
break
if args.enable_gpu:
log_ids = log_ids.cuda(non_blocking=True)
log_mask = log_mask.cuda(non_blocking=True)
input_ids = input_ids.cuda(non_blocking=True)
targets = targets.cuda(non_blocking=True)
bz_loss, y_hat = model(input_ids, log_ids, log_mask, targets)
loss += bz_loss.data.float()
accuary += utils.acc(targets, y_hat)
optimizer.zero_grad()
bz_loss.backward()
optimizer.step()
if cnt % args.log_steps == 0:
logging.info(
'[{}] Ed: {}, train_loss: {:.5f}, acc: {:.5f}'.format(
hvd_rank, cnt * args.batch_size, loss.data / cnt,
accuary / cnt))
# save model minibatch
print(hvd_rank,cnt,args.save_steps,cnt%args.save_steps)
if hvd_rank == 0 and cnt % args.save_steps == 0:
ckpt_path = os.path.join(args.model_dir, f'epoch-{ep+1}-{cnt}.pt')
torch.save(
{
'model_state_dict': model.state_dict(),
'category_dict': category_dict,
'word_dict': word_dict,
'domain_dict': domain_dict,
'subcategory_dict': subcategory_dict
}, ckpt_path)
logging.info(f"Model saved to {ckpt_path}")
loss /= cnt
print(ep + 1, loss)
# save model last of epoch
if hvd_rank == 0:
ckpt_path = os.path.join(args.model_dir, f'epoch-{ep+1}.pt')
torch.save(
{
'model_state_dict': model.state_dict(),
'category_dict': category_dict,
'word_dict': word_dict,
'domain_dict': domain_dict,
'subcategory_dict': subcategory_dict
}, ckpt_path)
logging.info(f"Model saved to {ckpt_path}")
dataloader.join()
def test(args):
if args.enable_hvd:
import horovod.torch as hvd
hvd_size, hvd_rank, hvd_local_rank = utils.init_hvd_cuda(
args.enable_hvd, args.enable_gpu)
if args.load_ckpt_name is not None:
#TODO: choose ckpt_path
ckpt_path = utils.get_checkpoint(args.model_dir, args.load_ckpt_name)
else:
ckpt_path = utils.latest_checkpoint(args.model_dir)
assert ckpt_path is not None, 'No ckpt found'
checkpoint = torch.load(ckpt_path)
if 'subcategory_dict' in checkpoint:
subcategory_dict = checkpoint['subcategory_dict']
else:
subcategory_dict = {}
category_dict = checkpoint['category_dict']
word_dict = checkpoint['word_dict']
domain_dict = checkpoint['domain_dict']
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
config = AutoConfig.from_pretrained("bert-base-uncased", output_hidden_states=True)
bert_model = AutoModel.from_pretrained("bert-base-uncased",config=config)
model = ModelBert(args, bert_model, len(category_dict), len(domain_dict), len(subcategory_dict))
if args.enable_gpu:
model.cuda()
model.load_state_dict(checkpoint['model_state_dict'])
logging.info(f"Model loaded from {ckpt_path}")
if args.enable_hvd:
hvd.broadcast_parameters(model.state_dict(), root_rank=0)
model.eval()
torch.set_grad_enabled(False)
news, news_index, category_dict, domain_dict, subcategory_dict = read_news_bert(
os.path.join(args.root_data_dir,
f'{args.market}/{args.test_dir}/news.tsv'),
args,
tokenizer
)
news_title, news_title_type, news_title_attmask, \
news_abstract, news_abstract_type, news_abstract_attmask, \
news_body, news_body_type, news_body_attmask, \
news_category, news_domain, news_subcategory = get_doc_input_bert(
news, news_index, category_dict, domain_dict, subcategory_dict, args)
news_combined = np.concatenate([
x for x in
[news_title, news_title_type, news_title_attmask, \
news_abstract, news_abstract_type, news_abstract_attmask, \
news_body, news_body_type, news_body_attmask, \
news_category, news_domain, news_subcategory]
if x is not None], axis=1)
class NewsDataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, idx):
return self.data[idx]
def __len__(self):
return self.data.shape[0]
def news_collate_fn(arr):
arr = torch.LongTensor(arr)
return arr
news_dataset = NewsDataset(news_combined)
news_dataloader = DataLoader(news_dataset,
batch_size=args.batch_size * 4,
num_workers=args.num_workers,
collate_fn=news_collate_fn)
news_scoring = []
with torch.no_grad():
for input_ids in tqdm(news_dataloader):
input_ids = input_ids.cuda()
news_vec = model.news_encoder(input_ids)
news_vec = news_vec.to(torch.device("cpu")).detach().numpy()
news_scoring.extend(news_vec)
news_scoring = np.array(news_scoring)
logging.info("news scoring num: {}".format(news_scoring.shape[0]))
dataloader = DataLoaderTest(
news_index=news_index,
news_scoring=news_scoring,
word_dict=word_dict,
news_bias_scoring= None,
data_dir=os.path.join(args.root_data_dir,
f'{args.market}/{args.test_dir}'),
filename_pat=args.filename_pat,
args=args,
world_size=hvd_size,
worker_rank=hvd_rank,
cuda_device_idx=hvd_local_rank,
enable_prefetch=True,
enable_shuffle=False,
enable_gpu=args.enable_gpu,
)
from metrics import roc_auc_score, ndcg_score, mrr_score, ctr_score
AUC = []
MRR = []
nDCG5 = []
nDCG10 = []
def print_metrics(hvd_local_rank, cnt, x):
logging.info("[{}] Ed: {}: {}".format(hvd_local_rank, cnt, \
'\t'.join(["{:0.2f}".format(i * 100) for i in x])))
def get_mean(arr):
return [np.array(i).mean() for i in arr]
#for cnt, (log_vecs, log_mask, news_vecs, news_bias, labels) in enumerate(dataloader):
for cnt, (log_vecs, log_mask, news_vecs, news_bias, labels) in enumerate(dataloader):
his_lens = torch.sum(log_mask, dim=-1).to(torch.device("cpu")).detach().numpy()
if args.enable_gpu:
log_vecs = log_vecs.cuda(non_blocking=True)
log_mask = log_mask.cuda(non_blocking=True)
user_vecs = model.user_encoder(log_vecs, log_mask).to(torch.device("cpu")).detach().numpy()
for index, user_vec, news_vec, bias, label, his_len in zip(
range(len(labels)), user_vecs, news_vecs, news_bias, labels, his_lens):
if label.mean() == 0 or label.mean() == 1:
continue
score = np.dot(
news_vec, user_vec
)
auc = roc_auc_score(label, score)
mrr = mrr_score(label, score)
ndcg5 = ndcg_score(label, score, k=5)
ndcg10 = ndcg_score(label, score, k=10)
AUC.append(auc)
MRR.append(mrr)
nDCG5.append(ndcg5)
nDCG10.append(ndcg10)
if cnt % args.log_steps == 0:
print_metrics(hvd_rank, cnt * args.batch_size, get_mean([AUC, MRR, nDCG5, nDCG10]))
# stop scoring
dataloader.join()
for i in range(2):
print_metrics(hvd_rank, cnt * args.batch_size, get_mean([AUC, MRR, nDCG5, nDCG10]))
if __name__ == "__main__":
utils.setuplogger()
args = parse_args()
Path(args.model_dir).mkdir(parents=True, exist_ok=True)
if 'train' in args.mode:
train(args)
if 'test' in args.mode:
test(args)