-
Notifications
You must be signed in to change notification settings - Fork 1
/
non_convolution_based_network.py
169 lines (132 loc) · 5.2 KB
/
non_convolution_based_network.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
import gc
import time
import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import LSTM, Dense, Flatten
from utils import *
physical_devices = tf.config.experimental.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(physical_devices[0], True)
LEARNING_RATE = 0.0001
EPOCH = 200
def model_ann(input_shape, hidden_shape, X_train, y_train, X_test):
input = keras.Input(shape=input_shape)
h = Flatten()(input)
h = Dense(hidden_shape)(h)
output = Dense(input_shape[1])(h)
model = keras.Model(inputs=input, outputs=output)
model.summary()
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE),
loss="mean_squared_error",
metrics=["mse", "mae"],
)
log_dir = "logs\\ANN" + str(t)
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir=log_dir,
histogram_freq=1,
write_images=True,
)
early_stopping = EarlyStopping(monitor="val_loss", min_delta=0.000001, patience=10)
model.fit(
X_train,
y_train,
epochs=EPOCH,
validation_split=0.25,
callbacks=[early_stopping, tensorboard_callback],
)
preds = model.predict(X_test)
del model, input, output, h
gc.collect()
return preds
def model_lstm(input_shape, hidden_shape, X_train, y_train, X_test):
input = keras.Input(shape=input_shape)
h = LSTM(hidden_shape, return_sequences=True)(input)
output = LSTM(input_shape[1])(h)
model = keras.Model(inputs=input, outputs=output)
model.summary()
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE),
loss="mean_squared_error",
metrics=["mse", "mae"],
)
log_dir = "E:\\logs\\LSTM" + str(t)
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir=log_dir, histogram_freq=1, write_images=True
)
early_stopping = EarlyStopping(monitor="val_loss", min_delta=0.000001, patience=30)
model.fit(
X_train,
y_train,
epochs=EPOCH,
validation_split=0.25,
verbose=2,
callbacks=[early_stopping, tensorboard_callback],
)
preds = model.predict(X_test)
del model, input, output, h
gc.collect()
return preds
if __name__ == "main":
model = {"ANN": model_ann, "LSTM": model_lstm}
for key in model.keys():
times = [60] # 10, 15, 20, 30, 60
for t in times:
print("time interval:", t)
path = "Docked_" + str(t) + "/"
time_inter = int(24 * 60 / t)
train = time_inter * 16
test = time_inter * 5
time_step = int(2 * 60 / t)
raw_data = np.load(path + "Docked_Station_adj_weight" + str(t) + ".npy")
shape = raw_data.shape[1]
raw_data_x = np.sum(raw_data, axis=1).reshape(-1, shape, 1)
raw_data_y = np.sum(raw_data, axis=2).reshape(-1, shape, 1)
raw_data = np.concatenate((raw_data_x, raw_data_y), axis=2)
raw_data = raw_data.reshape((raw_data.shape[0], -1))
del raw_data_x, raw_data_y
gc.collect()
raw_data_sqrt = np.sqrt(raw_data)
diff_normal = difference(raw_data_sqrt, 1)
diff_season = difference(diff_normal, time_inter)
seq_train = diff_season[:train]
seq_test = diff_season[train:]
scaler = MinMaxScaler(feature_range=(0, 1))
seq_train = scaler.fit_transform(seq_train)
seq_test = scaler.transform(seq_test)
X_train, y_train = seq_to_training_data(seq_train, time_step)
X_test, y_test = seq_to_training_data(seq_test, time_step)
RMSE = []
MAE = []
Running_Time = []
for j in range(10):
t0 = time.process_time()
preds = model[key](
(time_step, X_train.shape[-1]), shape, X_train, y_train, X_test
)
total_running_time = time.process_time() - t0
preds = scaler.inverse_transform(preds)
preds = inverse_difference(diff_normal[train:], preds, time_inter)
preds = inverse_difference(raw_data[train:], preds, 1)
preds[preds < 0] = 0
k = preds[0]
preds[:-1] = preds[1:]
preds[-1] = k
preds = np.around(preds)
rmse = np.sqrt(mean_squared_error(raw_data[train:], preds))
mae = mean_absolute_error(raw_data[train:], preds)
RMSE.append(rmse)
MAE.append(mae)
Running_Time.append(total_running_time)
del raw_data, raw_data_sqrt
del diff_normal, diff_season
del seq_train, seq_test, scaler
del X_train, y_train, X_test, y_test
del preds, k
gc.collect()
open(path + f"Result_{t}.csv", "a").write(
f"{key},{np.average(RMSE)},{np.average(MAE)},{np.average(Running_Time)}\n"
)