-
Notifications
You must be signed in to change notification settings - Fork 63
/
train_torch.py
217 lines (180 loc) · 8.55 KB
/
train_torch.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
#! /usr/bin/env python
import os
import datetime
import math
import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import data_loader
# Parameters
# ==================================================
ftype = torch.cuda.FloatTensor
ltype = torch.cuda.LongTensor
# Data loading params
train_file = "./prepro_train_50.txt"
valid_file = "./prepro_valid_50.txt"
test_file = "./prepro_test_50.txt"
# Model Hyperparameters
dim = 13 # dimensionality
ww = 360 # winodw width (6h)
up_time = 560632.0 # min
lw_time = 0.
up_dist = 457.335 # km
lw_dist = 0.
reg_lambda = 0.1
# Training Parameters
batch_size = 2
num_epochs = 30
learning_rate = 0.001
momentum = 0.9
evaluate_every = 1
h_0 = Variable(torch.randn(dim, 1), requires_grad=False).type(ftype)
user_cnt = 32899 #50 #107092#0
loc_cnt = 1115406 #50 #1280969#0
#user_cnt = 42242 #30
#loc_cnt = 1164559 #30
try:
xrange
except NameError:
xrange = range
# Data Preparation
# ===========================================================
# Load data
print("Loading data...")
train_user, train_td, train_ld, train_loc, train_dst = data_loader.treat_prepro(train_file, step=1)
valid_user, valid_td, valid_ld, valid_loc, valid_dst = data_loader.treat_prepro(valid_file, step=2)
test_user, test_td, test_ld, test_loc, test_dst = data_loader.treat_prepro(test_file, step=3)
print("User/Location: {:d}/{:d}".format(user_cnt, loc_cnt))
print("==================================================================================")
class STRNNCell(nn.Module):
def __init__(self, hidden_size):
super(STRNNCell, self).__init__()
self.hidden_size = hidden_size
self.weight_ih = nn.Parameter(torch.Tensor(hidden_size, hidden_size)) # C
self.weight_th_upper = nn.Parameter(torch.Tensor(hidden_size, hidden_size)) # T
self.weight_th_lower = nn.Parameter(torch.Tensor(hidden_size, hidden_size)) # T
self.weight_sh_upper = nn.Parameter(torch.Tensor(hidden_size, hidden_size)) # S
self.weight_sh_lower = nn.Parameter(torch.Tensor(hidden_size, hidden_size)) # S
self.location_weight = nn.Embedding(loc_cnt, hidden_size)
self.permanet_weight = nn.Embedding(user_cnt, hidden_size)
self.sigmoid = nn.Sigmoid()
self.reset_parameters()
def reset_parameters(self):
stdv = 1.0 / math.sqrt(self.hidden_size)
for weight in self.parameters():
weight.data.uniform_(-stdv, stdv)
def forward(self, td_upper, td_lower, ld_upper, ld_lower, loc, hx):
loc_len = len(loc)
Ttd = [((self.weight_th_upper*td_upper[i] + self.weight_th_lower*td_lower[i])\
/(td_upper[i]+td_lower[i])) for i in xrange(loc_len)]
Sld = [((self.weight_sh_upper*ld_upper[i] + self.weight_sh_lower*ld_lower[i])\
/(ld_upper[i]+ld_lower[i])) for i in xrange(loc_len)]
loc = self.location_weight(loc).view(-1,self.hidden_size,1)
loc_vec = torch.sum(torch.cat([torch.mm(Sld[i], torch.mm(Ttd[i], loc[i]))\
.view(1,self.hidden_size,1) for i in xrange(loc_len)], dim=0), dim=0)
usr_vec = torch.mm(self.weight_ih, hx)
hx = loc_vec + usr_vec # hidden_size x 1
return self.sigmoid(hx)
def loss(self, user, td_upper, td_lower, ld_upper, ld_lower, loc, dst, hx):
h_tq = self.forward(td_upper, td_lower, ld_upper, ld_lower, loc, hx)
p_u = self.permanet_weight(user)
q_v = self.location_weight(dst)
output = torch.mm(q_v, (h_tq + torch.t(p_u)))
return torch.log(1+torch.exp(torch.neg(output)))
def validation(self, user, td_upper, td_lower, ld_upper, ld_lower, loc, dst, hx):
# error exist in distance (ld_upper, ld_lower)
h_tq = self.forward(td_upper, td_lower, ld_upper, ld_lower, loc, hx)
p_u = self.permanet_weight(user)
user_vector = h_tq + torch.t(p_u)
ret = torch.mm(self.location_weight.weight, user_vector).data.cpu().numpy()
return np.argsort(np.squeeze(-1*ret))
###############################################################################################
def parameters():
params = []
for model in [strnn_model]:
params += list(model.parameters())
return params
def print_score(batches, step):
recall1 = 0.
recall5 = 0.
recall10 = 0.
recall100 = 0.
recall1000 = 0.
recall10000 = 0.
iter_cnt = 0
for batch in tqdm.tqdm(batches, desc="validation"):
batch_user, batch_td, batch_ld, batch_loc, batch_dst = batch
if len(batch_loc) < 3:
continue
iter_cnt += 1
batch_o, target = run(batch_user, batch_td, batch_ld, batch_loc, batch_dst, step=step)
recall1 += target in batch_o[:1]
recall5 += target in batch_o[:5]
recall10 += target in batch_o[:10]
recall100 += target in batch_o[:100]
recall1000 += target in batch_o[:1000]
recall10000 += target in batch_o[:10000]
print("recall@1: ", recall1/iter_cnt)
print("recall@5: ", recall5/iter_cnt)
print("recall@10: ", recall10/iter_cnt)
print("recall@100: ", recall100/iter_cnt)
print("recall@1000: ", recall1000/iter_cnt)
print("recall@10000: ", recall10000/iter_cnt)
###############################################################################################
def run(user, td, ld, loc, dst, step):
optimizer.zero_grad()
seqlen = len(td)
user = Variable(torch.from_numpy(np.asarray([user]))).type(ltype)
#neg_loc = Variable(torch.FloatTensor(1).uniform_(0, len(poi2pos)-1).long()).type(ltype)
#(neg_lati, neg_longi) = poi2pos.get(neg_loc.data.cpu().numpy()[0])
rnn_output = h_0
for idx in xrange(seqlen-1):
td_upper = Variable(torch.from_numpy(np.asarray(up_time-td[idx]))).type(ftype)
td_lower = Variable(torch.from_numpy(np.asarray(td[idx]-lw_time))).type(ftype)
ld_upper = Variable(torch.from_numpy(np.asarray(up_dist-ld[idx]))).type(ftype)
ld_lower = Variable(torch.from_numpy(np.asarray(ld[idx]-lw_dist))).type(ftype)
location = Variable(torch.from_numpy(np.asarray(loc[idx]))).type(ltype)
rnn_output = strnn_model(td_upper, td_lower, ld_upper, ld_lower, location, rnn_output)#, neg_lati, neg_longi, neg_loc, step)
td_upper = Variable(torch.from_numpy(np.asarray(up_time-td[-1]))).type(ftype)
td_lower = Variable(torch.from_numpy(np.asarray(td[-1]-lw_time))).type(ftype)
ld_upper = Variable(torch.from_numpy(np.asarray(up_dist-ld[-1]))).type(ftype)
ld_lower = Variable(torch.from_numpy(np.asarray(ld[-1]-lw_dist))).type(ftype)
location = Variable(torch.from_numpy(np.asarray(loc[-1]))).type(ltype)
if step > 1:
return strnn_model.validation(user, td_upper, td_lower, ld_upper, ld_lower, location, dst[-1], rnn_output), dst[-1]
destination = Variable(torch.from_numpy(np.asarray([dst[-1]]))).type(ltype)
J = strnn_model.loss(user, td_upper, td_lower, ld_upper, ld_lower, location, destination, rnn_output)#, neg_lati, neg_longi, neg_loc, step)
J.backward()
optimizer.step()
return J.data.cpu().numpy()
###############################################################################################
strnn_model = STRNNCell(dim).cuda()
optimizer = torch.optim.SGD(parameters(), lr=learning_rate, momentum=momentum, weight_decay=reg_lambda)
for i in xrange(num_epochs):
# Training
total_loss = 0.
train_batches = list(zip(train_user, train_td, train_ld, train_loc, train_dst))
for j, train_batch in enumerate(tqdm.tqdm(train_batches, desc="train")):
#inner_batches = data_loader.inner_iter(train_batch, batch_size)
#for k, inner_batch in inner_batches:
batch_user, batch_td, batch_ld, batch_loc, batch_dst = train_batch#inner_batch)
if len(batch_loc) < 3:
continue
total_loss += run(batch_user, batch_td, batch_ld, batch_loc, batch_dst, step=1)
#if (j+1) % 2000 == 0:
# print("batch #{:d}: ".format(j+1)), "batch_loss :", total_loss/j, datetime.datetime.now()
# Evaluation
if (i+1) % evaluate_every == 0:
print("==================================================================================")
#print("Evaluation at epoch #{:d}: ".format(i+1)), total_loss/j, datetime.datetime.now()
valid_batches = list(zip(valid_user, valid_td, valid_ld, valid_loc, valid_dst))
print_score(valid_batches, step=2)
# Testing
print("Training End..")
print("==================================================================================")
print("Test: ")
test_batches = list(zip(test_user, test_td, test_ld, test_loc, test_dst))
print_score(test_batches, step=3)