-
Notifications
You must be signed in to change notification settings - Fork 0
/
representations.py
148 lines (122 loc) · 3.64 KB
/
representations.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from keras.layers import (
Conv2D,
Dense,
Dropout,
Flatten,
GaussianNoise,
Input,
MaxPooling2D,
)
from keras.models import Model
import numpy as np
def make_base_repr():
return {
'type': None,
'params': {},
}
def make_dense_repr():
layer = make_base_repr()
layer['type'] = 'dense'
layer['params']['units'] = 2**np.random.choice(range(3,7))
layer['params']['kernel_initializer'] = 'glorot_normal'
return layer
def make_flatten_repr():
layer = make_base_repr()
layer['type'] = 'flatten'
return layer
def make_conv2d_repr():
layer = make_base_repr()
layer['type'] = 'conv2d'
layer['params']['filters'] = 2**np.random.choice(range(4, 9))
layer['params']['kernel_size'] = 3
layer['params']['kernel_initializer'] = 'glorot_normal'
layer['params']['activation'] = 'relu'
return layer
def make_noise_repr():
layer = make_base_repr()
layer['type'] = 'noise'
layer['params']['stddev'] = np.random.random()
return layer
def make_conv2d_dropout_repr():
layer = make_conv2d_repr()
layer['type'] = 'conv2ddropout'
layer['params']['rate'] = np.around(np.random.uniform(low=.1, high=.5),
decimals=1)
return layer
def make_conv2d_pool_repr():
layer = make_conv2d_repr()
layer['type'] = 'conv2dpool'
layer['params']['pool_size'] = 2
return layer
REPR_MAKERS = {
'conv2d': make_conv2d_repr,
'conv2ddropout': make_conv2d_dropout_repr,
'conv2dpool': make_conv2d_pool_repr,
'noise': make_noise_repr,
'flatten': make_flatten_repr,
'dense': make_dense_repr,
}
MUTABLE_PARAMS = {
'conv2d': [
'filters'
],
'conv2ddropout': [
'rate'
],
}
INSERTABLE = [
'conv2d',
'conv2ddropout',
'noise',
'flatten',
'dense',
]
REPR2LAYER = {
'conv2d': Conv2D,
'dense': Dense,
'dropout': Dropout,
'flatten': Flatten,
'noise': GaussianNoise,
'pool': MaxPooling2D,
}
def repr2layer(r):
return REPR2LAYER[r['type']](**r['params'])
def reprs2nn(reprs, shape):
inputs = Input(shape=shape)
x = inputs
for r in reprs:
if r['type'] == 'conv2ddropout':
x = repr2layer({'type': 'conv2d', 'params':
{x[0]: x[1] for x in r['params'].items()
if x[0] in make_conv2d_repr()['params'].keys()}})(x)
x = repr2layer({'type': 'dropout', 'params':
{x[0]: x[1] for x in r['params'].items()
if x[0] == 'rate'}})(x)
elif r['type'] == 'conv2dpool':
x = repr2layer({'type': 'conv2d', 'params':
{x[0]: x[1] for x in r['params'].items()
if x[0] in make_conv2d_repr()['params'].keys()}})(x)
x = repr2layer({'type': 'pool', 'params':
{x[0]: x[1] for x in r['params'].items()
if x[0] == 'pool_size'}})(x)
else:
x = repr2layer(r)(x)
x = Flatten()(x)
outputs = Dense(10, activation='softmax')(x)
return Model(inputs, outputs)
def check_validity(reprs, dataset='fashion'):
assert dataset in ['fashion', 'cifar10']
if dataset == 'fashion':
start_size = 28
elif dataset == 'cifar10':
start_size = 32
for layer in reprs:
t = layer['type']
p = layer['params']
if t.startswith('conv2d'):
start_size -= p['kernel_size'] - 1
if 'pool' in t:
start_size /= p['pool_size']
return start_size >= 1