-
Notifications
You must be signed in to change notification settings - Fork 1
/
mwis_gcn_call_twin.py
executable file
·291 lines (254 loc) · 10.7 KB
/
mwis_gcn_call_twin.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
# python3
# Make this standard template for testing and training
from __future__ import division
from __future__ import print_function
import sys
import os
import shutil
sys.path.append( '%s/gcn' % os.path.dirname(os.path.realpath(__file__)) )
import time
import random
import scipy.io as sio
import numpy as np
import scipy.sparse as sp
from multiprocessing import Queue
from copy import deepcopy
import networkx as nx
from scipy.stats.stats import pearsonr
import tensorflow as tf
from collections import deque
import warnings
warnings.filterwarnings('ignore')
from gcn.utils import *
from runtime_config import flags, FLAGS
# Settings (FLAGS)
from heuristics import *
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Dropout, Input
from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam, SGD, schedules
from tensorflow.keras.regularizers import l2
from spektral.data.loaders import SingleLoader
from spektral.datasets.citation import Citation
from spektral.layers import ChebConv
from spektral.transforms import LayerPreprocess
from solver_base_tf2 import Solver
tf.config.run_functions_eagerly(True)
class DQNAgent(Solver):
def __init__(self, input_flags, memory_size=5000):
super(DQNAgent, self).__init__(input_flags, memory_size)
self.flags = input_flags
self.num_supports = 1 + self.flags.max_degree
self.l2_reg = 5e-4
self.model = self._build_model()
self.memory_crt = deque(maxlen=memory_size)
self.mse = MeanSquaredError()
self.critic = self._build_critic(num_layer=5)
def _build_model(self):
# Neural Net for Actor Model
x_in = Input(shape=(self.feature_size,), dtype=tf.float64, name="x_in")
a_in = Input((None, ), sparse=True, dtype=tf.float64, name="a_in")
gc_l = x_in
for l in range(self.flags.num_layer):
if l < self.flags.num_layer - 1:
act = "leaky_relu"
output_dim = self.flags.hidden1
else:
act = "leaky_relu"
output_dim = self.flags.diver_num
do_l = Dropout(self.flags.dropout, dtype='float64')(gc_l)
gc_l = ChebConv(
output_dim, K=self.num_supports, activation=act,
kernel_regularizer=l2(self.l2_reg),
use_bias=False,
dtype='float64'
)([do_l, a_in])
# gc_l = (gc_l - tf.reduce_mean(gc_l))/(6*tf.math.reduce_std(gc_l)) + 0.5
# gc_l = (gc_l - tf.reduce_mean(gc_l)) + 0.5
# Build model
model = Model(inputs=[x_in, a_in], outputs=gc_l)
if self.flags.learning_decay == 1.0:
self.optimizer = Adam(learning_rate=self.learning_rate)
else:
lr_schedule = schedules.ExponentialDecay(
initial_learning_rate=self.learning_rate,
decay_steps=200,
decay_rate=self.flags.learning_decay)
self.optimizer = Adam(learning_rate=lr_schedule)
model.summary()
return model
def _build_critic(self, num_layer=3, dropout=0.0):
# Neural Net for Critic Model
x_in = Input(shape=(self.flags.diver_num+1,), dtype=tf.float64, name="x_in")
a_in = Input((None, ), sparse=True, dtype=tf.float64, name="a_in")
gc_l = (x_in - tf.reduce_mean(x_in))/(6*tf.math.reduce_std(x_in)) + 0.5
# gc_l = x_in
for l in range(num_layer):
if l < num_layer - 1:
act = "leaky_relu"
output_dim = 64
else:
act = "softmax"
output_dim = 2
do_l = Dropout(dropout, dtype='float64')(gc_l)
gc_l = ChebConv(
output_dim, K=self.num_supports, activation=act,
kernel_regularizer=l2(self.l2_reg),
use_bias=True,
dtype='float64'
)([do_l, a_in])
# Build model
model = Model(inputs=[x_in, a_in], outputs=gc_l)
if self.flags.learning_decay == 1.0:
self.opt_crt = Adam(learning_rate=0.0001)
else:
lr_schedule = schedules.ExponentialDecay(
initial_learning_rate=0.0001,
decay_steps=200,
decay_rate=self.flags.learning_decay)
self.opt_crt = Adam(learning_rate=lr_schedule)
model.summary()
return model
def load_critic(self, name):
ckpt = tf.train.latest_checkpoint(name)
if ckpt:
self.critic.load_weights(ckpt)
print('Critic loaded ' + ckpt)
def load(self, name):
ckpt = tf.train.latest_checkpoint(name)
if ckpt:
self.model.load_weights(ckpt)
print('Actor loaded ' + ckpt)
def save(self, checkpoint_path):
self.model.save_weights(checkpoint_path)
def save_critic(self, checkpoint_path):
self.critic.save_weights(checkpoint_path)
def makestate(self, adj, wts_nn):
reduced_nn = wts_nn.shape[0]
features = np.ones([reduced_nn, self.feature_size])
# features = np.multiply(np.ones([reduced_nn, self.feature_size]), wts_nn)
support = simple_polynomials(adj, self.flags.max_degree)
state = {"features": features, "support": support[1]}
return state
def memorize_crt(self, grad, loss, reward):
self.memory_crt.append((grad.copy(), loss, reward))
def predict(self, state):
x_in = tf.convert_to_tensor(state["features"], dtype=tf.float64)
coord, values, shape = state["support"]
a_in = tf.sparse.SparseTensor(coord, values, shape)
act_values = self.model([x_in, a_in])
return act_values, np.argmax(act_values.numpy())
def act(self, state, train, explore=0.0):
act_values, action = self.predict(state)
if explore > 0.001:
noise = tf.random.uniform(act_values.shape, -explore, explore, dtype=act_values.dtype)
act_values += noise
return act_values, action # returns action
def predict_critic(self, z_out, state):
x_in = tf.convert_to_tensor(state["features"], dtype=tf.float64)
coord, values, shape = state["support"]
a_in = tf.sparse.SparseTensor(coord, values, shape)
x_in = tf.concat([x_in, z_out], axis=1)
sch_pred = self.critic([x_in, a_in])
return sch_pred
def replay(self, batch_size):
if len(self.memory) < batch_size:
return float('NaN')
self.reward_mem.clear()
minibatch = random.sample(self.memory, batch_size)
losses = []
for grad, _, _, loss, _ in minibatch:
self.optimizer.apply_gradients(zip(grad, self.model.trainable_weights))
losses.append(loss)
self.memory.clear()
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
return np.nanmean(losses)
def replay_crt(self, batch_size):
if len(self.memory_crt) < batch_size:
return float('NaN')
minibatch = random.sample(self.memory_crt, batch_size)
losses = []
for grad, loss, _ in minibatch:
self.opt_crt.apply_gradients(zip(grad, self.critic.trainable_weights))
losses.append(loss)
self.memory_crt.clear()
return np.nanmean(losses)
def solve_mwis(self, adj_0, wts_0, train=False, grd=1.0):
"""
GCN followed by LGS
"""
adj = adj_0.copy()
wts_nn = np.reshape(wts_0, (wts_0.shape[0], self.flags.feature_size))
# GCN
state = self.makestate(adj, wts_nn)
act_vals_t, act = self.act(state, train)
act_vals = act_vals_t.numpy()
if self.flags.predict == 'mwis':
gcn_wts = np.multiply(act_vals.flatten(), wts_nn.flatten())
else:
gcn_wts = act_vals.flatten()
mwis, _ = local_greedy_search(adj, gcn_wts)
solu = list(mwis)
mwis_rt = mwis
total_wt = np.sum(wts_nn[solu, 0])
return mwis_rt, total_wt
def foo_train(self, adj_0, wts_0, train=False):
adj = adj_0.copy()
nn = wts_0.shape[0]
wts_nn = np.reshape(wts_0, (nn, FLAGS.feature_size))
ones = np.ones_like(wts_nn)
# GCN
with tf.GradientTape() as g:
g.watch(self.model.trainable_weights)
state = self.makestate(adj, ones)
act_val, act = self.act(state, train, explore=0.03)
act_val_norm = act_val
# act_val_norm = 0.5 + (act_val - tf.reduce_mean(act_val))
sch_pred = self.predict_critic(act_val_norm, state)
regularization_loss = tf.reduce_sum(self.model.losses)
obj_fn = -tf.reduce_mean(sch_pred[:, 0])
obj_fn = obj_fn + regularization_loss
if train: # place gradient computing outside GradientTape
gradients = g.gradient(obj_fn, self.model.trainable_weights)
self.memorize(gradients, [], [], obj_fn.numpy(), 0)
return state, act_val
def predict_train(self, adj_0, zs_0, state, n_samples=1, z_std=0.15):
"""
GCN followed by LGS
wts_0: topology weighted utility
"""
adj = adj_0.copy()
zs_nn = zs_0.numpy()
nn = zs_nn.shape[0]
# GCN
with tf.GradientTape() as g:
g.watch(self.critic.trainable_weights)
sch_pred = self.predict_critic(zs_0, state)
ind_vec = np.zeros_like(sch_pred.numpy())
apu_avg = 0.0
for i in range(n_samples):
wts = np.random.uniform(0, 1, size=(nn, 1))
# zs_i = zs_nn + np.random.normal(0, z_std, size=(nn, 1))
zs_i = zs_nn + np.random.uniform(-0.5*z_std, 0.5*z_std, size=(nn, 1))
# zs_i = zs_nn
if FLAGS.predict == 'mwis':
gcn_wts = np.multiply(zs_i.flatten(), wts.flatten())
else:
gcn_wts = zs_i.flatten()
# _, total_ref = local_greedy_search(adj, wts)
mwis, _ = local_greedy_search(adj, gcn_wts)
solu = list(mwis)
total_wt = np.sum(wts[solu, 0])
ind_vec[solu, 0] += wts[solu, 0]/float(n_samples)
# reward = apu_avg
ind_vec[:, 1] = 1.0 - ind_vec[:, 0]
corr, pval = pearsonr(ind_vec[:, 0], sch_pred[:, 0].numpy())
reward = corr
y_target = tf.convert_to_tensor(ind_vec, dtype=tf.float64)
regularization_loss = tf.reduce_sum(self.critic.losses)
loss_value = tf.sqrt(self.mse(y_target, sch_pred)) + regularization_loss
gradients = g.gradient(loss_value, self.critic.trainable_weights)
self.memorize_crt(gradients, loss_value.numpy(), reward)
return ind_vec, reward