-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic-algorithm.py
96 lines (73 loc) · 2.81 KB
/
genetic-algorithm.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
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from pprint import pprint
import array
import numpy as np
import random
import representations, fitness, mutations
INITIAL_BLOCKS = 5
POPULATION = 10
GENERATIONS = 20
PROB_MUTATIONS = 0.6
PROB_MATE = 0.4
NUMBER_EPOCHS = 30
DATASET = 'fashion' # or 'cifar10'
def getRandomLayer():
networks = [
representations.make_conv2d_repr(),
representations.make_conv2d_pool_repr(),
representations.make_conv2d_dropout_repr(),
representations.make_noise_repr(),
]
probabilities = np.array([.4, .5, .5, .05])
probabilities = probabilities / probabilities.sum()
return np.random.choice(networks, p=probabilities)
def evaluateFunc(individual):
return fitness.evaluate_nn(individual, NUMBER_EPOCHS, verbose=True, dataset=DATASET),
def initRepeatRandom(container, func, n):
'''
Extended toolbox.initRepeat() function to work with random initialization
instead of fixed numbers. Set this length to be minimal of 2, as we do two
point crossover!!
'''
return container(func() for _ in range(np.random.randint(2, n)))
# Create attributes
creator.create('FitnessMax', base.Fitness, weights=(1.0,))
creator.create('Individual', np.ndarray, fitness=creator.FitnessMax)
mutations.setIndividual(creator.Individual)
mutations.setInitialization(getRandomLayer)
toolbox = base.Toolbox()
toolbox.register('individual', initRepeatRandom, creator.Individual,
getRandomLayer, n=INITIAL_BLOCKS)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register('evaluate', evaluateFunc) #register the evaluation function
toolbox.register('mate', tools.cxTwoPoint)
toolbox.register('mutate', mutations.mutate_network, mutations=1)
toolbox.register('select', tools.selTournament, tournsize=3)
def main():
random.seed(1337)
pop = toolbox.population(n=POPULATION)
# Evaluate the entire population
fitnesses = list(map(toolbox.evaluate, pop))
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
hof = tools.HallOfFame(10, similar=np.array_equal)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register('avg', np.mean)
stats.register('std', np.std)
stats.register('min', np.min)
stats.register('max', np.max)
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=PROB_MATE,
mutpb=PROB_MUTATIONS, ngen=GENERATIONS,
stats=stats, halloffame=hof, verbose=True)
print('\n----------------------------------')
print(log)
print('\n----------------------------------\n')
print('Hall of Fame:\n')
for h in hof:
pprint(h)
print('Fitness: {}'.format(h.fitness))
if __name__ == '__main__':
main()