forked from mwong009/mtltrajet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixedlogit.py
237 lines (195 loc) · 7.18 KB
/
mixedlogit.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
import theano
import time
import pickle
import numpy as np
import pandas as pd
import theano.tensor as T
from theano import shared
from collections import OrderedDict
from optimizers import Optimizers
from utility import SetupH5PY, init_tensor
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
# CONSTANTS
VARIABLE_DTYPE_BINARY = 'binary'
VARIABLE_DTYPE_REAL = 'real'
VARIABLE_DTYPE_CATEGORY = 'category'
VARIABLE_DTYPE_INTEGER = 'integer'
DTYPE_FLOATX = theano.config.floatX
def main(data):
# optimizer
opt = Optimizers()
# sampler
theano_rng = RandomStreams(999)
# import dataset
n_samples = data.attrs['n_rows']
lr = 1e-3
batch_size = 128
x_data = [
data['purpose'], data['avg_speed'],
data['duration'], data['trip_km'],
data['n_coord'], data['interval'],
data['dow'],
data['startdistrict'],
data['enddistrict']
]
y_data = [data['mode']]
params = OrderedDict()
params_shp = OrderedDict()
output = []
input = []
asc_params = []
asc_params_m = []
beta_params_f = []
beta_params_s = []
beta_params_sf = []
beta_params = []
beta_params_m = []
for var in y_data:
name = 'asc_' + var.name.strip('/')
asc_shp = var['data'][:].squeeze().shape[1:]
print('y', name, asc_shp)
output.append(init_tensor((), name))
mask = np.ones(asc_shp, DTYPE_FLOATX)
mask[-1] = 0.
asc_value = np.zeros(asc_shp, DTYPE_FLOATX) * mask
asc_params.append(shared(asc_value, name))
asc_params_m.append(shared(mask, name + '_mask'))
params[name] = asc_params[-1]
params_shp[name] = asc_shp
for var in x_data:
name = 'beta_' + var.name.strip('/')
shp = var['data'].shape[1:] + asc_shp
print('x', name, shp)
input.append(init_tensor(var['data'].shape[1:], name))
mask = np.ones(shp, DTYPE_FLOATX)
mask[..., -1] = 0.
mask = mask.flatten()
beta_value = np.zeros(np.prod(shp), DTYPE_FLOATX) * mask
sigma_value = np.ones(np.prod(shp), DTYPE_FLOATX) * mask
beta_params_f.append(shared(beta_value, name))
beta_params_sf.append(shared(sigma_value, name + '_sigma'))
beta_params.append(T.reshape(beta_params_f[-1], shp))
beta_params_s.append(T.reshape(beta_params_sf[-1], shp))
beta_params_m.append(shared(mask, name + '_mask'))
params[name] = beta_params_f[-1]
params[name + '_sigma'] = beta_params_sf[-1]
params_shp[name] = shp
params_shp[name + '_sigma'] = shp
# compute the utility function
utility = 0.
h_utility = 0.
for x, b, s in zip(input, beta_params, beta_params_s):
normal_sample = b[..., None] + T.sqr(s)[..., None] * theano_rng.normal(
size=b.eval().shape + (1,),
avg=0.,
std=1.,
dtype=DTYPE_FLOATX
)
ax = [np.arange(x.ndim)[1:], np.arange(b.ndim)[:-1]]
utility += T.tensordot(x, normal_sample, axes=ax)
if x.ndim > 2:
h_utility += T.tensordot(x, b + T.sqr(s), axes=[[1, 2], [0, 1]])
else:
h_utility += T.tensordot(x, b + T.sqr(s), axes=[[1], [0]])
for y, asc in zip(output, asc_params):
utility += asc[None, ..., None]
h_utility += asc
(d1, d2, d3) = utility.shape
utility = utility.reshape((d1 * d3, d2))
p_y_given_x = T.nnet.softmax(utility)
hessian_prob = T.nnet.softmax(h_utility) #!
hessian_nll = T.log(hessian_prob)
hessian_cr = hessian_nll[T.arange(y.shape[0]), y]
hessian_cost = -T.sum(hessian_cr)
nll = T.log(p_y_given_x).reshape((d3, d1, d2))
nll = nll[:, T.arange(y.shape[0]), y]
cost = -T.sum(T.mean(nll, axis=0))
gparams = asc_params + beta_params_f + beta_params_sf
grads = T.grad(cost, gparams)
# mask gradient updates
mask = asc_params_m + beta_params_m + beta_params_m
for j, g in enumerate(grads):
grads[j] = g * mask[j]
# create list of updates to iterate over
updates = opt.sgd_updates(gparams, grads, lr)
# symbolic equation for the Hessian function
stderrs = []
hessian = T.hessian(cost=hessian_cost, wrt=gparams)
stderr = [T.sqrt(f) for f in [T.diag(2. / h) for h in hessian]]
stderrs.extend(stderr)
tensors = input + output
shared_x = [shared(var['data'][:], borrow=True) for var in x_data]
shared_y = [T.cast(shared(var['label'][:]), 'int32') for var in y_data]
shared_variables = shared_x + shared_y
i = T.lscalar('index')
start_idx = i * batch_size
end_idx = (i + 1) * batch_size
print('constructing Theano computational graph...')
train = theano.function(
inputs=[i],
outputs=cost,
updates=updates,
givens={
key: val[start_idx: end_idx]
for key, val in zip(tensors, shared_variables)
},
name='train',
allow_input_downcast=True,
)
std_err = theano.function(
inputs=[],
outputs=stderrs,
givens={
key: val[:]
for key, val in zip(tensors, shared_variables)
},
name='std errors',
allow_input_downcast=True,
)
# train model
print('training the model...')
curves = []
n_batches = n_samples // batch_size
epochs = 100
epoch = 0
t0 = time.time()
while epoch < epochs:
epoch += 1
cost = []
for i in range(n_batches):
cost_items = train(i)
cost.append(cost_items)
epoch_cost = np.sum(cost)
curves.append((epoch, epoch_cost))
minutes, seconds = divmod(time.time()-t0, 60.)
hours, minutes = divmod(minutes, 60.)
print(("epoch {0:d} loglikelihood "
"{1:.3f} time {hh:02d}:{mm:02d}:{ss:05.2f}").format(
epoch, epoch_cost, hh=int(hours), mm=int(minutes), ss=seconds))
if (epoch % 5) == 0:
print('checkpoint')
param_values = {}
for name, param in params.items():
param_shp = params_shp[name]
param_values[name] = param.eval().reshape(param_shp)
np.savetxt('params/{}.csv'.format(name),
param_values[name].squeeze(),
fmt='%.3f', delimiter=',')
to_file = param_values, curves
path = 'params/epoch_{0:d}.params'.format(epoch)
with open(path, 'wb') as f:
pickle.dump(to_file, f, protocol=pickle.HIGHEST_PROTOCOL)
# save parameters and stderrs to .csv
stderrs = std_err()
params_list = [p for p in asc_params + beta_params_f + beta_params_sf]
param_names = [p.name for p in asc_params + beta_params_f + beta_params_sf]
for se, param, name in zip(stderrs, params_list, param_names):
v = param.eval().squeeze()
shp = v.shape
path = 'params/stderrs_{}.csv'.format(name)
np.savetxt(path, se.reshape(shp), fmt='%.3f', delimiter=',')
path = 'params/tstat_{}.csv'.format(name)
np.savetxt(path, v / se.reshape(shp), fmt='%.3f', delimiter=',')
if __name__ == '__main__':
dataset = SetupH5PY.load_dataset('data.h5')
main(dataset)