-
Notifications
You must be signed in to change notification settings - Fork 110
/
encoders_decoders.py
executable file
·243 lines (178 loc) · 9.93 KB
/
encoders_decoders.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'''
Created on February 4, 2017
@author: optas
'''
import tensorflow as tf
import numpy as np
import warnings
from tflearn.layers.core import fully_connected, dropout
from tflearn.layers.conv import conv_1d, avg_pool_1d, highway_conv_1d
from tflearn.layers.normalization import batch_normalization
from tflearn.layers.core import fully_connected, dropout
from .. fundamentals.utils import expand_scope_by_name, replicate_parameter_for_all_layers
def encoder_with_convs_and_symmetry_new(in_signal, n_filters=[64, 128, 256, 1024], filter_sizes=[1], strides=[1],
b_norm=True, non_linearity=tf.nn.relu, regularizer=None, weight_decay=0.001,
symmetry=tf.reduce_max, dropout_prob=None, pool=avg_pool_1d, pool_sizes=None, scope=None,
reuse=False, padding='same', verbose=False, closing=None, conv_op=conv_1d):
'''An Encoder (recognition network), which maps inputs onto a latent space.
'''
if verbose:
print 'Building Encoder'
n_layers = len(n_filters)
filter_sizes = replicate_parameter_for_all_layers(filter_sizes, n_layers)
strides = replicate_parameter_for_all_layers(strides, n_layers)
dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers)
if n_layers < 2:
raise ValueError('More than 1 layers are expected.')
for i in xrange(n_layers):
if i == 0:
layer = in_signal
name = 'encoder_conv_layer_' + str(i)
scope_i = expand_scope_by_name(scope, name)
layer = conv_op(layer, nb_filter=n_filters[i], filter_size=filter_sizes[i], strides=strides[i], regularizer=regularizer,
weight_decay=weight_decay, name=name, reuse=reuse, scope=scope_i, padding=padding)
if verbose:
print name, 'conv params = ', np.prod(layer.W.get_shape().as_list()) + np.prod(layer.b.get_shape().as_list()),
if b_norm:
name += '_bnorm'
scope_i = expand_scope_by_name(scope, name)
layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i)
if verbose:
print 'bnorm params = ', np.prod(layer.beta.get_shape().as_list()) + np.prod(layer.gamma.get_shape().as_list())
if non_linearity is not None:
layer = non_linearity(layer)
if pool is not None and pool_sizes is not None:
if pool_sizes[i] is not None:
layer = pool(layer, kernel_size=pool_sizes[i])
if dropout_prob is not None and dropout_prob[i] > 0:
layer = dropout(layer, 1.0 - dropout_prob[i])
if verbose:
print layer
print 'output size:', np.prod(layer.get_shape().as_list()[1:]), '\n'
if symmetry is not None:
layer = symmetry(layer, axis=1)
if verbose:
print layer
if closing is not None:
layer = closing(layer)
print layer
return layer
def encoder_with_convs_and_symmetry(in_signal, n_filters=[64, 128, 256, 1024], filter_sizes=[1], strides=[1],
b_norm=True, spn=False, non_linearity=tf.nn.relu, regularizer=None, weight_decay=0.001,
symmetry=tf.reduce_max, dropout_prob=None, scope=None, reuse=False):
'''An Encoder (recognition network), which maps inputs onto a latent space.
'''
warnings.warn('Using old architecture.')
n_layers = len(n_filters)
filter_sizes = replicate_parameter_for_all_layers(filter_sizes, n_layers)
strides = replicate_parameter_for_all_layers(strides, n_layers)
dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers)
if n_layers < 2:
raise ValueError('More than 1 layers are expected.')
name = 'encoder_conv_layer_0'
scope_i = expand_scope_by_name(scope, name)
layer = conv_1d(in_signal, nb_filter=n_filters[0], filter_size=filter_sizes[0], strides=strides[0], regularizer=regularizer, weight_decay=weight_decay, name=name, reuse=reuse, scope=scope_i)
if b_norm:
name += '_bnorm'
scope_i = expand_scope_by_name(scope, name)
layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i)
layer = non_linearity(layer)
if dropout_prob is not None and dropout_prob[0] > 0:
layer = dropout(layer, 1.0 - dropout_prob[0])
for i in xrange(1, n_layers):
name = 'encoder_conv_layer_' + str(i)
scope_i = expand_scope_by_name(scope, name)
layer = conv_1d(layer, nb_filter=n_filters[i], filter_size=filter_sizes[i], strides=strides[i], regularizer=regularizer, weight_decay=weight_decay, name=name, reuse=reuse, scope=scope_i)
if b_norm:
name += '_bnorm'
#scope_i = expand_scope_by_name(scope, name) # FORGOT TO PUT IT BEFORE ICLR
layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i)
layer = non_linearity(layer)
if dropout_prob is not None and dropout_prob[i] > 0:
layer = dropout(layer, 1.0 - dropout_prob[i])
if symmetry is not None:
layer = symmetry(layer, axis=1)
return layer
def decoder_with_fc_only(latent_signal, layer_sizes=[], b_norm=True, non_linearity=tf.nn.relu,
regularizer=None, weight_decay=0.001, reuse=False, scope=None, dropout_prob=None,
b_norm_finish=False, verbose=False):
'''A decoding network which maps points from the latent space back onto the data space.
'''
if verbose:
print 'Building Decoder'
n_layers = len(layer_sizes)
dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers)
if n_layers < 2:
raise ValueError('For an FC decoder with single a layer use simpler code.')
for i in xrange(0, n_layers - 1):
name = 'decoder_fc_' + str(i)
scope_i = expand_scope_by_name(scope, name)
if i == 0:
layer = latent_signal
layer = fully_connected(layer, layer_sizes[i], activation='linear', weights_init='xavier', name=name, regularizer=regularizer, weight_decay=weight_decay, reuse=reuse, scope=scope_i)
if verbose:
print name, 'FC params = ', np.prod(layer.W.get_shape().as_list()) + np.prod(layer.b.get_shape().as_list()),
if b_norm:
name += '_bnorm'
scope_i = expand_scope_by_name(scope, name)
layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i)
if verbose:
print 'bnorm params = ', np.prod(layer.beta.get_shape().as_list()) + np.prod(layer.gamma.get_shape().as_list())
if non_linearity is not None:
layer = non_linearity(layer)
if dropout_prob is not None and dropout_prob[i] > 0:
layer = dropout(layer, 1.0 - dropout_prob[i])
if verbose:
print layer
print 'output size:', np.prod(layer.get_shape().as_list()[1:]), '\n'
# Last decoding layer never has a non-linearity.
name = 'decoder_fc_' + str(n_layers - 1)
scope_i = expand_scope_by_name(scope, name)
layer = fully_connected(layer, layer_sizes[n_layers - 1], activation='linear', weights_init='xavier', name=name, regularizer=regularizer, weight_decay=weight_decay, reuse=reuse, scope=scope_i)
if verbose:
print name, 'FC params = ', np.prod(layer.W.get_shape().as_list()) + np.prod(layer.b.get_shape().as_list()),
if b_norm_finish:
name += '_bnorm'
scope_i = expand_scope_by_name(scope, name)
layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i)
if verbose:
print 'bnorm params = ', np.prod(layer.beta.get_shape().as_list()) + np.prod(layer.gamma.get_shape().as_list())
if verbose:
print layer
print 'output size:', np.prod(layer.get_shape().as_list()[1:]), '\n'
return layer
def decoder_with_convs_only(in_signal, n_filters, filter_sizes, strides, padding='same', b_norm=True, non_linearity=tf.nn.relu,
conv_op=conv_1d, regularizer=None, weight_decay=0.001, dropout_prob=None, upsample_sizes=None,
b_norm_finish=False, scope=None, reuse=False, verbose=False):
if verbose:
print 'Building Decoder'
n_layers = len(n_filters)
filter_sizes = replicate_parameter_for_all_layers(filter_sizes, n_layers)
strides = replicate_parameter_for_all_layers(strides, n_layers)
dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers)
for i in xrange(n_layers):
if i == 0:
layer = in_signal
name = 'decoder_conv_layer_' + str(i)
scope_i = expand_scope_by_name(scope, name)
layer = conv_op(layer, nb_filter=n_filters[i], filter_size=filter_sizes[i],
strides=strides[i], padding=padding, regularizer=regularizer, weight_decay=weight_decay,
name=name, reuse=reuse, scope=scope_i)
if verbose:
print name, 'conv params = ', np.prod(layer.W.get_shape().as_list()) + np.prod(layer.b.get_shape().as_list()),
if (b_norm and i < n_layers - 1) or (i == n_layers - 1 and b_norm_finish):
name += '_bnorm'
scope_i = expand_scope_by_name(scope, name)
layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i)
if verbose:
print 'bnorm params = ', np.prod(layer.beta.get_shape().as_list()) + np.prod(layer.gamma.get_shape().as_list())
if non_linearity is not None and i < n_layers - 1: # Last layer doesn't have a non-linearity.
layer = non_linearity(layer)
if dropout_prob is not None and dropout_prob[i] > 0:
layer = dropout(layer, 1.0 - dropout_prob[i])
if upsample_sizes is not None and upsample_sizes[i] is not None:
layer = tf.tile(layer, multiples=[1, upsample_sizes[i], 1])
if verbose:
print layer
print 'output size:', np.prod(layer.get_shape().as_list()[1:]), '\n'
return layer