-
Notifications
You must be signed in to change notification settings - Fork 98
/
main.py
173 lines (143 loc) · 5 KB
/
main.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
# -*- coding:utf-8 -*-
import time
import json
import argparse
import numpy as np
import mxnet as mx
from utils import (construct_model, generate_data,
masked_mae_np, masked_mape_np, masked_mse_np)
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, help='configuration file')
parser.add_argument("--test", action="store_true", help="test program")
parser.add_argument("--plot", help="plot network graph", action="store_true")
parser.add_argument("--save", action="store_true", help="save model")
args = parser.parse_args()
config_filename = args.config
with open(config_filename, 'r') as f:
config = json.loads(f.read())
print(json.dumps(config, sort_keys=True, indent=4))
net = construct_model(config)
batch_size = config['batch_size']
num_of_vertices = config['num_of_vertices']
graph_signal_matrix_filename = config['graph_signal_matrix_filename']
if isinstance(config['ctx'], list):
ctx = [mx.gpu(i) for i in config['ctx']]
elif isinstance(config['ctx'], int):
ctx = mx.gpu(config['ctx'])
loaders = []
true_values = []
for idx, (x, y) in enumerate(generate_data(graph_signal_matrix_filename)):
if args.test:
x = x[: 100]
y = y[: 100]
y = y.squeeze(axis=-1)
print(x.shape, y.shape)
loaders.append(
mx.io.NDArrayIter(
x, y if idx == 0 else None,
batch_size=batch_size,
shuffle=(idx == 0),
label_name='label'
)
)
if idx == 0:
training_samples = x.shape[0]
else:
true_values.append(y)
train_loader, val_loader, test_loader = loaders
val_y, test_y = true_values
global_epoch = 1
global_train_steps = training_samples // batch_size + 1
all_info = []
epochs = config['epochs']
mod = mx.mod.Module(
net,
data_names=['data'],
label_names=['label'],
context=ctx
)
mod.bind(
data_shapes=[(
'data',
(batch_size, config['points_per_hour'], num_of_vertices, 1)
), ],
label_shapes=[(
'label',
(batch_size, config['points_per_hour'], num_of_vertices)
)]
)
mod.init_params(initializer=mx.init.Xavier(magnitude=0.0003))
lr_sch = mx.lr_scheduler.PolyScheduler(
max_update=global_train_steps * epochs * config['max_update_factor'],
base_lr=config['learning_rate'],
pwr=2,
warmup_steps=global_train_steps
)
mod.init_optimizer(
optimizer=config['optimizer'],
optimizer_params=(('lr_scheduler', lr_sch),)
)
num_of_parameters = 0
for param_name, param_value in mod.get_params()[0].items():
# print(param_name, param_value.shape)
num_of_parameters += np.prod(param_value.shape)
print("Number of Parameters: {}".format(num_of_parameters), flush=True)
metric = mx.metric.create(['RMSE', 'MAE'], output_names=['pred_output'])
if args.plot:
graph = mx.viz.plot_network(net)
graph.format = 'png'
graph.render('graph')
def training(epochs):
global global_epoch
lowest_val_loss = 1e6
for _ in range(epochs):
t = time.time()
info = [global_epoch]
train_loader.reset()
metric.reset()
for idx, databatch in enumerate(train_loader):
mod.forward_backward(databatch)
mod.update_metric(metric, databatch.label)
mod.update()
metric_values = dict(zip(*metric.get()))
print('training: Epoch: %s, RMSE: %.2f, MAE: %.2f, time: %.2f s' % (
global_epoch, metric_values['rmse'], metric_values['mae'],
time.time() - t), flush=True)
info.append(metric_values['mae'])
val_loader.reset()
prediction = mod.predict(val_loader)[1].asnumpy()
loss = masked_mae_np(val_y, prediction, 0)
print('validation: Epoch: %s, loss: %.2f, time: %.2f s' % (
global_epoch, loss, time.time() - t), flush=True)
info.append(loss)
if loss < lowest_val_loss:
test_loader.reset()
prediction = mod.predict(test_loader)[1].asnumpy()
tmp_info = []
for idx in range(config['num_for_predict']):
y, x = test_y[:, : idx + 1, :], prediction[:, : idx + 1, :]
tmp_info.append((
masked_mae_np(y, x, 0),
masked_mape_np(y, x, 0),
masked_mse_np(y, x, 0) ** 0.5
))
mae, mape, rmse = tmp_info[-1]
print('test: Epoch: {}, MAE: {:.2f}, MAPE: {:.2f}, RMSE: {:.2f}, '
'time: {:.2f}s'.format(
global_epoch, mae, mape, rmse, time.time() - t))
print(flush=True)
info.extend((mae, mape, rmse))
info.append(tmp_info)
all_info.append(info)
lowest_val_loss = loss
global_epoch += 1
if args.test:
epochs = 5
training(epochs)
the_best = min(all_info, key=lambda x: x[2])
print('step: {}\ntraining loss: {:.2f}\nvalidation loss: {:.2f}\n'
'tesing: MAE: {:.2f}\ntesting: MAPE: {:.2f}\n'
'testing: RMSE: {:.2f}\n'.format(*the_best))
print(the_best)
if args.save:
mod.save_checkpoint('STSGCN', epochs)