forked from joeddav/devol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genome_handler.py
217 lines (202 loc) · 9.16 KB
/
genome_handler.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import numpy as np
import random as rand
import math
from keras.models import Sequential
from keras.layers import Activation, Dense, Dropout, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
##################################
# Genomes are represented as fixed-with lists of integers corresponding
# to sequential layers and properties. A model with 2 convolutional layers
# and 1 dense layer would look like:
#
# [<conv layer><conv layer><dense layer><optimizer>]
#
# The makeup of the convolutional layers and dense layers is defined in the
# GenomeHandler below under self.convolutional_layer_shape and
# self.dense_layer_shape. <optimizer> consists of just one property.
###################################
class GenomeHandler:
def __init__(self, max_conv_layers, max_dense_layers, max_filters, max_dense_nodes,
input_shape, n_classes, batch_normalization=True, dropout=True, max_pooling=True,
optimizers=None, activations=None):
if max_dense_layers < 1:
raise ValueError("At least one dense layer is required for softmax layer")
filter_range_max = int(math.log(max_filters, 2)) + 1 if max_filters > 0 else 0
self.optimizer = optimizers or [
'adam',
'rmsprop',
'adagrad',
'adadelta'
]
self.activation = activations or [
'relu',
'sigmoid',
]
self.convolutional_layer_shape = [
"active",
"num filters",
"batch normalization",
"activation",
"dropout",
"max pooling",
]
self.dense_layer_shape = [
"active",
"num nodes",
"batch normalization",
"activation",
"dropout",
]
self.layer_params = {
"active": [0, 1],
"num filters": [2**i for i in range(3, filter_range_max)],
"num nodes": [2**i for i in range(4, int(math.log(max_dense_nodes, 2)) + 1)],
"batch normalization": [0, (1 if batch_normalization else 0)],
"activation": list(range(len(self.activation))),
"dropout": [(i if dropout else 0) for i in range(11)],
"max pooling": list(range(3)) if max_pooling else 0,
}
self.convolution_layers = max_conv_layers
self.convolution_layer_size = len(self.convolutional_layer_shape)
self.dense_layers = max_dense_layers - 1 # this doesn't include the softmax layer, so -1
self.dense_layer_size = len(self.dense_layer_shape)
self.input_shape = input_shape
self.n_classes = n_classes
def convParam(self, i):
key = self.convolutional_layer_shape[i]
return self.layer_params[key]
def denseParam(self, i):
key = self.dense_layer_shape[i]
return self.layer_params[key]
def mutate(self, genome, num_mutations):
num_mutations = np.random.choice(num_mutations)
for i in range(num_mutations):
index = np.random.choice(list(range(1, len(genome))))
if index < self.convolution_layer_size * self.convolution_layers:
if genome[index - index % self.convolution_layer_size]:
range_index = index % self.convolution_layer_size
choice_range = self.convParam(range_index)
genome[index] = np.random.choice(choice_range)
elif rand.uniform(0, 1) <= 0.01: # randomly flip deactivated layers
genome[index - index % self.convolution_layer_size] = 1
elif index != len(genome) - 1:
offset = self.convolution_layer_size * self.convolution_layers
new_index = (index - offset)
present_index = new_index - new_index % self.dense_layer_size
if genome[present_index + offset]:
range_index = new_index % self.dense_layer_size
choice_range = self.denseParam(range_index)
genome[index] = np.random.choice(choice_range)
elif rand.uniform(0, 1) <= 0.01:
genome[present_index + offset] = 1
else:
genome[index] = np.random.choice(list(range(len(self.optimizer))))
return genome
def decode(self, genome):
if not self.is_compatible_genome(genome):
raise ValueError("Invalid genome for specified configs")
model = Sequential()
offset = 0
dim = min(self.input_shape[:-1]) # keep track of smallest dimension
input_layer = True
for i in range(self.convolution_layers):
if genome[offset]:
convolution = None
if input_layer:
convolution = Convolution2D(
genome[offset + 1], (3, 3),
padding='same',
input_shape=self.input_shape)
input_layer = False
else:
convolution = Convolution2D(
genome[offset + 1], (3, 3),
padding='same')
model.add(convolution)
if genome[offset + 2]:
model.add(BatchNormalization())
model.add(Activation(self.activation[genome[offset + 3]]))
model.add(Dropout(float(genome[offset + 4] / 20.0)))
max_pooling_type = genome[offset + 5]
# must be large enough for a convolution
if max_pooling_type == 1 and dim >= 5:
model.add(MaxPooling2D(pool_size=(2, 2), padding="same"))
dim = int(math.ceil(dim / 2))
offset += self.convolution_layer_size
if not input_layer:
model.add(Flatten())
for i in range(self.dense_layers):
if genome[offset]:
dense = None
if input_layer:
dense = Dense(genome[offset + 1], input_shape=self.input_shape)
input_layer = False
else:
dense = Dense(genome[offset + 1])
model.add(dense)
if genome[offset + 2]:
model.add(BatchNormalization())
model.add(Activation(self.activation[genome[offset + 3]]))
model.add(Dropout(float(genome[offset + 4] / 20.0)))
offset += self.dense_layer_size
model.add(Dense(self.n_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=self.optimizer[genome[offset]],
metrics=["accuracy"])
return model
def genome_representation(self):
encoding = []
for i in range(self.convolution_layers):
for key in self.convolutional_layer_shape:
encoding.append("Conv" + str(i) + " " + key)
for i in range(self.dense_layers):
for key in self.dense_layer_shape:
encoding.append("Dense" + str(i) + " " + key)
encoding.append("Optimizer")
return encoding
def generate(self):
genome = []
for i in range(self.convolution_layers):
for key in self.convolutional_layer_shape:
param = self.layer_params[key]
genome.append(np.random.choice(param))
for i in range(self.dense_layers):
for key in self.dense_layer_shape:
param = self.layer_params[key]
genome.append(np.random.choice(param))
genome.append(np.random.choice(list(range(len(self.optimizer)))))
genome[0] = 1
return genome
def is_compatible_genome(self, genome):
expected_len = self.convolution_layers * self.convolution_layer_size \
+ self.dense_layers * self.dense_layer_size + 1
if len(genome) != expected_len:
return False
ind = 0
for i in range(self.convolution_layers):
for j in range(self.convolution_layer_size):
if genome[ind + j] not in self.convParam(j):
return False
ind += self.convolution_layer_size
for i in range(self.dense_layers):
for j in range(self.dense_layer_size):
if genome[ind + j] not in self.denseParam(j):
return False
ind += self.dense_layer_size
if genome[ind] not in range(len(self.optimizer)):
return False
return True
# metric = accuracy or loss
def best_genome(self, csv_path, metric="accuracy", include_metrics=True):
best = max if metric is "accuracy" else min
col = -1 if metric is "accuracy" else -2
data = np.genfromtxt(csv_path, delimiter=",")
row = list(data[:, col]).index(best(data[:, col]))
genome = list(map(int, data[row, :-2]))
if include_metrics:
genome += list(data[row, -2:])
return genome
# metric = accuracy or loss
def decode_best(self, csv_path, metric="accuracy"):
return self.decode(self.best_genome(csv_path, metric, False))