-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutations.py
87 lines (71 loc) · 2.56 KB
/
mutations.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from representations import (INSERTABLE,
MUTABLE_PARAMS,
REPR_MAKERS,
check_validity)
import representations
import numpy as np
import random
containerIndividual = None
getRandomLayer = None
def setIndividual(container):
'''
Sets the individual Object to the mutations global class.
In order to generate a representation of type 'Individual', you need to call the variable.
Example:
containerIndividual(array) --> Individual([block1], [block2], [block3])
'''
global containerIndividual
containerIndividual = container
def setInitialization(func):
global getRandomLayer
getRandomLayer = func
def mutate_layer(layer, verbose=False):
'''
Looks up the initializer function for a type,
and replaces it with a new initialization.
Appended [0] because you need to access the container inside the
Individual({type: conv2dpool, params})
class.
'''
for elem in REPR_MAKERS:
if layer['type'] == elem:
layer = REPR_MAKERS[elem]()
if verbose:
print('mutate layer {}'.format(layer['type']))
return layer
def mutate_network(reprRaw, mutations=1, verbose=False, appendRemoveProb=0.6):
'''
Mutates a whole representation network.
Arguments:
appendRemoveProb: Probability that a block is being appended/removed.
The inverse is the probability of mutating a block itself (so not
changing the structure)
'''
repr = reprRaw.tolist()
if mutations > len(repr):
mutations = len(repr)
if verbose:
print("MUTATING %d BLOCKS OF NETWORK" % mutations)
if np.random.random() < appendRemoveProb:
if np.random.random() < 0.5 or len(repr) <= 2:
repr.append(getRandomLayer())
if verbose:
print("APPEND BLOCK ELEM: ", len(repr))
print("NEW BLOCK ELEM: ", len(repr))
else:
random.shuffle(repr)
repr.pop()
if verbose:
print("REMOVE BLOCK ELEM: ", len(repr))
print("NEW BLOCK ELEM: ", len(repr))
else:
for layerIndex in np.random.randint(0, len(repr), mutations):
repr[layerIndex] = mutate_layer(repr[layerIndex], verbose=verbose)
while not check_validity(repr):
random.shuffle(repr)
repr.pop()
if verbose:
print('Network valid: {}'.format(check_validity(repr)))
return containerIndividual(repr),