-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
181 lines (136 loc) · 5.96 KB
/
test.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
import os
import glob
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# !/bin/bash
import torch.nn as nn
from torch.nn import init
from torch.autograd import Variable
import torch.nn.functional as F
from torch.utils.data import DataLoader
torch.cuda.empty_cache()
import numpy as np
import pandas as pd
import time
import datetime
from GetDataset import BikeDataset
from utils import load_distance, load_dataset
from Stmgam import STMGAM
def train(epoch):
model.train()
loss_train_one = torch.zeros(1).cuda()
for i in range(train_day * hour):
print(day_idxs[i], hour_idxs[i], gt_idxs[i])
feat_rent_days = torch.from_numpy(rent_flow[day_idxs[i],:,:]).float().cuda()
feat_return_days = torch.from_numpy(return_flow[day_idxs[i],:,:]).float().cuda()
feat_rent_hours = torch.from_numpy(rent_flow[hour_idxs[i],:,:]).float().cuda()
feat_return_hours = torch.from_numpy(return_flow[hour_idxs[i],:,:]).float().cuda()
ground_truth_rent = rent_data[gt_idxs[i]].reshape(-1,m_size) / rent_scale
ground_truth_return = return_data[gt_idxs[i]].reshape(-1,m_size) / return_scale
ground_truth = torch.from_numpy(np.concatenate((ground_truth_rent, ground_truth_return),axis = 0)).float()
scores, att = model(feat_rent_days, feat_rent_hours, feat_return_days, feat_return_hours, distance)
loss_train_one += model.loss(scores, ground_truth)
if (i + 1) % batch_size == 0:
loss_train = loss_train_one.div(batch_size)
loss_train.backward()
optimizer.step()
optimizer.zero_grad()
loss_train_one = torch.zeros(1).cuda()
print('loss_train = ', loss_train.item())
model.eval()
loss_val_one = torch.zeros(1).cuda()
for j in range(train_day * hour, (train_day + vali_day) * hour):
with torch.no_grad():
print(day_idxs[j], hour_idxs[j], gt_idxs[j])
feat_rent_days = torch.from_numpy(rent_flow[day_idxs[j],:,:]).float().cuda()
feat_return_days = torch.from_numpy(return_flow[day_idxs[j],:,:]).float().cuda()
feat_rent_hours = torch.from_numpy(rent_flow[hour_idxs[j],:,:]).float().cuda()
feat_return_hours = torch.from_numpy(return_flow[hour_idxs[j],:,:]).float().cuda()
ground_truth_rent = rent_data[gt_idxs[j]].reshape(-1,m_size) / rent_scale
ground_truth_return = return_data[gt_idxs[j]].reshape(-1,m_size) / return_scale
ground_truth = torch.from_numpy(np.concatenate((ground_truth_rent, ground_truth_return),axis = 0)).float()
scores, att = model(feat_rent_days, feat_rent_hours, feat_return_days, feat_return_hours, distance)
loss_val_one += model.loss(scores, ground_truth)
loss_val = loss_val_one.div(vali_day * hour)
print('-----------------------------------')
print('loss_val = ', loss_val.item())
return loss_val.item()
def test():
model.eval()
result = []
ground = []
att_days = []
att_hours = []
for i in range((train_day + vali_day)*hour, (train_day + vali_day + test_day) * hour):
print(i)
feat_rent_days = torch.from_numpy(rent_flow[day_idxs[i],:,:]).float().cuda()
feat_return_days = torch.from_numpy(return_flow[day_idxs[i],:,:]).float().cuda()
feat_rent_hours = torch.from_numpy(rent_flow[hour_idxs[i],:,:]).float().cuda()
feat_return_hours = torch.from_numpy(return_flow[hour_idxs[i],:,:]).float().cuda()
ground_truth_rent = rent_data[gt_idxs[i]].reshape(-1,m_size) / rent_scale
ground_truth_return = return_data[gt_idxs[i]].reshape(-1,m_size) / return_scale
ground_truth = np.concatenate((ground_truth_rent, ground_truth_return),axis = 0)
pre, att = model(feat_rent_days, feat_rent_hours, feat_return_days, feat_return_hours, distance)
result.append(pre.detach().cpu().numpy())
ground.append(ground_truth)
result = np.array(result)
ground = np.array(ground)
return result, ground
start_time = time.time()
# station = 'rent'
path= '/home/wang/wang/chicago/'
# if station == 'rent':
rent_dataset = 'pickup'
rent_flow_dataset = 'sparse_rent2return'
# if station == 'return':
return_dataset = 'dropoff'
return_flow_dataset = 'sparse_return2rent'
day = 7
hour = 96
m_size = 607
train_day = 60
vali_day = 30
test_day = 176
learning_rate = 0.01
epoch_no = 100
# batch_no = hour
batch_size = 16
embed_dim = 32
feature_dim = m_size * 4
rent_data, rent_flow, rent_scale = load_dataset(path, rent_dataset, rent_flow_dataset, m_size)
return_data, return_flow, return_scale = load_dataset(path, return_dataset, return_flow_dataset, m_size)
print('d')
distance = load_distance(path)
day_idxs = []
hour_idxs = []
gt_idxs = []
for n in range(day* hour, (day + train_day + vali_day + test_day) * hour):
day_idxs.append([(n - i) for i in range((day) * hour, 0, -hour)])
hour_idxs.append(list(range((n - hour), n)))
gt_idxs.append(n)
print(len(gt_idxs))
model = STMGAM(feature_dim, embed_dim, m_size, day, hour)
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr = learning_rate)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size = 2, gamma=0.1)
loss_values = []
bad_counter = 0
best = epoch_no + 1
best_epoch = 0
model.load_state_dict(torch.load(path + 'Result/' + '12.pkl'))
with torch.no_grad():
predict, ground_truth = test()
df1 = predict
np.save(path + 'Result/'+ 'pr.npy', df1)
df2 = ground_truth
np.save(path + 'Result/' + 'gt.npy', df2)
# df3 = att_days
# np.save(path + 'Result/' + station +'_att_days.npy', df3)
# df4 = att_hours
# np.save(path + 'Result/' + station +'_att_hours.npy', df4)
torch.cuda.empty_cache()
end_time = time.time()
times = end_time - start_time
print('Total_time =',times)
torch.cuda.empty_cache()
print('max = ', para)