-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent_gdpg_util.py
executable file
·356 lines (307 loc) · 13.1 KB
/
agent_gdpg_util.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
# 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
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, 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)
# Some preprocessing
nsr = np.power(10.0, -FLAGS.snr_db/20.0)
class GDPGAgent(Solver):
def __init__(self, input_flags, memory_size=5000):
super(GDPGAgent, 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.memory_act = deque(maxlen=memory_size)
self.mse = MeanSquaredError()
self.critic = self._build_critic(num_layer=3)
self.critic.trainable = False
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 = "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,), 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 = 32
else:
act = "linear"
output_dim = 1
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=False,
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.001)
else:
lr_schedule = schedules.ExponentialDecay(
initial_learning_rate=0.001,
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 save_critic(self, checkpoint_path):
self.critic.save_weights(checkpoint_path)
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 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 memorize_act(self, grad, loss, reward):
self.memory_act.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):
act_values, action = self.predict(state)
return act_values, action # returns action
def predict_critic(self, z_out, state):
coord, values, shape = state["support"]
a_in = tf.sparse.SparseTensor(coord, values, shape)
sch_pred = self.critic([z_out, a_in])
return sch_pred
def replay(self, batch_size):
if len(self.memory) < batch_size:
return float('NaN'), float('NaN')
self.reward_mem.clear()
minibatch = random.sample(self.memory, batch_size)
self.critic.trainable = True
self.model.trainable = True
for state, act_vals, solu, next_state, reward in minibatch:
if next_state:
act_vals_next, _ = self.predict(next_state)
val_next = self.predict_critic(act_vals_next, next_state)
else:
val_next = np.zeros_like(act_vals)
reward_t = tf.convert_to_tensor(reward, dtype=tf.float64) / 100.0
reward_t = tf.reshape(reward_t, (-1, 1))
val_vec = reward_t + self.gamma * val_next
act_vals_t = tf.convert_to_tensor(act_vals, dtype=tf.float64)
val_this = self.predict_critic(act_vals_t, state)
with tf.GradientTape() as g:
g.watch(self.critic.trainable_weights)
regularization_loss = tf.reduce_sum(self.critic.losses)
loss_value = tf.sqrt(self.mse(val_vec, val_this)) + regularization_loss
gradients = g.gradient(loss_value, self.critic.trainable_weights)
self.memorize_crt(gradients, loss_value.numpy(), reward)
with tf.GradientTape() as g:
g.watch(self.model.trainable_weights)
act_val, _ = self.act(state, False)
val_crt = self.predict_critic(act_val, state)
regularization_loss = tf.reduce_sum(self.model.losses)
obj_fn = -tf.reduce_mean(val_crt[:, 0])
obj_fn = obj_fn + regularization_loss
gradients = g.gradient(obj_fn, self.model.trainable_weights)
self.memorize_act(gradients, obj_fn.numpy(), 0)
loss_c = self.replay_crt(batch_size)
loss_a = self.replay_act(batch_size*10)
# self.memory.clear()
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
return loss_a, loss_c
def replay_act(self, batch_size):
if len(self.memory_act) < batch_size:
return float('NaN')
self.reward_mem.clear()
minibatch = random.sample(self.memory_act, batch_size)
losses = []
for grad, loss, _ in minibatch:
self.optimizer.apply_gradients(zip(grad, self.model.trainable_weights))
losses.append(loss)
self.memory_act.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 utility(self, adj_0, wts_0, train=False):
"""
GCN for per utility function
"""
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 = self.act(state, train)
gcn_wts = act_vals
return gcn_wts, state
def schedule(self, adj_0, wts_0, train=False):
"""
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, state, act_vals
def actor_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)
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)
if train:
regularization_loss = tf.reduce_sum(self.model.losses)
obj_fn = -tf.reduce_mean(sch_pred[:, 0])
obj_fn = obj_fn + regularization_loss
gradients = g.gradient(obj_fn, self.model.trainable_weights)
self.memorize_act(gradients, obj_fn.numpy(), 0)
return state, act_val
def critic_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))
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] += 1.0/float(n_samples)
apu_avg += total_wt/(total_ref*float(n_samples)+1e-6)
reward = apu_avg
ind_vec[:, 1] = 1.0 - ind_vec[:, 0]
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, apu_avg
# use gpu 0
os.environ['CUDA_VISIBLE_DEVICES'] = str(0)
# Initialize session
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True