Last active
January 11, 2018 16:20
-
-
Save guillefix/77b11190a3e21f128275eedd9122fd7c to your computer and use it in GitHub Desktop.
Neuroevolution in 50 lines of code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from deap import creator, base, tools, algorithms | |
from scoop import futures | |
toolbox = base.Toolbox() | |
toolbox.register("map", futures.map) | |
creator.create("FitnessMax", base.Fitness, weights=(1.0,)) | |
creator.create("Individual", list, fitness=creator.FitnessMax) | |
number_of_parameters = 100 | |
size_of_population = 100 | |
mutation_size=0.05 | |
toolbox.register("attribute", random.randint,0,1e8) | |
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attribute, n=number_of_parameters) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
import tensorflow as tf | |
import numpy as np | |
tf.reset_default_graph() | |
x=tf.placeholder(tf.float32, shape=(None,5)) | |
W=tf.Variable(tf.random_normal([5,5])) | |
y = tf.matmul(x,W) | |
y_true=tf.placeholder(tf.float32, shape=(None,5)) | |
error = tf.losses.mean_squared_error(y_true,y) | |
sess=tf.Session() | |
sess.run(tf.global_variables_initializer()) | |
trainX = np.random.randn(10,5) | |
trainY = np.dot(trainX,np.random.rand(5,5)) | |
def EvaluateNet(seeds): | |
vals=sess.run(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)) | |
np.random.seed(seeds[0]) | |
for i,val in enumerate(vals): | |
vals[i] = np.random.randn(vals[i].shape[0],vals[i].shape[1]) | |
for seed in seeds[1:]: | |
np.random.seed(seed) | |
for i,val in enumerate(vals): | |
vals[i] += mutation_size*np.random.randn(vals[i].shape[0],vals[i].shape[1]) | |
ops=[] | |
for i,var in enumerate(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)): | |
ops.append(var.assign(vals[i])) | |
sess.run(ops) | |
return sess.run(-error,feed_dict={x:trainX,y_true:trainY}), | |
toolbox.register("evaluate", EvaluateNet) | |
toolbox.register("select", tools.selTournament, tournsize=3) | |
population = toolbox.population(n=size_of_population) | |
def mutate(individual): | |
return individual.append(random.randint(0,1e8)) | |
if __name__ == '__main__': | |
NGEN=20 | |
offspring = toolbox.select(population, len(population)) | |
for gen in range(NGEN): | |
print(gen) | |
offspring = list(map(toolbox.clone, offspring)) | |
mutants = list(map(mutate,offspring)) | |
fits = list(map(toolbox.evaluate, offspring)) | |
for fit, ind in zip(fits, offspring): | |
ind.fitness.values = fit | |
population = toolbox.select(list(offspring), k=len(population)) | |
top10 = tools.selBest(population, k=10) | |
print(toolbox.evaluate(top10[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment