-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgnn_LSTM.py
More file actions
235 lines (185 loc) · 9.36 KB
/
Copy pathgnn_LSTM.py
File metadata and controls
235 lines (185 loc) · 9.36 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 20:29:05 2020
@author: guotong
"""
import pandas as pd
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras import optimizers
from keras.layers import LSTM, Dense, Activation, Dropout
from keras import Input
from keras.models import Model, Sequential
from keras.regularizers import l2
from keras.optimizers import Adam
import keras.backend as K
import numpy as np
from keras_dgl.layers import GraphAttentionCNN
from utils import *
from keras_dgl.layers import GraphCNN
# convert series to supervised learning
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = list(), list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
# put it all together
agg = concat(cols, axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
def load_data(data_filepath=None, label_filepath=None):
# load dataset
dataset = read_csv(data_filepath, header=None, index_col=None)
label = read_csv(label_filepath, header=None, index_col=None)
dataset = pd.concat([dataset, label], axis=1)
values = dataset.values
# ensure all data is float
values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# specify the number of lag hours
n_min = 15
n_features = 31
# frame as supervised learning
reframed = series_to_supervised(scaled, n_min, 1)
print(reframed.shape) # 448 = 16 * 28
# split into train and test sets
values = reframed.values
# n_train_hours = 650
# train = values[:n_train_hours, :]
# test = values[n_train_hours:, :]
# split into input and outputs
n_obs = n_min * n_features
# train_X, train_y = train[:, :n_obs], train[:, -4:-1]
# test_X, test_y = test[:, :n_obs], test[:, -4:-1]
# print(train_X.shape, len(train_X), train_y.shape)
X, y = values[:, :n_obs], values[:, -4:-1]
# # reshape input to be 3D [samples, timesteps, features]
# train_X = train_X.reshape((train_X.shape[0], n_min, n_features))
# test_X = test_X.reshape((test_X.shape[0], n_min, n_features))
# print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
# return train_X, train_y, test_X, test_y
return X, y
def get_data():
# CD01_train_x, CD01_train_y, CD01_test_x, CD01_test_y = load_data('/home/guotong/libiyue/additional/CD01.csv', '/home/guotong/libiyue/additional/CD01_status.csv')
# CD02_train_x, CD02_train_y, CD02_test_x, CD02_test_y = load_data('/home/guotong/libiyue/additional/CD02.csv', '/home/guotong/libiyue/additional/CD02_status.csv')
# CD04_train_x, CD04_train_y, CD04_test_x, CD04_test_y = load_data('/home/guotong/libiyue/additional/CD04.csv', '/home/guotong/libiyue/additional/CD04_status.csv')
# GY01_train_x, GY01_train_y, GY01_test_x, GY01_test_y = load_data('/home/guotong/libiyue/additional/GY01.csv', '/home/guotong/libiyue/additional/GY01_status.csv')
# GY02_train_x, GY02_train_y, GY02_test_x, GY02_test_y = load_data('/home/guotong/libiyue/additional/GY02.csv', '/home/guotong/libiyue/additional/GY02_status.csv')
# KM03_train_x, KM03_train_y, KM03_test_x, KM03_test_y = load_data('/home/guotong/libiyue/additional/KM03.csv', '/home/guotong/libiyue/additional/KM03_status.csv')
# train_x = np.concatenate((CD01_train_x, CD02_train_x, CD04_train_x, GY01_train_x, GY02_train_x, KM03_train_x), axis=0)
# train_y = np.concatenate((CD01_train_y, CD02_train_y, CD04_train_y, GY01_train_y, GY02_train_y, KM03_train_y), axis=0)
# test_x = np.concatenate((CD01_test_x, CD02_test_x, CD04_test_x, GY01_test_x, GY02_test_x, KM03_test_x), axis=0)
# test_y = np.concatenate((CD01_test_y, CD02_test_y, CD04_test_y, GY01_test_y, GY02_test_y, KM03_test_y), axis=0)
X_CD01, y_CD01 = load_data('/home/guotong/libiyue/additional/CD01.csv', '/home/guotong/libiyue/additional/CD01_status.csv')
X_CD02, y_CD02 = load_data('/home/guotong/libiyue/additional/CD02.csv', '/home/guotong/libiyue/additional/CD02_status.csv')
X_CD04, y_CD04 = load_data('/home/guotong/libiyue/additional/CD04.csv', '/home/guotong/libiyue/additional/CD04_status.csv')
X_GY01, y_GY01 = load_data('/home/guotong/libiyue/additional/GY01.csv', '/home/guotong/libiyue/additional/GY01_status.csv')
X_GY02, y_GY02 = load_data('/home/guotong/libiyue/additional/GY02.csv', '/home/guotong/libiyue/additional/GY02_status.csv')
X_KM03, y_KM03 = load_data('/home/guotong/libiyue/additional/KM03.csv', '/home/guotong/libiyue/additional/KM03_status.csv')
X = np.concatenate((X_CD01, X_CD02, X_CD04, X_GY01, X_GY02, X_KM03), axis=0)
y = np.concatenate((y_CD01, y_CD02, y_CD04, y_GY01, y_GY02, y_KM03), axis=0)
# return train_x, train_y, test_x, test_y
return X, y
def get_adj_matrix(x):
adj_matrix = np.zeros((x.shape[0], x.shape[0]))
adj_matrix[0:650, 0:2600] = 1 # CD01
adj_matrix[650:1300, :] = 1 # CD02
adj_matrix[1300:1950, 0:1950] = 1 # CD04
adj_matrix[1950:2600, 0:1300] = 1 # GY01
adj_matrix[1950:2600, 1950:3250] = 1
adj_matrix[2600:3250, 650:1300] = 1 # GY02
adj_matrix[2600:3250, 1950:3900] = 1
adj_matrix[3250:3900, 650:1300] = 1 # KM03
adj_matrix[3250:3900, 2600:3900]
return adj_matrix
# X_train, Y_train, X_test, Y_test = get_data()
# X_train = X_train.reshape(X_train.shape[0], -1)
# X_test = X_test.reshape(X_test.shape[0], -1)
X, Y = get_data()
A = get_adj_matrix(X)
# X, A, Y = load_data(dataset='cora')
# A = np.array(A.todense())
_, Y_val, _, train_idx, val_idx, test_idx, train_mask = get_splits(Y)
train_idx = np.array(train_idx)
val_idx = np.array(val_idx)
test_idx = np.array(test_idx)
labels = np.argmax(Y, axis=1) + 1
# # Normalize X
# X /= X.sum(1).reshape(-1, 1)
# X = np.array(X)
Y_train = np.zeros(Y.shape)
labels_train = np.zeros(labels.shape)
Y_train[train_idx] = Y[train_idx]
labels_train[train_idx] = labels[train_idx]
Y_test = np.zeros(Y.shape)
labels_test = np.zeros(labels.shape)
Y_test[test_idx] = Y[test_idx]
labels_test[test_idx] = labels[test_idx]
# print(X.shape, Y_train.shape)
# # Build Graph Convolution filters
# SYM_NORM = True
# # A_norm = preprocess_adj_numpy(A, SYM_NORM)
# A_norm = A
# num_filters = 2
# graph_conv_filters = np.concatenate([A_norm, np.matmul(A_norm, A_norm)], axis=0)
# graph_conv_filters = K.constant(graph_conv_filters)
# # Build Model
# input_tensor = Input(shape=(X.shape[1],))
# x = GraphCNN(16, num_filters, graph_conv_filters, input_shape=(X.shape[1],), activation='elu', kernel_regularizer=l2(5e-4))(input_tensor)
# x = Dropout(0.2)(x)
# x = GraphCNN(16, num_filters, graph_conv_filters, activation='elu', kernel_regularizer=l2(5e-4))(x)
# print(x.shape)
# output_tensor = Dense(Y.shape[1], activation='softmax')(x)
# model = Model(input_tensor, output_tensor)
# model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.01), metrics=['acc'])
# model.summary()
# nb_epochs = 200
# for epoch in range(nb_epochs):
# model.fit(X, Y_train, sample_weight=train_mask, batch_size=A.shape[0], epochs=1, shuffle=False, verbose=0)
# Y_pred = model.predict(X, batch_size=A.shape[0])
# _, train_acc = evaluate_preds(Y_pred, [Y_train], [train_idx])
# _, test_acc = evaluate_preds(Y_pred, [Y_test], [test_idx])
# print("Epoch: {:04d}".format(epoch), "train_acc= {:.4f}".format(train_acc[0]), "test_acc= {:.4f}".format(test_acc[0]))
# model.save_weights('gnn.h5')
# build graph_conv_filters
num_filters = 3
graph_conv_filters = np.concatenate([np.eye(A.shape[0]), A, np.matmul(A, A)], axis=0)
graph_conv_filters = K.constant(graph_conv_filters)
# build model
model = Sequential()
model.add(Dropout(0.6, input_shape=(X.shape[1],)))
model.add(GraphAttentionCNN(8, A, num_filters, graph_conv_filters, num_attention_heads=8, attention_combine='concat', attention_dropout=0.6, activation='elu', kernel_regularizer=l2(5e-4)))
model.add(Dropout(0.6))
model.add(GraphAttentionCNN(Y.shape[1], A, num_filters, graph_conv_filters, num_attention_heads=1, attention_combine='average', attention_dropout=0.6, activation='elu', kernel_regularizer=l2(5e-4)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.005), metrics=['accuracy'])
NB_EPOCH = 100
for epoch in range(1, NB_EPOCH + 1):
model.fit(X, Y_train, sample_weight=train_mask, batch_size=A.shape[0], epochs=1, shuffle=False, verbose=0)
Y_pred = model.predict(X, batch_size=A.shape[0])
_, train_acc = evaluate_preds(Y_pred, [Y_train], [train_idx])
_, test_acc = evaluate_preds(Y_pred, [Y_test], [test_idx])
print("Epoch: {:04d}".format(epoch), "train_acc= {:.4f}".format(train_acc[0]), "test_acc= {:.4f}".format(test_acc[0]))
model.save_weights('gnn.h5')