-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_multi.py
391 lines (353 loc) ยท 21.5 KB
/
train_multi.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from models.MyArima import *
import numpy as np
from libs.utils import generate_x_y
from models.STResNet_Multi_Step_Pre import stresnet_multi_step_pre
import keras.backend as K
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping
import config
import pandas as pd
import warnings
import os
def train_stresnet(Metro_Flow_Matrix, Metro_Edge_Flow_Matrix):
warnings.filterwarnings('ignore')
N_days = config.N_days # ็จไบๅคๅฐๅคฉ็ๆฐๆฎ(็ฎๅ17ไธชๅทฅไฝๆฅ)
N_hours = config.N_hours
N_time_slice = config.N_time_slice # 1ๅฐๆถๆ6ไธชๆถ้ด็
N_station = config.N_station # 81ไธช็ซ็น
N_flow = config.N_flow # ่ฟ็ซ & ๅบ็ซ
len_seq1 = config.len_seq1 # weekๆถ้ดๅบๅ้ฟๅบฆไธบ2
len_seq2 = config.len_seq2 # dayๆถ้ดๅบๅ้ฟๅบฆไธบ3
len_seq3 = config.len_seq3 # hourๆถ้ดๅบๅ้ฟๅบฆไธบ5
len_pre = config.pre_step
nb_flow = config.nb_flow # ่พๅ
ฅ็นๅพ
# ่ชๅฎไน็ๆๅคฑๅฝๆฐ
# clipๅฐ่ถ
ๅบๆๅฎ่ๅด็ๆฐๅผบๅถๅไธบ่พน็ๅผ
def my_own_loss_function(y_true, y_pred):
return K.mean(abs(y_true - y_pred)) + 0.01 * K.mean(
abs(K.clip(y_true, 0.001, 1) - K.clip(y_pred, 0.001, 1)) / K.clip(y_true, 0.001, 1)) * K.mean(
abs(K.clip(y_true, 0.001, 1) - K.clip(y_pred, 0.001, 1)) / K.clip(y_true, 0.001, 1))
# ๅญฆไน ็ๆงๅถ
def scheduler(epoch):
# ๆฏ้15ไธชepoch๏ผๅญฆไน ็ๅๅฐไธบๅๆฅ็1/2
if epoch % 15 == 0 and epoch != 0:
lr = K.get_value(model.optimizer.lr)
K.set_value(model.optimizer.lr, lr * 0.5)
print("lr changed to {}".format(lr * 0.5))
return K.get_value(model.optimizer.lr)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ็ป็ปๆฐๆฎโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ็ฑไบ>25็ๆฐ้ๆๅ
ถๅฐ๏ผๅฐ>25็ๅผๅ
จ้ฝ้ป่ฎคไธบ25
# Metro_Flow_Matrix[np.where(Metro_Flow_Matrix > 25)] = 25
# ่ฟๆฏๅฝไธๅ
# ็นๆต/3000
# ่พนๆต/233
node_scale_ratio = 30 # ้ขๅค็็ฌฌไบๆญฅ ็ธๅฝไบไธๅ
ฑๆฏ/3000
Metro_Flow_Matrix /= node_scale_ratio
edge_scale_ratio = 233.0
Metro_Edge_Flow_Matrix /= edge_scale_ratio
# ็ๆ่ฎญ็ปๆ ทๆฌ๏ผไนๅพๅ
ณ้ฎ๏ผ
data, target = generate_x_y(Metro_Flow_Matrix, len_seq1, len_seq2, len_seq3, len_pre) # typeไธบtuple
edge_data, edge_target = generate_x_y(Metro_Edge_Flow_Matrix, len_seq1, len_seq2, len_seq3, len_pre)
# tuple to array
# zip()ๅฝๆฐ็จไบๅฐๅฏ่ฟญไปฃ็ๅฏน่ฑกไฝไธบๅๆฐ๏ผๅฐๅฏน่ฑกไธญๅฏนๅบ็ๅ
็ด ๆๅ
ๆไธไธชไธชๅ
็ป๏ผ็ถๅ่ฟๅ็ฑ่ฟไบๅ
็ป็ปๆ็ๅ่กจใ
week, day, hour = zip(*data)
# ็ฉ้ต่ฝฌๆขไธบarray
node_data_1 = np.array(week)
node_data_2 = np.array(day)
node_data_3 = np.array(hour)
# node_data_3.shape)
week, day, hour = zip(*edge_data)
edge_data_1 = np.array(week)
edge_data_2 = np.array(day)
edge_data_3 = np.array(hour)
target = np.array(target)
edge_target = np.array(edge_target)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ้ๆฐ็ป็ปๆฐๆฎโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๅฐdataๅๅฒๅบrecent\period\trendๆฐๆฎ
length = node_data_1.shape[0]
xr_train = np.zeros([length, N_station, len_seq3 * N_flow])
xp_train = np.zeros([length, N_station, len_seq2 * nb_flow])
xt_train = np.zeros([length, N_station, len_seq1 * nb_flow])
xredge_train = np.zeros([length, N_station, len_seq3 * N_station])
xpedge_train = np.zeros([length, N_station, len_seq2 * N_station])
xtedge_train = np.zeros([length, N_station, len_seq1 * N_station])
# ่ฃ
่ฝฝxr_train, xp_train, xt_train็ญๆ็ปๆ ทๆฌ๏ผๆไปฅlen_seq * nb_flow = 3*2
for i in range(length):
for j in range(len_seq3):
for k in range(2):
# ็ป่ฃ
ๅฝๅๅ5ไธชๆถ้ด็็ฉ้ต
xr_train[i, :, j * 2 + k] = node_data_3[i, j, :, k]
for i in range(length):
for j in range(len_seq2):
for k in range(2):
# ็ป่ฃ
ๅไธๅคฉๅ3ไธชๆถ้ด็็ฉ้ต
xp_train[i, :, j * 2 + k] = node_data_2[i, j, :, k]
for i in range(length):
for j in range(len_seq1):
for k in range(2):
# ็ป่ฃ
ๅไธๅจๅฏนๅบ็ๅ2ไธชๆถ้ด็็ฉ้ต
xt_train[i, :, j * 2 + k] = node_data_1[i, j, :, k]
for i in range(length):
for j in range(len_seq1):
for k in range(81):
xtedge_train[i, :, j * 81 + k] = edge_data_1[i, j, :, k]
for i in range(length):
for j in range(len_seq2):
for k in range(81):
xpedge_train[i, :, j * 81 + k] = edge_data_2[i, j, :, k]
for i in range(length):
for j in range(len_seq3):
for k in range(81):
xredge_train[i, :, j * 81 + k] = edge_data_3[i, j, :, k]
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅ ๅ
ฅๅค้จไฟกๆฏ-ๅจไฟกๆฏโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
DAY = 3 # ่ฎญ็ปๆ ทๆฌไป3ๅทๅผๅง๏ผไธบๅจๅ๏ผๆๅ้่ฆ็งปๅจ5ไธชๆถ้ด็[0,1,2,3,4]
x_external_information1 = np.zeros([(N_days - 1) * N_hours * N_time_slice, 1])
# range(start, stop[, step])
# 0ๅฐ16
for i in range(0, (N_days - 1) * N_hours * N_time_slice, 24 * 6):
# ๆ ่ฎฐๆฏไธชๆ ทๆฌๅฑไบๅชไธๅคฉ
x_external_information1[i:i + 24 * 6, 0] = DAY
# ่ฟ้ไธบไปไนๆฏ5
DAY = (DAY + 1) % 5
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅ ๅ
ฅๅค้จไฟกๆฏ-ๅฐๆถ&ๅ้ไฟกๆฏโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
HOUR = 0 # ไป0ๆถๅปๅผๅง๏ผๆๅ้่ฆ็งปๅจ5ไธชๆถ้ด็[0,1,2,...,24*6-1]
x_external_information2 = np.zeros([(N_days - 1) * N_hours * N_time_slice, 1])
for i in range(0, (N_days - 1) * N_hours * N_time_slice):
x_external_information2[i, 0] = HOUR
HOUR = (HOUR + 1) % (24 * 6)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅ ๅ
ฅๅค้จไฟกๆฏ-ๅคฉๆฐไฟกๆฏโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
x_external_information4 = np.zeros([N_days * N_hours * N_time_slice, 1])
# [ไธญ้จใๅฐ้จใ้ดใๅคไบใๆด] --> ็ฎๅๆ
ๅตไธบ[้จๅคฉ/ๆดๅคฉ] 0/1
# 2ๅท--้ด
x_external_information4[0:24 * 6, 0] = 1
# 3ๅท--ๅฐ้จ/้ด
x_external_information4[24 * 6:2 * 24 * 6, 0] = 0
# 4ๅท--ไธญ้จ/ๅฐ้จ
x_external_information4[2 * 24 * 6:3 * 24 * 6, 0] = 0
# 7ๅท--ๅฐ้จ
x_external_information4[3 * 24 * 6:4 * 24 * 6, 0] = 0
# 8ๅท--ๅฐ้จ/้ด
x_external_information4[4 * 24 * 6:5 * 24 * 6, 0] = 0
# 9ๅท--ไธญ้จ/ๅฐ้จ
x_external_information4[5 * 24 * 6:6 * 24 * 6, 0] = 0
# 10ๅท--ๅฐ้จ
x_external_information4[6 * 24 * 6:7 * 24 * 6, 0] = 0
# 11ๅท--ๅฐ้จ
x_external_information4[7 * 24 * 6:8 * 24 * 6, 0] = 0
# 14ๅท--ๅฐ้จ
x_external_information4[8 * 24 * 6:9 * 24 * 6, 0] = 0
# 15ๅท--ๅฐ้จ
x_external_information4[9 * 24 * 6:10 * 24 * 6, 0] = 0
# 16ๅท--ๅคไบ
x_external_information4[10 * 24 * 6:11 * 24 * 6, 0] = 1
# 17ๅท--ๆด
x_external_information4[11 * 24 * 6:12 * 24 * 6, 0] = 1
# 18ๅท--ๅฐ้จ
x_external_information4[12 * 24 * 6:13 * 24 * 6, 0] = 0
# 21ๅท--ๅคไบ/ๆด
x_external_information4[13 * 24 * 6:14 * 24 * 6, 0] = 1
# 22ๅท--ๆด
x_external_information4[14 * 24 * 6:15 * 24 * 6, 0] = 1
# 23ๅท--ๆด
x_external_information4[15 * 24 * 6:16 * 24 * 6, 0] = 1
# 24ๅท--ๆด
x_external_information4[16 * 24 * 6:17 * 24 * 6, 0] = 1
# 25ๅท--ๅคไบ
# x_external_information4[17*24*6:18*24*6, 3] = 1
# 28ๅท
# x_external_information4[18*24*6:19*24*6, 2] = 1
# ้คๅป2ๅท็ๅคฉๆฐไฟกๆฏ๏ผๆญคๅคๅบ่ฏฅๆฏๅผๅง็ฉ้ตๅคงๅฐๆฒก่ฎพ่ฎกๅฅฝ๏ผๆไปฅ้่ฆๅพๅ็งปๅจ144ไธชๆถ้ด็๏ผ็ถๅๅ็งปๅจ5ไธชๆถ้ด็
x_external_information4 = x_external_information4[24 * 6:, :]
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅ ๅ
ฅๅค้จไฟกๆฏ-้ธๆบๆฐ้โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
external_information5 = np.load('./npy/train_data/Two_more_features.npy')
x_external_information5 = np.zeros([(N_days - 1) * N_hours * N_time_slice, 81])
external_information5 = external_information5[:, 0]
for i in range((N_days - 1) * N_hours * N_time_slice):
x_external_information5[i, :] = external_information5
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅ ๅ
ฅๆฉๆ้ซๅณฐใไธ่ฌ้ซๅณฐใๅนณๅณฐไฟกๆฏโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
x_external_information9 = np.zeros([(N_days - 1) * N_hours * N_time_slice, 1])
# [ๅนณๅณฐใไธ่ฌ้ซๅณฐใๆฉๆ้ซๅณฐ] [0,1,2]
for i in range(0, (N_days - 1) * N_hours * N_time_slice, 24 * 6):
# โโโโโโโโโโโโโโโโโโๆฉๆ้ซๅณฐโโโโโโโโโโโโโโโโโโโโโ
x_external_information9[i + 39:i + 54, 0] = 2 # 7๏ผ30 - 9๏ผ00
x_external_information9[i + 102:i + 114, 0] = 2 # 17๏ผ00 - 19๏ผ00
# โโโโโโโโโโโโโโโโโโ้ซๅณฐโโโโโโโโโโโโโโโโโโโโโโโโโ
x_external_information9[i + 33:i + 39, 0] = 1 # 6:30-7:30
x_external_information9[i + 63:i + 70, 0] = 1 # 10:30-11:30
x_external_information9[i + 99:i + 102, 0] = 1 # 16:30-17:30
x_external_information9[i + 114:i + 132, 0] = 1 # 19:00-22:00
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅฏน้ฝๅค้จไฟกๆฏโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๅฏน้ฝๅค้จไฟกๆฏ
length = node_data_1.shape[0]
x_true_external_information1 = x_external_information1[len(x_external_information1) - length:, :]
x_true_external_information2 = x_external_information2[len(x_external_information2) - length:, :]
# x_true_external_information3 = x_external_information3[5:, :]
x_true_external_information4 = x_external_information4[len(x_external_information4) - length:, :]
x_true_external_information5 = x_external_information5[len(x_external_information5) - length:, :]
x_true_external_information9 = x_external_information9[len(x_external_information9) - length:, :]
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโSHUFFLEโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
indices = np.arange(length)
# ๆไนฑ
np.random.shuffle(indices)
xr_train = xr_train[indices] # learning task: (2302, 81, 10)->(2302, 81, 2)
xp_train = xp_train[indices]
xt_train = xt_train[indices]
xredge_train = xredge_train[indices]
xpedge_train = xpedge_train[indices]
xtedge_train = xtedge_train[indices]
# 1ใ2ใ4ใ9ไธบEmbedding, 5ไธบDense
x_train_external_information1 = x_true_external_information1[indices]
x_train_external_information2 = x_true_external_information2[indices]
# x_true_external_information3 = x_true_external_information3[indices]
x_train_external_information4 = x_true_external_information4[indices]
x_train_external_information5 = x_true_external_information5[indices]
x_train_external_information9 = x_true_external_information9[indices]
target = target[indices]
edge_target = edge_target[indices]
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๆๅปบ้ช่ฏ้ๅ(24-25ๅทๆฐๆฎไฝไธบ้ช่ฏ้)โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๆ้ ๅพๆ็นๅคๆ.....2019.05.22
node_day_24 = np.load('./npy/train_data/day_24.npy')
node_day_25 = np.load('./npy/train_data/day_25.npy')
node_day_18 = np.load('./npy/train_data/raw_node_data.npy')[144 * 12:-144, :, :]
val_node_data = np.concatenate((node_day_18, node_day_24, node_day_25), axis=0)
raw_edge_data = np.load('./npy/train_data/raw_edge_data.npy')
edge_day_24 = raw_edge_data[-144:, :, :]
edge_day_18 = raw_edge_data[144 * 12:-144, :, :]
edge_day_25 = np.load('./npy/train_data/day_25_edge.npy')
val_edge_data = np.concatenate((edge_day_18, edge_day_24, edge_day_25), axis=0)
# normalization
val_node_data /= node_scale_ratio
val_edge_data /= edge_scale_ratio
# ๆๅปบๅฅฝ้ช่ฏ้็ๆ ทๆฌๅๆ ็ญพ
val_node_data, val_node_target = generate_x_y(val_node_data, len_seq1, len_seq2, len_seq3, len_pre)
val_edge_data, val_edge_target = generate_x_y(val_edge_data, len_seq1, len_seq2, len_seq3, len_pre)
# tuple-->array
week, day, hour = zip(*val_node_data)
val_node_data_1 = np.array(week)
val_node_data_2 = np.array(day)
val_node_data_3 = np.array(hour)
week, day, hour = zip(*val_edge_data)
val_edge_data_1 = np.array(week)
val_edge_data_2 = np.array(day)
val_edge_data_3 = np.array(hour)
val_node_target = np.array(val_node_target)
val_edge_target = np.array(val_edge_target)
# # ๅฐdataๅๅฒๅบrecent\period\trendๆฐๆฎ
val_length = val_node_data_1.shape[0]
xr_val = np.zeros([val_length, N_station, len_seq3 * N_flow])
xp_val = np.zeros([val_length, N_station, len_seq2 * N_flow])
xt_val = np.zeros([val_length, N_station, len_seq1 * N_flow])
xredge_val = np.zeros([val_length, N_station, len_seq3 * N_station])
xpedge_val = np.zeros([val_length, N_station, len_seq2 * N_station])
xtedge_val = np.zeros([val_length, N_station, len_seq1 * N_station])
# # ้ๅบst_resnet๏ผ็ฑไบๆฒกๆLSTM๏ผๆไปฅlen_seq * nb_flow = 3*2
for i in range(val_length):
for j in range(len_seq3):
for k in range(2):
xr_val[i, :, j * 2 + k] = val_node_data_3[i, j, :, k]
for i in range(val_length):
for j in range(len_seq2):
for k in range(2):
xp_val[i, :, j * 2 + k] = val_node_data_2[i, j, :, k]
for i in range(val_length):
for j in range(len_seq1):
for k in range(2):
xt_val[i, :, j * 2 + k] = val_node_data_1[i, j, :, k]
for i in range(val_length):
for j in range(len_seq3):
for k in range(81):
xredge_val[i, :, j * 81 + k] = val_edge_data_3[i, j, :, k]
for i in range(val_length):
for j in range(len_seq2):
for k in range(81):
xpedge_val[i, :, j * 81 + k] = val_edge_data_2[i, j, :, k]
for i in range(val_length):
for j in range(len_seq1):
for k in range(81):
xtedge_val[i, :, j * 81 + k] = val_edge_data_1[i, j, :, k]
# โโโโโโโโโโโโโโโโโโโโโโๆทปๅ ้ช่ฏ้ๅค้จไฟกๆฏโโโโโโโโโโโโโโโโโโโโโโโโ
# ้ป่ฎคไบ00:00-00:40็ไบบๆต้ๅไธบ0
# 0125ๆฏๅจไบ,weekdayไฟกๆฏ
x_val_external_information1 = np.zeros([24 * 6, 1])
x_val_external_information1[:, 0] = 4 # ไปฃ่กจๅจไบ
# ๆถ้ด็ไฟกๆฏ
x_val_external_information2 = np.zeros([24 * 6, 1])
HOUR = 0
for i in range(0, 24 * 6):
x_val_external_information2[i, 0] = HOUR
HOUR = HOUR + 1
# ๅคฉๆฐไฟกๆฏ,25ๅทไธบๅคไบ
x_val_external_information4 = np.zeros([24 * 6, 1])
x_val_external_information4[:, 0] = 1
# ้ธๆบไฟกๆฏ
x_val_external_information5 = np.zeros([24 * 6, 81])
t = np.load('./npy/train_data/Two_more_features.npy')
t = t[:, 0]
for i in range(144):
x_val_external_information5[i, :] = t
# ๆฉๆ้ซๅณฐใ้ซๅณฐใๅนณๅณฐไฟกๆฏ
x_val_external_information9 = np.zeros([N_hours * N_time_slice, 1])
# #โโโโโโโโโโโโโโโโโโๆฉๆ้ซๅณฐโโโโโโโโโโโโโโโโโโโโโ
x_val_external_information9[39:54, 0] = 2 # 7๏ผ30 - 9๏ผ00
x_val_external_information9[102:114, 0] = 2 # 17๏ผ00 - 19๏ผ00
# #โโโโโโโโโโโโโโโโโโ้ซๅณฐโโโโโโโโโโโโโโโโโโโโโโโโโ
x_val_external_information9[33:39, 0] = 1 # 6:30-7:30
x_val_external_information9[63:70, 0] = 1 # 10:30-11:30
x_val_external_information9[99:102, 0] = 1 # 16:30-17:30
x_val_external_information9[114:132, 0] = 1 # 19:00-22:00
# #------------val external informationๅพๅ็งปไธไธชๆถ้ด็------------------
x_val_external_information1 = x_val_external_information1[len(x_val_external_information1) - len(xr_val):, :]
x_val_external_information2 = x_val_external_information2[len(x_val_external_information2) - len(xr_val):, :]
x_val_external_information4 = x_val_external_information4[len(x_val_external_information4) - len(xr_val):, :]
x_val_external_information5 = x_val_external_information5[len(x_val_external_information5) - len(xr_val):, :]
x_val_external_information9 = x_val_external_information9[len(x_val_external_information9) - len(xr_val):, :]
# ้ๆฐreshapeไธไธ
val_node_target = val_node_target.reshape(
[val_node_target.shape[0], val_node_target.shape[1] * val_node_target.shape[2], val_node_target.shape[3]])
target = target.reshape([target.shape[0], target.shape[1] * target.shape[2], target.shape[3]])
edge_target = edge_target.reshape(
[edge_target.shape[0], edge_target.shape[1] * edge_target.shape[2], edge_target.shape[3]])
val_edge_target = val_edge_target.reshape(
[val_edge_target.shape[0], val_edge_target.shape[1] * val_edge_target.shape[2], val_edge_target.shape[3]])
# ่ฟ้ๅผๅง่ทๆจกๅ๏ผ็ฅ็ป็ฝ็ป็ธๅ
ณ็๏ผ้็น็่ฟ้
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโๅปบ็ซๆจกๅโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
model = stresnet_multi_step_pre(c_conf=(len_seq3, N_flow, N_station), p_conf=(len_seq2, N_flow, N_station),
t_conf=(len_seq1, N_flow, N_station), # ็ซ็นๆต้input
c1_conf=(len_seq3, N_station, N_station), p1_conf=(len_seq2, N_station, N_station),
t1_conf=(len_seq1, N_station, N_station), # ่พนๆต้input
external_dim1=1, external_dim2=1, external_dim3=None, # ๅค้จไฟกๆฏ
external_dim4=1, external_dim5=81, external_dim6=None,
external_dim7=None, external_dim8=None, external_dim9=1,
nb_residual_unit=4, nb_edge_residual_unit=4, pre_step=len_pre) # ่ฟ้็unitไปฃ่กจไบๅคงไฝ็็ฝ็ปๆทฑๅบฆ
model.compile(optimizer='adam',
loss={'node_logits': my_own_loss_function, 'edge_logits': my_own_loss_function},
loss_weights={'node_logits': 1, 'edge_logits': 0.2}, # ไธคไธชไปปๅกๅ่ช็ๆ้
metrics=['accuracy'])
filepath = config.path_stresnet_multi_step_pre + "{epoch:02d}-{node_logits_loss:.8f}.hdf5"
if not os.path.exists(config.path_stresnet_multi_step_pre):
os.makedirs(config.path_stresnet_multi_step_pre)
# ไธญ้่ฎญ็ปๆๆๆๅ, ๅๅฐๆไปถไฟๅญ, ๆฏๆๅไธๆฌก, ไฟๅญไธๆฌก
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True,
mode='min')
reduce_lr = LearningRateScheduler(scheduler)
early_stopping = EarlyStopping(monitor='val_loss', patience=30, verbose=0, mode='min')
callbacks_list = [checkpoint, reduce_lr, early_stopping]
K.set_value(model.optimizer.lr, 0.0001)
# ่พๅ
ฅ็ไธ่ฅฟ่ฆๆณจๆ
history = model.fit([xr_train, xp_train, xt_train, xredge_train, xpedge_train, xtedge_train,
x_train_external_information1, x_train_external_information2, x_train_external_information4,
x_train_external_information5, x_train_external_information9],
[target, edge_target],
validation_data=([xr_val, xp_val, xt_val, xredge_val, xpedge_val, xtedge_val,
x_val_external_information1, x_val_external_information2,
x_val_external_information4, x_val_external_information5,
x_val_external_information9],
[val_node_target, val_edge_target]),
batch_size=config.batch_size, epochs=config.epochs, callbacks=callbacks_list)
pd.DataFrame(columns=['loss'], data=history.history['loss']).to_csv(config.loss_acc_csvFile, index=None)
if __name__ == '__main__':
# --------------------------------stresnet่ฎญ็ป-start---------------------------------------#
# ็ดๆฅไป่ฟ้ๅผๅง็๏ผ็จๅบ็ๅ
ฅๅฃ๏ผๅ ่ฝฝ็ป่ฎกๅฅฝ็ๆต้ๆไปถ
Metro_Flow_Matrix = np.load('./npy/train_data/raw_node_data.npy') # shape=(2448, 81, 2)
Metro_Edge_Flow_Matrix = np.load('./npy/train_data/raw_edge_data.npy') # shape=(2448, 81, 81)
train_stresnet(Metro_Flow_Matrix, Metro_Edge_Flow_Matrix)
# --------------------------------stresnet่ฎญ็ป-end---------------------------------------#