-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutator.py
75 lines (57 loc) · 2.09 KB
/
mutator.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
import numpy as np
'''
mutations:
add_neuron, remove_neuron, modify_weight, modify_bias
USAGE:
mutator = Mutator()
mutator.mutate(layer, weights, biases)
'''
class Mutator:
def __init__(self):
pass
def mutate(self, layer, weights, biases):
random = np.random.uniform(0, 1)
if random < 0.25:
return self.add_neuron(layer, weights, biases)
elif random < 0.5:
return self.remove_neuron(layer, weights, biases)
elif random < 0.75:
return self.modify_weight(layer, weights), biases
else:
return weights, self.modify_bias(layer, biases)
def add_neuron(self, layer, weights, biases):
weights[layer - 1] = np.concatenate((
weights[layer - 1],
np.random.randn(1, len(weights[layer - 1][0]))
))
weights[layer] = np.append(
weights[layer],
[[np.random.randn()] for _ in range(len(weights[layer]))],
axis=1
)
biases[layer - 1] = np.concatenate((
biases[layer - 1],
np.random.randn(1, 1)
))
return weights, biases
def remove_neuron(self, layer, weights, biases):
weights[layer - 1] = weights[layer - 1][:-1]
weights[layer] = np.delete(weights[layer], len(weights[layer]) - 1, axis=1)
biases[layer - 1] = biases[layer - 1][:-1]
return weights, biases
def modify_weight(self, layer, weights):
shape = np.shape(weights[layer])
new_weights = np.random.randn(shape[0], shape[1])
if np.random.uniform(0, 1) > 0.5:
weights[layer] = np.add(weights[layer], new_weights)
else:
weights[layer] = np.subtract(weights[layer], new_weights)
return weights
def modify_bias(self, layer, biases):
shape = np.shape(biases[layer])
new_biases = np.random.randn(shape[0], shape[1])
if np.random.uniform(0, 1) > 0.5:
biases[layer] = np.add(biases[layer], new_biases)
else:
biases[layer] = np.subtract(biases[layer], new_biases)
return biases