-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
176 lines (158 loc) · 6.54 KB
/
train.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
import numpy as np
import pandas as pd
import torch
import torch.optim as optim
import pickle
from tqdm import tqdm
import os
from trainer import FullEvaluator, SeqLoader
from utils import args, get_model, init_env, test_remap
@torch.no_grad()
def test(args, test_dataloader, k=20):
result = []
uids = []
model = get_model(args.model)(
args, n_items=test_dataloader.n_items, device=device)
model.to(device)
model.load_state_dict(torch.load(
f'./saved/{args.model}.pth')['state_dict'])
test_items = np.load('./dataset/prepared/test_items.npy')
test_items = torch.from_numpy(test_items + 1).long().to(device)
with tqdm(test_dataloader, total=test_dataloader.batch_num, leave=False) as t:
for _, batch_data in enumerate(t):
logits = model.predict(batch_data)
logits.scatter_(1, batch_data['mask'], -np.inf)
logits[:, test_items] += 100
result.append(torch.topk(logits[:, 1:], k=k, dim=-1)[1].cpu().numpy())
uids.append(batch_data['user_ids'].cpu().numpy())
iids = np.concatenate(result)
uids = np.concatenate(uids)
test_remap(uids, iids)
@torch.no_grad()
def save_test(args, test_dataloader):
scores = []
iids = []
uids = []
model = get_model(args.model)(
args, n_items=test_dataloader.n_items, device=device)
model.to(device)
model.load_state_dict(torch.load(
f'./saved/{args.model}_{args.commit}.pth')['state_dict'])
test_items = np.load('./dataset/prepared/test_items.npy')
test_items = torch.from_numpy(test_items + 1).long().to(device)
with tqdm(test_dataloader, total=test_dataloader.batch_num, leave=False) as t:
for _, batch_data in enumerate(t):
logits = model.predict(batch_data)
logits.scatter_(1, batch_data['mask'], -np.inf)
logits[:, test_items] += 100
score, iid = torch.topk(logits[:, 1:], k=100, dim=-1)
iids.append(iid.cpu().numpy())
scores.append(score.cpu().numpy())
uids.append(batch_data['user_ids'].cpu().numpy())
iids = np.concatenate(iids)
scores = np.concatenate(scores)
uids = np.concatenate(uids)
with open(f'{args.commit}_test_result.pkl', 'wb') as f:
pickle.dump((uids, iids, scores), f)
def save_valid(valid_dataloader, evaluator):
scores = []
iids = []
uids = []
model = get_model(args.model)(
args, n_items=test_dataloader.n_items, device=device)
model.to(device)
model.load_state_dict(torch.load(
f'./saved/{args.model}_{args.commit}.pth')['state_dict'])
model.eval()
matrix = []
result = []
with torch.no_grad():
with tqdm(valid_dataloader, total=valid_dataloader.batch_num, leave=False) as t:
for _, batch_data in enumerate(t):
logits = model.predict(batch_data)
logits.scatter_(1, batch_data['mask'], -np.inf)
score, iid = torch.topk(logits[:, 1:], k=100, dim=-1)
iids.append(iid.cpu().numpy())
scores.append(score.cpu().numpy())
uids.append(batch_data['user_ids'].cpu().numpy())
batch_matrix, idxs = evaluator.collect(
logits, batch_data)
matrix.append(batch_matrix)
result.append(idxs)
iids = np.concatenate(iids)
scores = np.concatenate(scores)
uids = np.concatenate(uids)
with open(f'{args.commit}_valid_result.pkl', 'wb') as f:
pickle.dump((uids, iids, scores), f)
mrr = evaluator.evaluate(matrix)
logger.info(f'MRR@1 {mrr}')
def train(args, train_dataloader, valid_dataloader, model, optimizer, evaluator):
best_result = None
iters = 0
for epoch in range(args.epochs):
# Train
total_loss = []
model.train()
train_dataloader.shuffle()
with tqdm(train_dataloader, total=train_dataloader.batch_num, leave=False) as t:
for idx, batch_data in enumerate(t):
optimizer.zero_grad()
loss = model.calculate_loss(batch_data)
loss.backward()
optimizer.step()
total_loss.append(loss.item())
t.set_description(f'Epoch {epoch}')
t.set_postfix(loss=loss.item())
logger.info('[epoch={}]: loss {}'.format(epoch, np.mean(total_loss)))
# logger.info('[epoch={}]: loss {}]'.format(epoch, np.mean(total_loss)))
# Test
# if args.stage == 'offline':
model.eval()
matrix = []
result = []
with torch.no_grad():
with tqdm(valid_dataloader, total=valid_dataloader.batch_num, leave=False) as t:
for _, batch_data in enumerate(t):
logits = model.predict(batch_data)
logits.scatter_(1, batch_data['mask'], -np.inf)
batch_matrix, idxs = evaluator.collect(
logits, batch_data)
matrix.append(batch_matrix)
result.append(idxs)
mrr = evaluator.evaluate(matrix)
logger.info(f'[epoch={epoch}]: MRR@1 {mrr}')
if best_result is None:
best_result = mrr
model.save_model()
elif mrr > best_result:
best_result = mrr
iters = 0
model.save_model()
else:
iters += 1
if iters > args.early_stop:
logger.info('Early Stop...')
logger.info('Best Result:' + best_result)
break
if __name__ == "__main__":
logger = init_env(args)
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.device)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
valid_dataloader = SeqLoader(
phase='valid', device=device, batch_size=2 * args.batch_size)
evaluator = FullEvaluator(
metrics=["MRR"], topk=20, pos_len=1)
if args.evaluate:
test_dataloader = SeqLoader(
phase='test', device=device, batch_size=2 * args.batch_size)
save_valid(valid_dataloader, evaluator)
save_test(args, test_dataloader)
else:
train_dataloader = SeqLoader(
phase='train', device=device, batch_size=args.batch_size)
model = get_model(args.model)(
args, n_items=train_dataloader.n_items, device=device)
model.to(device)
optimizer = optim.Adam(model.parameters(), lr=args.lr)
train(args, train_dataloader, valid_dataloader,
model, optimizer, evaluator)