-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_predictor.py
175 lines (131 loc) · 6.1 KB
/
neural_predictor.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
import tensorflow as tf
from tensorflow.keras import Model, Input
from tensorflow.keras.layers import Dense, Average, Dropout, Conv1D, MaxPool1D, GlobalAveragePooling1D, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.regularizers import l2
from tensorflow.keras.experimental import CosineDecay
from spektral.layers import GCNConv, GlobalAvgPool
N_to_D = {
43: 48 ,
172: 144,
86: 72,
334: 210,
129: 96,
860: 320
}
def classifier(train_data, labels, N=172, D=0, n_gcn=3, n_hidden_fc=[128], init_lr=0.0002, dropout_rate=0.1, weight_decay=0.001, n_epochs=300, batch_size=10):
tf.keras.backend.clear_session()
"""
**Input**
- Node features of shape `([batch], n_nodes, n_node_features)`;
- Modified Laplacian of shape `([batch], n_nodes, n_nodes)`; can be computed with
`spektral.utils.convolution.gcn_filter`.
"""
D = D if D > 0 else N_to_D[N] # constant for all layers
n_nodes, n_dim = train_data[0].shape[1:]
V0 = Input(shape=[n_nodes, n_dim])
A = Input(shape=[n_nodes, n_nodes])
AT = Input(shape=[n_nodes, n_nodes])
V = V0
for l in range(n_gcn):
V1 = GCNConv(D, activation='relu', kernel_regularizer=l2(weight_decay))([V, A])
V2 = GCNConv(D, activation='relu', kernel_regularizer=l2(weight_decay))([V, AT])
V = Average()([V1, V2])
fc_in = GlobalAvgPool()(V)
for units in n_hidden_fc:
fc_in = Dense(units, kernel_regularizer=l2(weight_decay))(fc_in)
fc_in = Dropout(dropout_rate)(fc_in)
y = Dense(1, activation='sigmoid', kernel_regularizer=l2(weight_decay))(fc_in)
model = Model(inputs=[V0, A, AT], outputs=y)
lr = CosineDecay(init_lr, 10000)
model.compile(optimizer=Adam(learning_rate=lr), loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, labels, epochs=n_epochs, batch_size=batch_size, verbose=0)
return model
def ss_sigmoid(x):
x = tf.sigmoid(x) * 100 + 10
x = tf.where(x >= 100., 100., x)
return x
def s_sigmoid(x):
x = tf.sigmoid(x) * 100
x = tf.where(x >= 100., 100., x)
return x
def regressor(train_data, labels, N=172, D=0, n_gcn=3, n_hidden_fc=[128], init_lr=0.0001, dropout_rate=0.1, weight_decay=0.001, n_epochs=300, batch_size=10, mode='accuracy', is_shift=True):
tf.keras.backend.clear_session()
"""
**Input**
- Node features of shape `([batch], n_nodes, n_node_features)`;
- Modified Laplacian of shape `([batch], n_nodes, n_nodes)`; can be computed with
`spektral.utils.convolution.gcn_filter`.
"""
D = D if D > 0 else N_to_D[N] # constant for all layers
n_nodes, n_dim = train_data[0].shape[1:]
V0 = Input(shape=[n_nodes, n_dim])
A = Input(shape=[n_nodes, n_nodes])
AT = Input(shape=[n_nodes, n_nodes])
V = V0
for l in range(n_gcn):
V1 = GCNConv(D, activation='relu', kernel_regularizer=l2(weight_decay))([V, A])
V2 = GCNConv(D, activation='relu', kernel_regularizer=l2(weight_decay))([V, AT])
V = Average()([V1, V2])
fc_in = GlobalAvgPool()(V)
for units in n_hidden_fc:
fc_in = Dense(units, kernel_regularizer=l2(weight_decay))(fc_in)
fc_in = Dropout(dropout_rate)(fc_in)
if mode == 'accuracy':
y = Dense(1, activation=ss_sigmoid if is_shift else s_sigmoid, kernel_regularizer=l2(weight_decay))(fc_in)
elif mode == 'loss':
y = Dense(1, activation='relu', kernel_regularizer=l2(weight_decay))(fc_in)
model = Model(inputs=[V0, A, AT], outputs=y)
lr = CosineDecay(init_lr, 10000)
model.compile(optimizer=Adam(learning_rate=lr), loss='mse')
model.fit(x=train_data, y=labels, epochs=n_epochs, batch_size=batch_size, verbose=0)
return model
def regressor_mlp(train_data, labels, N=172, n_gcn=3, n_hidden_fc=[128], init_lr=0.0001, dropout_rate=0.1, weight_decay=0.001, n_epochs=300, batch_size=10):
tf.keras.backend.clear_session()
"""
**Input**
- Node features of shape `([batch], n_nodes, n_node_features)`;
- Modified Laplacian of shape `([batch], n_nodes, n_nodes)`; can be computed with
`spektral.utils.convolution.gcn_filter`.
"""
D = N_to_D[N] # constant for all layers
n_nodes, n_dim = train_data.shape[1:]
V0 = Input(shape=[n_nodes, n_dim])
V = Flatten()(V0)
for l in range(n_gcn):
V = Dense(D, activation='relu', kernel_regularizer=l2(weight_decay))(V)
fc_in = V
for units in n_hidden_fc:
fc_in = Dense(units, kernel_regularizer=l2(weight_decay))(fc_in)
fc_in = Dropout(dropout_rate)(fc_in)
y = Dense(1, activation=ss_sigmoid, kernel_regularizer=l2(weight_decay))(fc_in)
model = Model(inputs=V0, outputs=y)
lr = CosineDecay(init_lr, 10000)
model.compile(optimizer=Adam(learning_rate=lr), loss='mse')
model.fit(train_data, labels, epochs=n_epochs, batch_size=batch_size, verbose=0)
return model
def regressor_cnn(train_data, labels, N=172, n_gcn=3, n_hidden_fc=[128], init_lr=0.0001, dropout_rate=0.1, weight_decay=0.001, n_epochs=300, batch_size=10):
tf.keras.backend.clear_session()
"""
**Input**
- Node features of shape `([batch], n_nodes, n_node_features)`;
- Modified Laplacian of shape `([batch], n_nodes, n_nodes)`; can be computed with
`spektral.utils.convolution.gcn_filter`.
"""
D = N_to_D[N] # constant for all layers
n_nodes, n_dim = train_data.shape[1:]
V0 = Input(shape=[n_nodes, n_dim])
V = V0
for l in range(n_gcn):
V = Conv1D(D, kernel_size=3, activation='relu', padding='same', kernel_regularizer=l2(weight_decay))(V)
V = MaxPool1D(2, padding='same')(V)
fc_in = GlobalAveragePooling1D()(V)
for units in n_hidden_fc:
fc_in = Dense(units, kernel_regularizer=l2(weight_decay))(fc_in)
fc_in = Dropout(dropout_rate)(fc_in)
y = Dense(1, activation=ss_sigmoid, kernel_regularizer=l2(weight_decay))(fc_in)
model = Model(inputs=V0, outputs=y)
lr = CosineDecay(init_lr, 10000)
model.compile(optimizer=Adam(learning_rate=lr), loss='mse')
model.fit(train_data, labels, epochs=n_epochs, batch_size=batch_size, verbose=0)
return model