-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_nyc_bike.py
285 lines (238 loc) · 10.3 KB
/
train_nyc_bike.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import sys
import argparse
import h5py
import numpy as np
from sklearn import metrics
import torch
from torch import nn
from torch import optim
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torch.utils.data.sampler import SubsetRandomSampler
sys.path.append('../../')
from models.STPWNet import PWNet
import random
parse = argparse.ArgumentParser()
parse.add_argument('-close_size', type=int, default=3)
parse.add_argument('-period_size', type=int, default=0)
parse.add_argument('-trend_size', type=int, default=0)
parse.add_argument('-test_rate', type=float, default=0.2)
parse.set_defaults(crop=False)
parse.add_argument('-train', dest='train', action='store_true')
parse.add_argument('-no-train', dest='train', action='store_false')
parse.set_defaults(train=True)
parse.add_argument('-loss', type=str, default='l2', help='l1 | l2')
parse.add_argument('-lr', type=float, default=0.001)
parse.add_argument('-batch_size', type=int, default=32, help='batch size')
parse.add_argument('-epoch', type=int, default=100, help='epochs')
parse.add_argument('-save_dir', type=str, default='results')
opt = parse.parse_args()
def train_epoch():
total_loss = 0
model.train()
data = train_loader
if (opt.period_size > 0) & (opt.close_size > 0) & (opt.trend_size > 0):
for idx, (c, p, t, target) in enumerate(data):
optimizer.zero_grad()
model.zero_grad()
input_var = [Variable(_.float()).cuda() for _ in [c, p, t]]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = model(input_var)
loss = criterion(pred, target_var)
total_loss += loss.item()
loss.backward()
optimizer.step()
elif (opt.close_size > 0) & (opt.period_size > 0):
for idx, (c, p, target) in enumerate(data):
optimizer.zero_grad()
model.zero_grad()
input_var = [Variable(_.float()).cuda() for _ in [c, p]]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = model(input_var)
loss = criterion(pred, target_var)
total_loss += loss.item()
loss.backward()
optimizer.step()
elif opt.close_size > 0:
for idx, (c, target) in enumerate(data):
optimizer.zero_grad()
model.zero_grad()
x = [Variable(c.float()).cuda()]
y = Variable(target.float(), requires_grad=False).cuda()
pred = model(x)
loss = criterion(pred, y)
total_loss += loss.item()
loss.backward()
optimizer.step()
return total_loss
def valid_epoch():
total_loss = 0
model.eval()
data = valid_loader
if (opt.period_size > 0) & (opt.close_size > 0) & (opt.trend_size > 0):
for idx, (c, p, t, target) in enumerate(data):
input_var = [Variable(_.float()).cuda() for _ in [c, p, t]]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = model(input_var)
loss = criterion(pred, target_var)
total_loss += loss.item()
elif (opt.close_size > 0) & (opt.period_size > 0):
for idx, (c, p, target) in enumerate(data):
input_var = [Variable(_.float()).cuda() for _ in [c, p]]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = model(input_var)
loss = criterion(pred, target_var)
total_loss += loss.item()
elif opt.close_size > 0:
for idx, (c, target) in enumerate(data):
x = [Variable(c.float()).cuda()]
y = Variable(target.float(), requires_grad=False).cuda()
pred = model(x)
loss = criterion(pred, y)
total_loss += loss.item()
return total_loss
def train():
best_valid_loss = 1.0
train_loss, valid_loss = [], []
for i in range(opt.epoch):
print('epoch ',i)
train_loss.append(train_epoch())
valid_loss.append(valid_epoch())
if valid_loss[-1] < best_valid_loss:
best_valid_loss = valid_loss[-1]
torch.save({'epoch': i, 'model': model, 'train_loss': train_loss,
'valid_loss': valid_loss}, '.model')
torch.save(optimizer, '.optim')
print('train and val loss =', train_loss[-1],valid_loss[-1])
def predict(test_type='test'):
predictions = []
ground_truth = []
loss = []
best_model = torch.load('.model').get('model')
if test_type == 'train':
data = train_loader
elif test_type == 'test':
data = test_loader
elif test_type == 'valid':
data = valid_loader
if (opt.period_size > 0) & (opt.close_size > 0) & (opt.trend_size > 0):
for idx, (c, p, t, target) in enumerate(data):
input_var = [Variable(_.float()).cuda() for _ in [c, p, t]]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = best_model(input_var)
predictions.append(pred.data.cpu().numpy())
ground_truth.append(target.numpy())
loss.append(criterion(pred, target_var).item())
elif (opt.close_size > 0) & (opt.period_size > 0):
print('--> test: close size & period size',opt.close_size,opt.period_size)
for idx, (c, p, target) in enumerate(data):
input_var = [Variable(_.float()).cuda() for _ in [c, p]]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = best_model(input_var)
predictions.append(pred.data.cpu().numpy())
ground_truth.append(target.numpy())
loss.append(criterion(pred, target_var).item())
elif opt.close_size > 0:
for idx, (c, target) in enumerate(data):
input_var = [Variable(c.float()).cuda()]
target_var = Variable(target.float(), requires_grad=False).cuda()
pred = best_model(input_var)
predictions.append(pred.data.cpu().numpy())
ground_truth.append(target.numpy())
loss.append(criterion(pred, target_var).item())
final_predict = np.concatenate(predictions) * mmn[1]+mmn[0]
ground_truth = np.concatenate(ground_truth) * mmn[1]+mmn[0]
print('final prediction shape:', final_predict.shape, ground_truth.shape)
np.save('final_predict.npy',final_predict)
np.save('ground_truth.npy',ground_truth)
print('final prediction and ground truth shape: {} {}'.format(final_predict.shape, ground_truth.shape))
print('FINAL RMSE:{:0.2f}'.format(
metrics.mean_squared_error(ground_truth.ravel(), final_predict.ravel()) ** 0.5))
print('FINAL MAE:{:0.2f}'.format(
metrics.mean_absolute_error(ground_truth.ravel(), final_predict.ravel())))
print('FINAL R2:{:0.2f}'.format(
metrics.r2_score(ground_truth.ravel(), final_predict.ravel())))
print('FINAL Variance:{:0.2f}'.format(
metrics.explained_variance_score(ground_truth.ravel(), final_predict.ravel())))
def train_valid_split(dataloader, test_size=0.2, shuffle=True, random_seed=0):
length=len(dataloader)
indices = list(range(0, length))
if shuffle:
np.random.seed(random_seed)
np.random.shuffle(indices)
if type(test_size) is float:
split = int(np.floor(test_size * length))
elif type(test_size) is int:
split = test_size
else:
raise ValueError('%s should be an int or float'.format(str))
return indices[split:], indices[:split]
def create_dataset(data, close_len=3, output_len=1,period_len=0, test_rate=0, shuffle=False, norm=True):
'''test length will be set 30 or 60,
shuffle equal false is mean using last 30 days for test ...'''
time_intervel = max(close_len, period_len * 30)
print(close_len,output_len)
X = []
Y = []
for i in range(time_intervel, len(data) - output_len + 1):
X.append(data[i - time_intervel:i])
Y.append(data[i + output_len-1:i+output_len])
X = np.array(X)
Y = np.array(Y)
X = np.reshape(X,(X.shape[0],-1,X.shape[-2],X.shape[-1]))
if output_len>=1:
Y=np.squeeze(Y,1)
if shuffle:
index = [i for i in range(len(X))]
random.shuffle(index)
X = X[index]
Y = Y[index]
test_len=int(test_rate*len(X))
x_train, y_train, x_test, y_test = X[:-test_len], Y[:-test_len], X[-test_len:], Y[-test_len:]
mmn_list = []
if norm:
max_value = np.max(x_train)
min_value = np.min(x_train)
max_sub_min = max_value - min_value
x_train = (x_train- min_value) / max_sub_min
y_train = (y_train - min_value) / max_sub_min
x_test = (x_test - min_value) / max_sub_min
y_test = (y_test - min_value) / max_sub_min
mmn_list.append(min_value)
mmn_list.append(max_sub_min)
return [x_train], [y_train], [x_test], [y_test], mmn_list
return [x_train], [y_train], [x_test], [y_test], mmn_list
if __name__ == '__main__':
f = h5py.File('data/Bike_NYC14_M16x8_T60_NewEnd.h5')
data = f['data']
x_train, y_train, x_test, y_test, mmn = create_dataset(data,close_len=3,output_len=1,test_rate=0.2)
x_train+=y_train
x_test+=y_test
train_data = list(zip(*x_train))
test_data = list(zip(*x_test))
train_idx, valid_idx = train_valid_split(train_data, 0.1, shuffle=True)
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
train_loader = DataLoader(train_data, batch_size=opt.batch_size, sampler=train_sampler,
num_workers=8, pin_memory=True)
valid_loader = DataLoader(train_data, batch_size=opt.batch_size, sampler=valid_sampler,
num_workers=2, pin_memory=True)
test_loader = DataLoader(test_data, batch_size=opt.batch_size, shuffle=False)
# get data channels
channels = [opt.close_size*2,
opt.period_size*2,
opt.trend_size*2]
model = PWNet(6,2).cuda()
optimizer = optim.Adam(model.parameters(), opt.lr)
if not os.path.exists(opt.save_dir):
os.makedirs(opt.save_dir)
if not os.path.isdir(opt.save_dir):
raise Exception('%s is not a dir' % opt.save_dir)
if opt.loss == 'l1':
criterion = nn.L1Loss().cuda()
elif opt.loss == 'l2':
criterion = nn.MSELoss().cuda()
if opt.train:
print('Training...')
train()