class SSNES(): def __init__(self, x0, learning_rate_mult, popsize): self.snes = SNES(x0, learning_rate_mult, popsize) self.gen() def gen(self): self.asked = self.snes.ask() self.scores = {n: [] for n in range(len(self.asked))} def predict(self): r = randint(0, len(self.asked) - 1) asked = self.asked[r] return asked, r def fit(self, scores, r): # sort them out for i, score in enumerate(scores): self.scores[r[i]].append(score) told = [] for i in range(len(self.asked)): told.append(np.array(self.scores[i]).mean()) self.snes.tell(self.asked, told) self.gen()
def main(): print("Total number of weights to evolve is:", weights.shape) all_examples_indices = list(range(x_train.shape[0])) clf, _ = train_classifier(model, x_train, y_train) y_pred = predict_classifier(model, clf, x_test) print(y_test.shape, y_pred.shape) test_accuracy = accuracy_score(y_test, y_pred) print('Non-trained NN Test accuracy:', test_accuracy) # print('Test MSE:', test_mse) snes = SNES(weights, 1, POPULATION_SIZE) for i in range(0, GENERATIONS): start = timer() asked = snes.ask() # to be provided back to snes told = [] # use a small number of training samples for speed purposes subsample_indices = np.random.choice(all_examples_indices, size=SAMPLE_SIZE, replace=False) # evaluate on another subset subsample_indices_valid = np.random.choice(all_examples_indices, size=SAMPLE_SIZE + 1, replace=False) # iterate over the population for asked_j in asked: # set nn weights nnw.set_weights(asked_j) # train the classifer and get back the predictions on the training data clf, _ = train_classifier(model, x_train[subsample_indices], y_train[subsample_indices]) # calculate the predictions on a different set y_pred = predict_classifier(model, clf, x_train[subsample_indices_valid]) score = accuracy_score(y_train[subsample_indices_valid], y_pred) # clf, _ = train_classifier(model, x_train, y_train) # y_pred = predict_classifier(model, clf, x_test) # score = accuracy_score(y_test, y_pred) # append to array of values that are to be returned told.append(score) snes.tell(asked, told) end = timer() print("It took", end - start, "seconds to complete generation", i + 1) nnw.set_weights(snes.center) clf, _ = train_classifier(model, x_train, y_train) y_pred = predict_classifier(model, clf, x_test) print(y_test.shape, y_pred.shape) test_accuracy = accuracy_score(y_test, y_pred) print('Test accuracy:', test_accuracy)
def train_agent(game_chosen,game,run,pop_size,generations): """ function to run the game Args: game_chosen (str): name of the game game(str): folder name run(int): folder name pop_size: population size for the SNES algorithm generations: number of generations for the SNES algorithm """ # start the desired game file textPlayer = tP.TextPlayer(game_chosen) # initialize a list to keep track of the game scores track_scores = [] # set last score to zero last_score = 0 # initialize the agent agent = Neuroagent_agent.NeuroAgent() # return the original weights of the neural network structure in the neuroevolution initial_weights = agent.agent_return_weights() state = textPlayer.run() last_state = state # pass variables to SNES snes = SNES(initial_weights, 1, pop_size) # start the timer start = timer() # iterate through number of generations for i in range(0, generations): asked = snes.ask() told = [] j = 0 for asked_j in asked: # use SNES to set the weights of the NN last_state = state action = agent.take_action(state,asked_j) state = textPlayer.execute_command(action) print('{0} >>> {1}'.format(action,state)) print('This is Population No. {0} of Generation no. {1}'.format(j,i)) if textPlayer.get_score() != None: score, possible_score = textPlayer.get_score() # if previous state is equal to current state, then agent reward gets deducted by value of -0.2 otherwise reward multiplies by a factor of * 1.2 reward = score - last_score if last_state == state: agent_reward = reward - 0.2 else: agent_reward = reward * 1.2 told.append(agent_reward) last_score = score agent.update(reward) accumulated_reward = agent.get_total_points_earned() print ('Your overall score is {0} and you gained reward of {1} in the last action and agent reward of {2}'.format(accumulated_reward,reward,agent_reward)) track_scores.append((state,j,i,action,agent_reward,reward,accumulated_reward)) else: #in case the game cant retrieve the score from the game engine, set the score for that generation to 0 score = 0 # if previous state is equal to current state, then agent reward gets deducted by value of -0.2 otherwise reward multiplies by a factor of * 1.2 reward = score - last_score if last_state == state: agent_reward = reward - 0.2 else: agent_reward = reward * 1.2 told.append(agent_reward) last_score = score agent.update(reward) accumulated_reward = agent.get_total_points_earned() print ('Your overall score is {0} and you gained reward of {1} in the last action and agent reward of {2}'.format(accumulated_reward,reward,agent_reward)) track_scores.append((state,j,i,action,agent_reward,reward,accumulated_reward)) j += 1 snes.tell(asked,told) save(i,track_scores,game,run) tokens, labels = agent.agent_return_models() save_tokens_labels_words_seen(i, tokens, labels,game,run) # at end of training # pass the final weights to the neural network structure snes_centre_weights = snes.center agent.pass_snes_centre_weight(snes_centre_weights) word_seen = agent.agent_return_word_seen() # save the scores with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/track_scores_'+game+'_pop50gen500.pkl', 'wb') as f: pickle.dump(track_scores, f) # save the words seen in the game with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/words_seen_'+game+'_pop50gen500.pkl', 'wb') as h: pickle.dump(word_seen, h) with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/test_word_seen_vectors_'+game+'_pop50gen500.pkl', 'wb') as i: pickle.dump(tokens, i) with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/test_word_seen_vocab_'+game+'_pop50gen500.pkl', 'wb') as k: pickle.dump(labels, k) # end the timer end = timer() print('The time taken to run the traning is {0}'.format(end - start)) textPlayer.quit()
# this is irrelevant for what we want to achieve model.compile(loss="mse", optimizer="adam") print("compilation is over") nnw = NNWeightHelper(model) weights = nnw.get_weights() print("Total number of weights to evolve is:", weights.shape) all_examples_indices = list(range(x_train.shape[0])) clf, _ = train_classifier(model, x_train, y_train) y_pred = predict_classifier(model, clf, x_test) test_accuracy = accuracy_score(y_test, y_pred) print('Non-trained NN Test accuracy:', test_accuracy) # print('Test MSE:', test_mse) snes = SNES(weights, 1, POPULATION_SIZE) p = [] for i in range(0, GENERATIONS): start = timer() asked = snes.ask() # to be provided back to snes told = [] # use a small number of training samples for speed purposes subsample_indices = np.random.choice(all_examples_indices, size=SAMPLE_SIZE, replace=False) # evaluate on another subset subsample_indices_valid = np.random.choice(all_examples_indices, size=SAMPLE_SIZE + 1,
def __init__(self, x0, learning_rate_mult, popsize): self.snes = SNES(x0, learning_rate_mult, popsize) self.gen()
#weights = K.get_trainable_weights(model) #dim = len(weights) #steps = 2*SAMPLES/GENERATION**2/BATCH_SIZE #target generations for SNES to reach full adaptation batch_size = int(len(source_adapt) / round(len(source_adapt) / BATCH_SIZE, 0)) steps = 2 * SAMPLES / GENERATION / batch_size #batches = int(len(source_adapt)/batch_size) #number iterations for each epoch batches = int(GENERATION * len(source_adapt) / batch_size) batches -= batches % GENERATION trgbatch_size = int( len(target_adapt) / round(len(target_adapt) / batch_size, 0)) trg_batches = int(GENERATION * len(target_adapt) / trgbatch_size) yd_fit = np.concatenate([np.tile(0, batch_size), np.tile(1, trgbatch_size)]) #epochs = int(EPOCHS*SAMPLES_PER_EPOCH/batch_size/batches) epochs = int(EPOCHS * GENERATION * SAMPLES_PER_EPOCH / batch_size / batches) snes = SNES(weights, LEARN_RATE, GENERATION) #snes = SSNES(weights, 2, GENERATION)#np.zeros(dim), 1, GENERATION) fitnesses = np.zeros(GENERATION) losses = [[], [], [], []] asked = snes.ask() #asked = snes.asked """----------------------commence adaptation---------------------------- """ t0 = time.time() step = 0 genloss = [] timeloss = [] trg_batch = 0 trg_start = 0 trgtrain_end = trgbatch_size trgtest_start = trgtrain_end trgtest_end = 2 * trgtrain_end
#hp.setrelheap() track_scores = [] last_score = 0 # defining population size pop_size = 10 # defining generation size generations = 100 # init the agent agent = vivan_agent.vivAgent() initial_weights = agent.agent_return_weights() state = textPlayer.run() last_state = state # print (state) # pass variables to SNES snes = SNES(initial_weights, 1, pop_size) start = timer() #while (counter < training_cycles): for i in range(0, generations): asked = snes.ask() told = [] j = 0 for asked_j in asked: # use SNES to set the weights of the NN # only run the SNES after 1st round # pass asked to function in vivan_wordfiles.py everytime # init the neural network in vivan_wordfiles.py # but it is run only when the def_results_for_words is called. # how about reward?