-
Notifications
You must be signed in to change notification settings - Fork 2
/
DQNController.py
363 lines (288 loc) · 15.8 KB
/
DQNController.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
357
358
359
360
361
362
363
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 22 09:58:50 2022
@author: AnshumaanChauhan
"""
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Activation, Flatten, Input, LSTM
from keras.models import Model
from keras.callbacks import TensorBoard
from keras.activations import relu
from keras.optimizers import *
from collections import deque
import time
import random
import os
import tensorflow.keras.optimizers
import numpy as np
from keras.preprocessing.sequence import pad_sequences
import CNNCONSTANTS
from CNNCONSTANTS import *
from CNNGenerator import CNNSearchSpace
from operator import itemgetter
#In order to make results comparable for different models
random.seed(1)
class DQNAgent (CNNSearchSpace):
def __init__(self):
REPLAY_MEMORY_SIZE = 50000 # Can also write as 50_000 for readability
MODEL_NAME= "First_Try"
self.MIN_REPLAY_MEMORY_SIZE=10
#MINIBATCH_SIZE= 64 #Usually the size of mini batch is 32, 64, or multiple of 8
#DISCOUNT= 0.99
self.UPDATE_TARGET_EVERY=3
#Constants for epsilon greedy algo
self.EPSILON=1 #Will be decayed over the training process
self.EPSILON_DECAY=0.01
self.seq_data=[]
self.replay_memory=[]
#self.replay_memory= deque(maxlen=REPLAY_MEMORY_SIZE)
#Will store x,y, val accuracy, pred_accuracy for all the things geenrated
#List with a fixed max length , will store last maxlen number of steps of Main model
#Batch Learning generally makes a better and stabilised model (doesn't overfits)
#Now we take a random samle out of these 50000 memory and then this batch is what we feed to Target Model
self.target_update_counter= 0 # Will use to track and tell when to update the Target Network
self.max_len = MAX_ARCHITECTURE_LENGTH
self.controller_lstm_dim = CONTROLLER_LSTM_DIM
self.controller_optimizer = CONTROLLER_OPTIMIZER
self.controller_lr = CONTROLLER_LEARNING_RATE
self.controller_decay = CONTROLLER_DECAY
self.controller_momentum = CONTROLLER_MOMENTUM
self.use_predictor = CONTROLLER_USE_PREDICTOR
# inheriting from the search space
super().__init__(TARGET_CLASSES)
# number of classes for the controller (+ 1 for padding)
self.controller_classes = len(self.vocab) + 1
# file path of controller weights to be stored at
self.controller_weights = 'LOGS2/controller_weights.h5'
#Main model
#self.model=self.create_control_model()
#Target model
#self.target_model=self.create_control_model()
#self.target_model.set_weights(self.model.get_weights())
#Initially it will do just random exploration and eventually learn about the optimal value
#Therefore, not advisable to update weights after each predict
#Will fit main model that will fitted after every step (Trained every step)
#Target model will be the one we will do predict every step
#After some n number of epochs we set weights of Train model same as that of Main model
#Stablises the model, and a lot of randomness is noticed in initial steps
def sample_architecture_sequences(self, model, number_of_samples):
# define values needed for sampling
final_layer_id = len(self.vocab)
BatchNorm_id = final_layer_id - 1
Flatten_id=final_layer_id-2
vocab_idx = [0] + list(self.vocab.keys())
# initialize list for architecture samples
samples = []
print("GENERATING ARCHITECTURE SAMPLES...")
print('------------------------------------------------------')
# while number of architectures sampled is less than required
while len(samples) < number_of_samples:
# initialise the empty list for architecture sequence
seed = []
# while len of generated sequence is less than maximum architecture length
while len(seed) < self.max_len:
# pad sequence for correctly shaped input for controller
sequence = pad_sequences([seed], maxlen=self.max_len - 1, padding='post')
sequence = sequence.reshape(1, 1, self.max_len - 1)
# given the previous elements, get softmax distribution for the next element
if self.use_predictor:
(probab, _) = model.predict(sequence)
else:
probab = model.predict(sequence)
#print(probab[0])
#print(len(probab[0]))
#probab = probab[0][0]
#print(probab)
# sample the next element randomly given the probability of next elements (the softmax distribution)
'''
random_val=random.random()
if self.EPSILON > 0:
if random_val<self.EPSILON:
next = np.random.choice(vocab_idx, size=1, p=probab[0])
next=next[0]
self.EPSILON=self.EPSILON-self.EPSILON_DECAY
else:
best_action= max(probab[0])
list_rep=probab[0].tolist()
next=vocab_idx[list_rep.index(best_action)]
else:
best_action= max(probab[0])
list_rep=probab[0].tolist()
next=vocab_idx[list_rep.index(best_action)]
'''
next = np.random.choice(vocab_idx, size=1, p=probab[0])
#Here we have to specify a range of values, to cover the point of dropout cannot be the first layer
if (next >= self.conv_id) and len(seed) == 0:
continue
#Have to make a rule such that first layer cannot be anything except the Convolutional Layer
# first layer is not final layer
if next == final_layer_id and len(seed) == 0:
continue
# if final layer, break out of inner loop
if next == final_layer_id:
seed.pop()
seed.append(Flatten_id)
seed.append(next)
break
# if sequence length is 1 less than maximum, add final
# layer and break out of inner loop
if len(seed) == self.max_len - 2:
seed.append(Flatten_id)
seed.append(final_layer_id)
break
# ignore padding
if not next == 0:
check_insert=False
if next > self.pool_id and next <= self.fully_id :
for i in seed:
if i == Flatten_id :
seed.append(next)
check_insert=True
break
if next == Flatten_id :
check_dupli_flatten=False
for i in seed :
if i==next:
check_dupli_flatten=True
if not check_dupli_flatten:
seed.append(next)
check_insert=True
if next <= self.pool_id :
check_no_pool_conv_after_flatten=False
for i in seed:
if i == Flatten_id:
check_no_pool_conv_after_flatten=True
if not check_no_pool_conv_after_flatten :
seed.append(next)
check_insert=True
if next > self.fully_id and next<=self.reg_layer_id:
i=seed[-1]
if ((i>self.conv_id and i<=self.pool_id) or (i>self.pool_id and i<=self.fully_id)):
seed.append(next)
check_insert=True
if next== BatchNorm_id:
i=seed[-1]
if i>self.fully_id and i<=self.reg_layer_id:
seed.append(next)
check_insert=True
if not check_insert:
seed.append(next)
else:
continue
# check if the generated sequence has been generated before.
# if not, add it to the sequence data.
if seed not in self.seq_data:
samples.append(seed)
self.seq_data.append(seed)
return samples
def create_control_model(self, controller_input_shape, controller_batch_size):
main_input=Input(shape=controller_input_shape, name='main_input')
x= LSTM(self.controller_lstm_dim, return_sequences=True)(main_input)
x2=Dropout(0.2)(x)
x3=LSTM(self.controller_lstm_dim)(x2)
x4=Dropout(0.2)(x3)
main_output=Dense(self.controller_classes, activation='softmax', name='main_output')(x4)
model=Model(inputs=[main_input],outputs=[main_output])
return model
def create_hybrid_model(self, controller_input_shape, controller_batch_size):
main_input=Input(shape=controller_input_shape, name='main_input')
x= LSTM(self.controller_lstm_dim, return_sequences=True)(main_input)
x2=Dropout(0.2)(x)
x3=LSTM(self.controller_lstm_dim)(x2)
x4=Dropout(0.2)(x3)
main_output=Dense(self.controller_classes, activation='softmax', name='main_output')(x4)
# LSTM layer
x5 = LSTM(self.controller_lstm_dim, return_sequences=True)(main_input)
# single neuron sigmoid layer for accuracy prediction
predictor_output = Dense(1, activation='sigmoid', name='predictor_output')(x2)
# finally the Keras Model class is used to create a multi-output model
model = Model(inputs=[main_input], outputs=[main_output, predictor_output])
return model
#Only train when certain number of samples have been stord in the replay table
def train_control_model(self, model, target_model, x_data, y_data, val_accuracy, loss_func, controller_batch_size, nb_epochs):
for i in range(len(x_data)):
self.replay_memory.append([x_data[i][0],y_data[i],val_accuracy[i]])
if len(self.replay_memory)<self.MIN_REPLAY_MEMORY_SIZE:
return
#Top 250 Architectures are taken
self.replay_memory= sorted(self.replay_memory,key=itemgetter(2))
to_train=self.replay_memory[:1]
optim = getattr(tensorflow.keras.optimizers, self.controller_optimizer)(lr=self.controller_lr,
decay=self.controller_decay)
# compile model depending on loss function and optimizer provided
model.compile(optimizer=optim, loss={'main_output': loss_func})
# load controller weights
if os.path.exists(self.controller_weights):
model.load_weights(self.controller_weights)
x_data=[]
y_data=[]
for i in range(len(to_train)):
x_data.append(to_train[i][0].reshape(1,7))
y_data.append(to_train[i][1])
# train the controller
#We are trying to make it learn that if the previous layers are given in this order and the next predicted is final
#Taking the ones with best accuracy helps it learn which is better
print("TRAINING CONTROLLER...")
model.fit({'main_input': np.array(x_data)},
{'main_output': np.array(y_data)},
epochs=nb_epochs,
batch_size=controller_batch_size,
verbose=0)
#{'main_output': y_data.reshape(len(y_data), 1, self.controller_classes)}
# save controller weights
model.save_weights(self.controller_weights)
#Updating the counter and checking whether we want to update the model or not
self.target_update_counter+=1
#If we are at the point where counter value id reached, then we just copy the weights from Main model to Target Model
if self.target_update_counter>self.UPDATE_TARGET_EVERY:
print("TRANSFERRING WEIGHTS...")
target_model.set_weights(model.get_weights())
#Reinitialize target update counter value to 0
self.target_update_counter=0
def train_hybrid_model(self, model, target_model, x_data, y_data, val_accuracy, pred_accuracy, loss_func, controller_batch_size, nb_epochs):
for i in range(len(x_data)):
self.replay_memory.append([x_data[i][0],y_data[i],val_accuracy[i],pred_accuracy[i]])
if len(self.replay_memory)<self.MIN_REPLAY_MEMORY_SIZE:
return
#Top 250 Architectures are taken
self.replay_memory= sorted(self.replay_memory,key=itemgetter(2))
to_train=self.replay_memory[:1]
optim = getattr(tensorflow.keras.optimizers, self.controller_optimizer)(lr=self.controller_lr, decay=self.controller_decay, clipnorm=1.0)
model.compile(optimizer=optim,
loss={'main_output': loss_func, 'predictor_output': 'mse'},
loss_weights={'main_output': 1, 'predictor_output': 1})
if os.path.exists(self.controller_weights):
model.load_weights(self.controller_weights)
x_data=[]
y_data=[]
pred_target=[]
for i in range(len(to_train)):
x_data.append(to_train[i][0].reshape(1,7))
y_data.append(to_train[i][1])
pred_target.append(to_train[i][3])
print("TRAINING CONTROLLER...")
model.fit({'main_input': np.array(x_data)},
{'main_output': np.array(y_data),
'predictor_output': np.array(pred_target)},
epochs=nb_epochs,
batch_size=controller_batch_size,
verbose=0)
model.save_weights(self.controller_weights)
self.target_update_counter+=1
#If we are at the point where counter value id reached, then we just copy the weights from Main model to Target Model
if self.target_update_counter>self.UPDATE_TARGET_EVERY:
print("TRANSFERRING WEIGHTS...")
self.target_model.set_weights(model.get_weights())
#Reinitialize target update counter value to 0
self.target_update_counter=0
def get_predicted_accuracies_hybrid_model(self, model, seqs):
pred_accuracies = []
for seq in seqs:
# pad each sequence
control_sequences = pad_sequences([seq], maxlen=self.max_len, padding='post')
xc = control_sequences[:, :-1].reshape(len(control_sequences), 1, self.max_len - 1)
# get predicted accuracies
(_, pred_accuracy) = [x[0][0] for x in model.predict(xc)]
pred_accuracies.append(pred_accuracy[0])
return pred_accuracies