-
Notifications
You must be signed in to change notification settings - Fork 32
/
run.py
201 lines (146 loc) · 6.51 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
import numpy as np
import torch
from torch import optim
import random
from copy import deepcopy
from utils import get_data, ndcg, recall, implicit_slim
from model import VAE
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str)
parser.add_argument('--hidden-dim', type=int, default=600)
parser.add_argument('--latent-dim', type=int, default=200)
parser.add_argument('--batch-size', type=int, default=500)
parser.add_argument('--beta', type=float, default=None)
parser.add_argument('--gamma', type=float, default=None)
parser.add_argument('--lr', type=float, default=5e-4)
parser.add_argument('--n-epochs', type=int, default=None)
parser.add_argument('--n-enc_epochs', type=int, default=3)
parser.add_argument('--n-dec_epochs', type=int, default=1)
parser.add_argument('--not-alternating', default=False, action="store_true")
parser.add_argument('--implicitslim', default=False, action="store_true")
parser.add_argument('--lambd', type=float, default=None)
parser.add_argument('--alpha', type=float, default=None)
parser.add_argument('--threshold', type=int, default=None)
parser.add_argument('--step', type=int, default=None)
args = parser.parse_args()
seed = 1337
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
device = torch.device("cuda:0")
data = get_data(args.dataset)
train_data, valid_in_data, valid_out_data, test_in_data, test_out_data = data
def generate(batch_size, device, data_in, data_out=None, shuffle=False, samples_perc_per_epoch=1):
assert 0 < samples_perc_per_epoch <= 1
total_samples = data_in.shape[0]
samples_per_epoch = int(total_samples * samples_perc_per_epoch)
if shuffle:
idxlist = np.arange(total_samples)
np.random.shuffle(idxlist)
idxlist = idxlist[:samples_per_epoch]
else:
idxlist = np.arange(samples_per_epoch)
for st_idx in range(0, samples_per_epoch, batch_size):
end_idx = min(st_idx + batch_size, samples_per_epoch)
idx = idxlist[st_idx:end_idx]
yield Batch(device, idx, data_in, data_out)
class Batch:
def __init__(self, device, idx, data_in, data_out=None):
self._device = device
self._idx = idx
self._data_in = data_in
self._data_out = data_out
def get_idx(self):
return self._idx
def get_idx_to_dev(self):
return torch.LongTensor(self.get_idx()).to(self._device)
def get_ratings(self, is_out=False):
data = self._data_out if is_out else self._data_in
return data[self._idx]
def get_ratings_to_dev(self, is_out=False):
return torch.Tensor(
self.get_ratings(is_out).toarray()
).to(self._device)
def evaluate(model, data_in, data_out, metrics, samples_perc_per_epoch=1, batch_size=500):
metrics = deepcopy(metrics)
model.eval()
for m in metrics:
m['score'] = []
for batch in generate(batch_size=batch_size,
device=device,
data_in=data_in,
data_out=data_out,
samples_perc_per_epoch=samples_perc_per_epoch
):
ratings_in = batch.get_ratings_to_dev()
ratings_out = batch.get_ratings(is_out=True)
ratings_pred = model(ratings_in, calculate_loss=False).cpu().detach().numpy()
if not (data_in is data_out):
ratings_pred[batch.get_ratings().nonzero()] = -np.inf
for m in metrics:
m['score'].append(m['metric'](ratings_pred, ratings_out, k=m['k']))
for m in metrics:
m['score'] = np.concatenate(m['score']).mean()
return [x['score'] for x in metrics]
def run(model, opts, train_data, batch_size, n_epochs, beta, gamma, dropout_rate):
model.train()
for epoch in range(n_epochs):
for batch in generate(batch_size=batch_size, device=device, data_in=train_data, shuffle=True):
ratings = batch.get_ratings_to_dev()
for optimizer in opts:
optimizer.zero_grad()
_, loss = model(ratings, beta=beta, gamma=gamma, dropout_rate=dropout_rate)
loss.backward()
for optimizer in opts:
optimizer.step()
model_kwargs = {
'hidden_dim': args.hidden_dim,
'latent_dim': args.latent_dim,
'input_dim': train_data.shape[1]
}
metrics = [{'metric': ndcg, 'k': 100}]
best_ndcg = -np.inf
train_scores, valid_scores = [], []
model = VAE(**model_kwargs).to(device)
model_best = VAE(**model_kwargs).to(device)
learning_kwargs = {
'model': model,
'train_data': train_data,
'batch_size': args.batch_size,
'beta': args.beta,
'gamma': args.gamma
}
decoder_params = set(model.decoder.parameters())
encoder_params = set(model.encoder.parameters())
optimizer_encoder = optim.Adam(encoder_params, lr=args.lr)
optimizer_decoder = optim.Adam(decoder_params, lr=args.lr)
for epoch in range(args.n_epochs):
if args.implicitslim and epoch % args.step == args.step - 1:
encoder_embs = model.encoder.fc1.weight.data
decoder_embs = model.decoder.weight.data.T
for embs in [encoder_embs, decoder_embs]:
embs[:] = torch.Tensor(
implicit_slim(embs.detach().cpu().numpy(), train_data, args.lambd, args.alpha, args.threshold)
).to(device)
if args.not_alternating:
run(opts=[optimizer_encoder, optimizer_decoder], n_epochs=1, dropout_rate=0.5, **learning_kwargs)
else:
run(opts=[optimizer_encoder], n_epochs=args.n_enc_epochs, dropout_rate=0.5, **learning_kwargs)
model.update_prior()
run(opts=[optimizer_decoder], n_epochs=args.n_dec_epochs, dropout_rate=0, **learning_kwargs)
train_scores.append(
evaluate(model, train_data, train_data, metrics, 0.01)[0]
)
valid_scores.append(
evaluate(model, valid_in_data, valid_out_data, metrics, 1)[0]
)
if valid_scores[-1] > best_ndcg:
best_ndcg = valid_scores[-1]
model_best.load_state_dict(deepcopy(model.state_dict()))
print(f'epoch {epoch} | valid ndcg@100: {valid_scores[-1]:.4f} | ' +
f'best valid: {best_ndcg:.4f} | train ndcg@100: {train_scores[-1]:.4f}')
test_metrics = [{'metric': ndcg, 'k': 100}, {'metric': recall, 'k': 20}, {'metric': recall, 'k': 50}]
final_scores = evaluate(model_best, test_in_data, test_out_data, test_metrics)
for metric, score in zip(test_metrics, final_scores):
print(f"{metric['metric'].__name__}@{metric['k']}:\t{score:.4f}")