add noise exploration

This commit is contained in:
bohanjason 2019-09-22 23:38:06 -04:00 committed by Dana Van Aken
parent 11cfe5afc1
commit f716ade292
2 changed files with 28 additions and 12 deletions

View File

@ -32,14 +32,15 @@ class NeuralNet(object):
learning_rate=0.01, learning_rate=0.01,
debug=False, debug=False,
debug_interval=100, debug_interval=100,
batch_size=2, batch_size=1,
explore_iters=500, explore_iters=500,
noise_scale_begin=0.5, noise_scale_begin=0.1,
noise_scale_end=0.01): noise_scale_end=0):
# absolute path for the model weitghs file # absolute path for the model weitghs file
# one model for each (project, session) # one model for each (project, session)
self.weights_file = weights_file self.weights_file = weights_file
self.history = None
self.recommend_iters = 0 self.recommend_iters = 0
self.n_input = n_input self.n_input = n_input
self.debug = debug self.debug = debug
@ -73,7 +74,7 @@ class NeuralNet(object):
self.model.load_weights(self.weights_file) self.model.load_weights(self.weights_file)
if self.debug: if self.debug:
LOG.info('Neural Network Model weights file exists, load weights from the file') LOG.info('Neural Network Model weights file exists, load weights from the file')
except: except Exception: # pylint: disable=broad-except
LOG.info('Weights file does not match neural network model, train model from scratch') LOG.info('Weights file does not match neural network model, train model from scratch')
# Build same neural network as self.model, But input X is variables, # Build same neural network as self.model, But input X is variables,
@ -111,20 +112,22 @@ class NeuralNet(object):
# save model weights # save model weights
self.save_weights() self.save_weights()
if self.debug: if self.debug:
MSEs = self.history.history['mean_squared_error'] mse = self.history.history['mean_squared_error']
i = 0 i = 0
size = len(MSEs) size = len(mse)
while(i < size): while(i < size):
LOG.info("Neural network training phase, epoch %d: mean_squared_error %f", LOG.info("Neural network training phase, epoch %d: mean_squared_error %f",
i, MSEs[i]) i, mse[i])
i += self.debug_interval i += self.debug_interval
LOG.info("Neural network training phase, epoch %d: mean_squared_error %f", LOG.info("Neural network training phase, epoch %d: mean_squared_error %f",
size - 1, MSEs[size - 1]) size - 1, mse[size - 1])
def predict(self, X_pred): def predict(self, X_pred):
return self.model.predict(X_pred) return self.model.predict(X_pred)
def add_noise(self, weights, scale=1): # Reference: Parameter Space Noise for Exploration.ICLR 2018, https://arxiv.org/abs/1706.01905
def add_noise(self, weights):
scale = self.adaptive_noise_scale()
size = weights.shape[-1] size = weights.shape[-1]
noise = scale * np.random.normal(size=size) noise = scale * np.random.normal(size=size)
return weights + noise return weights + noise
@ -144,6 +147,14 @@ class NeuralNet(object):
w2, b2 = self.model.get_layer(index=2).get_weights() w2, b2 = self.model.get_layer(index=2).get_weights()
w3, b3 = self.model.get_layer(index=3).get_weights() w3, b3 = self.model.get_layer(index=3).get_weights()
if explore is True:
w1 = self.add_noise(w1)
b1 = self.add_noise(b1)
w2 = self.add_noise(w2)
b2 = self.add_noise(b2)
w3 = self.add_noise(w3)
b3 = self.add_noise(b3)
if self.debug: if self.debug:
y_predict = self.predict(X_start) y_predict = self.predict(X_start)
LOG.info("Recommend phase, y prediction: min %f, max %f, mean %f", LOG.info("Recommend phase, y prediction: min %f, max %f, mean %f",

View File

@ -546,10 +546,15 @@ def configuration_recommendation(target_data):
# neural network model # neural network model
# FIXME: choose algorithm based on the session option # FIXME: choose algorithm based on the session option
model_nn = NeuralNet(weights_file=full_path, n_input=X_samples.shape[1], model_nn = NeuralNet(weights_file=full_path,
batch_size=X_samples.shape[0], debug=True) n_input=X_samples.shape[1],
batch_size=X_samples.shape[0],
explore_iters=500,
noise_scale_begin=0.1,
noise_scale_end=0,
debug=True)
model_nn.fit(X_scaled, y_scaled) model_nn.fit(X_scaled, y_scaled)
res = model_nn.recommend(X_samples, X_min, X_max, explore=False) res = model_nn.recommend(X_samples, X_min, X_max, explore=True)
model = GPRGD(length_scale=DEFAULT_LENGTH_SCALE, model = GPRGD(length_scale=DEFAULT_LENGTH_SCALE,
magnitude=DEFAULT_MAGNITUDE, magnitude=DEFAULT_MAGNITUDE,