-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.py
188 lines (143 loc) · 5.98 KB
/
main.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
"""Entry point to evolving the neural network. Start here."""
from __future__ import print_function
from evolver import Evolver
from tqdm import tqdm
import logging
import sys
# Setup logging.
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.INFO#,
#filename='log.txt'
)
def train_genomes(genomes, dataset):
"""Train each genome.
Args:
networks (list): Current population of genomes
dataset (str): Dataset to use for training/evaluating
"""
logging.info("***train_networks(networks, dataset)***")
pbar = tqdm(total=len(genomes))
for genome in genomes:
genome.train(dataset)
pbar.update(1)
pbar.close()
def get_average_accuracy(genomes):
"""Get the average accuracy for a group of networks/genomes.
Args:
networks (list): List of networks/genomes
Returns:
float: The average accuracy of a population of networks/genomes.
"""
total_accuracy = 0
for genome in genomes:
total_accuracy += genome.accuracy
return total_accuracy / len(genomes)
def generate(generations, population, all_possible_genes, dataset):
"""Generate a network with the genetic algorithm.
Args:
generations (int): Number of times to evolve the population
population (int): Number of networks in each generation
all_possible_genes (dict): Parameter choices for networks
dataset (str): Dataset to use for training/evaluating
"""
logging.info("***generate(generations, population, all_possible_genes, dataset)***")
evolver = Evolver(all_possible_genes)
genomes = evolver.create_population(population)
# Evolve the generation.
for i in range( generations ):
logging.info("***Now in generation %d of %d***" % (i + 1, generations))
print_genomes(genomes)
# Train and get accuracy for networks/genomes.
train_genomes(genomes, dataset)
# Get the average accuracy for this generation.
average_accuracy = get_average_accuracy(genomes)
# Print out the average accuracy each generation.
logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
logging.info('-'*80) #-----------
# Evolve, except on the last iteration.
if i != generations - 1:
# Evolve!
genomes = evolver.evolve(genomes)
# Sort our final population according to performance.
genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True)
# Print out the top 5 networks/genomes.
print_genomes(genomes[:5])
#save_path = saver.save(sess, '/output/model.ckpt')
#print("Model saved in file: %s" % save_path)
def print_genomes(genomes):
"""Print a list of genomes.
Args:
genomes (list): The population of networks/genomes
"""
logging.info('-'*80)
for genome in genomes:
genome.print_genome()
def main():
"""Evolve a genome."""
population = 30 # Number of networks/genomes in each generation.
#we only need to train the new ones....
ds = 4
if( ds == 1):
dataset = 'mnist_mlp'
elif (ds == 2):
dataset = 'mnist_cnn'
elif (ds == 3):
dataset = 'cifar10_mlp'
elif (ds == 4):
dataset = 'cifar10_cnn'
else:
dataset = 'mnist_mlp'
print("***Dataset:", dataset)
if dataset == 'mnist_cnn':
generations = 8 # Number of times to evolve the population.
all_possible_genes = {
'nb_neurons': [16, 32, 64, 128],
'nb_layers': [1, 2, 3, 4 ,5],
'activation': ['relu', 'elu', 'tanh', 'sigmoid', 'hard_sigmoid','softplus','linear'],
'optimizer': ['rmsprop', 'adam', 'sgd', 'adagrad','adadelta', 'adamax', 'nadam']
}
elif dataset == 'mnist_mlp':
generations = 8 # Number of times to evolve the population.
all_possible_genes = {
'nb_neurons': [64, 128], #, 256, 512, 768, 1024],
'nb_layers': [1, 2, 3, 4, 5],
'activation': ['relu', 'elu', 'tanh', 'sigmoid', 'hard_sigmoid','softplus','linear'],
'optimizer': ['rmsprop', 'adam', 'sgd', 'adagrad','adadelta', 'adamax', 'nadam']
}
elif dataset == 'cifar10_mlp':
generations = 8 # Number of times to evolve the population.
all_possible_genes = {
'nb_neurons': [64, 128, 256, 512, 768, 1024],
'nb_layers': [1, 2, 3, 4, 5],
'activation': ['relu', 'elu', 'tanh', 'sigmoid', 'hard_sigmoid','softplus','linear'],
'optimizer': ['rmsprop', 'adam', 'sgd', 'adagrad','adadelta', 'adamax', 'nadam']
}
elif dataset == 'cifar10_cnn':
generations = 8 # Number of times to evolve the population.
all_possible_genes = {
'nb_neurons': [16, 32, 64, 128],
'nb_layers': [1, 2, 3, 4, 5],
'activation': ['relu', 'elu', 'tanh', 'sigmoid', 'hard_sigmoid','softplus','linear'],
'optimizer': ['rmsprop', 'adam', 'sgd', 'adagrad','adadelta', 'adamax', 'nadam']
}
else:
generations = 8 # Number of times to evolve the population.
all_possible_genes = {
'nb_neurons': [64, 128, 256, 512, 768, 1024],
'nb_layers': [1, 2, 3, 4, 5],
'activation': ['relu', 'elu', 'tanh', 'sigmoid', 'hard_sigmoid','softplus','linear'],
'optimizer': ['rmsprop', 'adam', 'sgd', 'adagrad','adadelta', 'adamax', 'nadam']
}
# replace nb_neurons with 1 unique value for each layer
# 6th value reserved for dense layer
nb_neurons = all_possible_genes['nb_neurons']
for i in range(1,7):
all_possible_genes['nb_neurons_' + str(i)] = nb_neurons
# remove old value from dict
all_possible_genes.pop('nb_neurons')
print("***Evolving for %d generations with population size = %d***" % (generations, population))
generate(generations, population, all_possible_genes, dataset)
if __name__ == '__main__':
main()