forked from younggyoseo/vae-cf-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_disentangled.py
267 lines (217 loc) · 9.62 KB
/
main_disentangled.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
import argparse
import time
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
from tensorboardX import SummaryWriter
from scipy import sparse
import models_dis
import data
import metric
import torch.nn.functional as F
parser = argparse.ArgumentParser(description='PyTorch Variational Autoencoders for Collaborative Filtering')
parser.add_argument('--data', type=str, default='data/amazon',
help='Movielens-20m dataset location')
parser.add_argument('--lr', type=float, default=1e-3,
help='initial learning rate')
parser.add_argument('--wd', type=float, default=0.001,
help='weight decay coefficient')
parser.add_argument('--batch_size', type=int, default=100,
help='batch size')
parser.add_argument('--epochs', type=int, default=1,
help='upper epoch limit')
# parser.add_argument('--total_anneal_steps', type=int, default=200000,
# help='the total number of gradient updates for annealing')
parser.add_argument('--anneal_cap', type=float, default=0.2,
help='largest annealing parameter')
parser.add_argument('--seed', type=int, default=98765,
help='random seed')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='report interval')
parser.add_argument('--save', type=str, default='model.pt',
help='path to save the final model')
parser.add_argument('--keep', type=float, default=0.5,
help='Keep probability for dropout, in (0,1].')
parser.add_argument('--tau', type=float, default=0.1,
help='Temperature of sigmoid/softmax, in (0,oo).')
parser.add_argument('--std', type=float, default=0.075,
help='Standard deviation of the Gaussian prior.')
parser.add_argument('--kfac', type=int, default=7,
help='Number of facets (macro concepts).')
parser.add_argument('--dfac', type=int, default=100,
help='Dimension of each facet.')
parser.add_argument('--nogb', action='store_true', default=False,
help='Disable Gumbel-Softmax sampling.')
args = parser.parse_args()
# Set the random seed manually for reproductibility.
torch.manual_seed(args.seed)
if torch.cuda.is_available():
if not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
device = torch.device("cuda" if args.cuda else "cpu")
###############################################################################
# Load data
###############################################################################
# args.data = 'alishop-7c'
loader = data.DataLoader(args.data)
n_items = loader.load_n_items()
train_data, _ = loader.load_data('train')
vad_data_tr, vad_data_te, _ = loader.load_data('validation')
test_data_tr, test_data_te, _ = loader.load_data('test')
N = train_data.shape[0]
idxlist = list(range(N))
num_batches = int(np.ceil(float(N) / args.batch_size))
total_anneal_steps = 5 * num_batches
###############################################################################
# Build the model
###############################################################################
p_dims = [args.dfac, args.dfac, n_items]
model = models_dis.MultiVAE(p_dims, q_dims=None, dropout=args.keep, tau=args.tau,
std=args.std, kfac=args.kfac, nogb=args.nogb).to(device)
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.wd)
criterion = models_dis.loss_function
###############################################################################
# Training code
###############################################################################
# TensorboardX Writer
writer = SummaryWriter()
def sparse2torch_sparse(data):
"""
Convert scipy sparse matrix to torch sparse tensor with L2 Normalization
This is much faster than naive use of torch.FloatTensor(data.toarray())
https://discuss.pytorch.org/t/sparse-tensor-use-cases/22047/2
"""
samples = data.shape[0]
features = data.shape[1]
coo_data = data.tocoo()
indices = torch.LongTensor([coo_data.row, coo_data.col])
row_norms_inv = 1 / np.sqrt(data.sum(1))
row2val = {i: row_norms_inv[i].item() for i in range(samples)}
values = np.array([row2val[r] for r in coo_data.row])
t = torch.sparse.FloatTensor(indices, torch.from_numpy(values).float(), [samples, features])
return t
def naive_sparse2tensor(data):
return torch.FloatTensor(data.toarray())
def set_rng_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
def train():
set_rng_seed(args.seed)
# Turn on training mode
model.train()
train_loss = 0.0
start_time = time.time()
global update_count
np.random.shuffle(idxlist)
for batch_idx, start_idx in enumerate(range(0, N, args.batch_size)):
end_idx = min(start_idx + args.batch_size, N)
data = train_data[idxlist[start_idx:end_idx]]
data = naive_sparse2tensor(data).to(device)
if total_anneal_steps > 0:
anneal = min(args.anneal_cap,
1. * update_count / total_anneal_steps)
else:
anneal = args.anneal_cap
optimizer.zero_grad()
# recon_batch, mu, logvar = model(data)
std_list, recon_batch = model(data)
loss = criterion(data, std_list, recon_batch, anneal)
loss.backward()
train_loss += loss.item()
optimizer.step()
update_count += 1
if batch_idx % args.log_interval == 0 and batch_idx > 0:
elapsed = time.time() - start_time
print('| epoch {:3d} | {:4d}/{:4d} batches | ms/batch {:4.2f} | '
'loss {:4.2f}'.format(
epoch, batch_idx, len(range(0, N, args.batch_size)),
elapsed * 1000 / args.log_interval,
train_loss / args.log_interval))
# Log loss to tensorboard
n_iter = (epoch - 1) * len(range(0, N, args.batch_size)) + batch_idx
writer.add_scalars('data/loss', {'train': train_loss / args.log_interval}, n_iter)
start_time = time.time()
train_loss = 0.0
def evaluate(data_tr, data_te):
set_rng_seed(args.seed)
# Turn on evaluation mode
model.eval()
total_loss = 0.0
global update_count
e_idxlist = list(range(data_tr.shape[0]))
e_N = data_tr.shape[0]
n100_list = []
r20_list = []
r50_list = []
with torch.no_grad():
for start_idx in range(0, e_N, args.batch_size):
end_idx = min(start_idx + args.batch_size, N)
data = data_tr[e_idxlist[start_idx:end_idx]]
heldout_data = data_te[e_idxlist[start_idx:end_idx]]
data_tensor = naive_sparse2tensor(data).to(device)
if total_anneal_steps > 0:
anneal = min(args.anneal_cap,
1. * update_count / total_anneal_steps)
else:
anneal = args.anneal_cap
# recon_batch, mu, logvar = model(data_tensor)
std_list, recon_batch = model(data_tensor)
# loss = criterion(recon_batch, data_tensor, mu, logvar, anneal)
loss = criterion(data_tensor, std_list, recon_batch, anneal)
total_loss += loss.item()
# Exclude examples from training set
# recon_batch = F.log_softmax(recon_batch, 1) # TODO: not sure
recon_batch = recon_batch.cpu().numpy()
recon_batch[data.nonzero()] = -np.inf
n100 = metric.NDCG_binary_at_k_batch(recon_batch, heldout_data, 100)
r20 = metric.Recall_at_k_batch(recon_batch, heldout_data, 20)
r50 = metric.Recall_at_k_batch(recon_batch, heldout_data, 50)
n100_list.append(n100)
r20_list.append(r20)
r50_list.append(r50)
total_loss /= len(range(0, e_N, args.batch_size))
n100_list = np.concatenate(n100_list)
r20_list = np.concatenate(r20_list)
r50_list = np.concatenate(r50_list)
return total_loss, np.mean(n100_list), np.mean(r20_list), np.mean(r50_list)
best_n100 = -np.inf
update_count = 0
# At any point you can hit Ctrl + C to break out of training early.
try:
for epoch in range(1, args.epochs + 1):
epoch_start_time = time.time()
# train
train()
# evaluate
val_loss, n100, r20, r50 = evaluate(vad_data_tr, vad_data_te)
print('-' * 89)
print('| end of epoch {:3d} | time: {:4.2f}s | valid loss {:4.2f} | '
'n100 {:5.3f} | r20 {:5.3f} | r50 {:5.3f}'.format(
epoch, time.time() - epoch_start_time, val_loss,
n100, r20, r50))
print('-' * 89)
n_iter = epoch * len(range(0, N, args.batch_size))
writer.add_scalars('data/loss', {'valid': val_loss}, n_iter)
writer.add_scalar('data/n100', n100, n_iter)
writer.add_scalar('data/r20', r20, n_iter)
writer.add_scalar('data/r50', r50, n_iter)
# Save the model if the n100 is the best we've seen so far.
if n100 > best_n100:
with open(args.save, 'wb') as f:
torch.save(model, f)
best_n100 = n100
except KeyboardInterrupt:
print('-' * 89)
print('Exiting from training early')
# Load the best saved model.
with open(args.save, 'rb') as f:
model = torch.load(f)
# Run on test data.
test_loss, n100, r20, r50 = evaluate(test_data_tr, test_data_te)
print('=' * 89)
print('| End of training | test loss {:4.5f} | n100 {:4.5f} | r20 {:4.5f} | '
'r50 {:4.5f}'.format(test_loss, n100, r20, r50))
print('=' * 89)