-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
403 lines (314 loc) · 12.6 KB
/
models.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
'''
Created on Jul 6, 2018
@author: urishaham
Resnet code is based on: https://github.com/tensorflow/models/blob/master/official/resnet/resnet_model.py
VAE code, tflib, pylib are based on https://github.com/LynnHo/VAE-Tensorflow
Transformer code is based on https://github.com/Kyubyong/transformer
'''
from functools import partial
import tensorflow as tf
import tensorflow.contrib.slim as slim
import tflib as tl
fc = partial(tl.flatten_fully_connected, activation_fn=None)
lrelu = tf.nn.leaky_relu
relu = tf.nn.relu
batch_norm = partial(slim.batch_norm, scale=True, updates_collections=None)
def mlp():
def Enc(inputs,
hidden_dim=20,
code_dim=5,
is_training=True):
with tf.variable_scope('Encoder', reuse=tf.AUTO_REUSE):
y = fc(inputs, hidden_dim)
y = lrelu(y)
y = fc(y, hidden_dim)
y = lrelu(y)
c_mu = fc(y, code_dim)
c_log_sigma_sq = fc(y, code_dim)
return c_mu, c_log_sigma_sq
def Dec_a(code,
output_dim,
hidden_dim=20,
is_training=True):
with tf.variable_scope('Decoder_a', reuse=tf.AUTO_REUSE):
y = fc(code, hidden_dim)
y = lrelu(y)
y = fc(y, hidden_dim)
y = lrelu(y)
recon = fc(y, output_dim)
return recon
def Dec_b(code,
output_dim,
hidden_dim=20,
is_training=True):
with tf.variable_scope('Decoder_b', reuse=tf.AUTO_REUSE):
y = fc(code, hidden_dim)
y = lrelu(y)
y = fc(y, hidden_dim)
y = lrelu(y)
recon = fc(y, output_dim)
return recon
def Disc(code,
hidden_dim=20):
with tf.variable_scope('discriminator', reuse=tf.AUTO_REUSE):
y = fc(code, hidden_dim)
y = lrelu(y)
y = fc(y, hidden_dim)
y = lrelu(y)
output = fc(y, 1)
return output
return Enc, Dec_a, Dec_b, Disc
def _resnet_block_v2(inputs,
block_dim,
is_training,
reuse=tf.AUTO_REUSE):
with tf.variable_scope("resnet_block", reuse=reuse):
shortcut = inputs
inputs = batch_norm(inputs, is_training)
inputs = lrelu(inputs)
inputs = fc(inputs, block_dim)
inputs = batch_norm(inputs, is_training)
inputs = lrelu(inputs)
inputs = fc(inputs, block_dim)
return inputs + shortcut
def resnet():
def Enc(inputs,
n_blocks=3,
block_dim=20,
code_dim=5,
is_training=True):
with tf.variable_scope('Encoder', reuse=tf.AUTO_REUSE):
inputs = batch_norm(inputs, is_training)
y = lrelu(inputs)
y = fc(y, block_dim)
for _ in range(n_blocks):
y = _resnet_block_v2(y, block_dim, is_training)
c_mu = fc(y, code_dim)
c_log_sigma_sq = fc(y, code_dim)
return c_mu, c_log_sigma_sq
def Dec_a(code,
output_dim,
n_blocks=3,
block_dim=20,
is_training=True):
with tf.variable_scope('Decoder_a', reuse=tf.AUTO_REUSE):
code = batch_norm(code, is_training)
y = lrelu(code)
y = fc(y, block_dim)
for _ in range(n_blocks):
y = _resnet_block_v2(y, block_dim, is_training)
recon = fc(y, output_dim)
return recon
def Dec_b(code,
output_dim,
n_blocks=3,
block_dim=20,
is_training=True):
with tf.variable_scope('Decoder_b', reuse=tf.AUTO_REUSE):
code = batch_norm(code, is_training)
y = lrelu(code)
y = fc(y, block_dim)
for _ in range(n_blocks):
y = _resnet_block_v2(y, block_dim, is_training)
recon = fc(y, output_dim)
return recon
def Disc(code,
n_blocks=3,
block_dim=20,
is_training=True):
with tf.variable_scope('discriminator', reuse=tf.AUTO_REUSE):
code = batch_norm(code, is_training)
y = lrelu(code)
y = fc(y, block_dim)
for _ in range(n_blocks):
y = _resnet_block_v2(y, block_dim, is_training)
output = fc(y, 1)
return output
return Enc, Dec_a, Dec_b, Disc
def _normalize(inputs,
epsilon = 1e-8,
scope="ln",
reuse=tf.AUTO_REUSE):
'''Applies layer normalization.
Args:
inputs: A tensor with 2 or more dimensions, where the first dimension has
`batch_size`.
epsilon: A floating number. A very small number for preventing ZeroDivision Error.
scope: Optional scope for `variable_scope`.
reuse: Boolean, whether to reuse the weights of a previous layer
by the same name.
Returns:
A tensor with the same shape and data dtype as `inputs`.
'''
with tf.variable_scope(scope, reuse=reuse):
inputs_shape = inputs.get_shape()
params_shape = inputs_shape[-1:]
mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True)
beta= tf.Variable(tf.zeros(params_shape))
gamma = tf.Variable(tf.ones(params_shape))
normalized = (inputs - mean) / ( (variance + epsilon) ** (.5) )
outputs = gamma * normalized + beta
return outputs
def _multihead_attention(keys,
is_training,
num_units=20,
num_heads=5,
dropout_rate=0,
reuse=tf.AUTO_REUSE):
'''Applies multihead attention.
Args:
keys: A 2d tensor with shape of [N, h].
num_units: A scalar. Attention size.
dropout_rate: A floating point number.
is_training: Boolean. Controller of mechanism for dropout.
num_heads: An int. Number of heads.
reuse: Boolean, whether to reuse the weights of a previous layer
by the same name.
Returns
A 2d tensor with shape of (N, h)
'''
with tf.variable_scope("multihead_attention", reuse=reuse):
# Linear projections
proj = tf.layers.dense(keys, num_units * num_heads, activation=lrelu) # (N, c*h)
attn_weights = tf.layers.dense(keys, num_units * num_heads, activation=lrelu) # (N, c*h)
# Split and concat
proj_ = tf.concat(tf.split(proj, num_heads, axis=1), axis=0) # (h*N, c)
attn_weights_ = tf.concat(tf.split(attn_weights, num_heads, axis=1), axis=0) # (h*N, c)
# Activation
attn_weights_ = tf.nn.softmax(attn_weights_) # (h*N, c)
# Weighted sum
outputs = tf.reduce_sum(tf.multiply(proj_, attn_weights_),1, keep_dims=False)# (h*N)
# Restore shape
outputs = tf.concat(tf.split(tf.expand_dims(outputs,1), num_heads, axis=0), axis=1) # (N, h)
# Residual connection
outputs += keys # (N, h)
# Normalize
#outputs = _normalize(outputs) # (N, h)
return outputs
def _feedforward(inputs,
num_units=20,
reuse=tf.AUTO_REUSE):
'''Point-wise feed forward net.
Args:
inputs: A 2d tensor with shape of [N, h].
num_units: an integer, should be same as the same hyperparam in multihead_attention
reuse: Boolean, whether to reuse the weights of a previous layer
by the same name.
Returns:
A 2d tensor with the same shape and dtype as inputs
'''
with tf.variable_scope("forward", reuse=reuse):
input_dim = inputs.get_shape().as_list()[-1]
# Inner layer
outputs = fc(inputs, num_units)
outputs = lrelu(outputs)
# Readout layer
outputs = fc(outputs, input_dim)
outputs = lrelu(outputs)
# Residual connection
outputs += inputs
# Normalize
#outputs = _normalize(outputs)
return outputs
def transformer():
def Enc(inputs,
n_blocks=3,
num_units=10,
num_heads=8,
code_dim=5,
is_training=True,
dropout_rate=0):
with tf.variable_scope('Encoder', reuse=tf.AUTO_REUSE):
y = fc(inputs, num_heads)
y = lrelu(y)
for _ in range(n_blocks):
y = _multihead_attention(keys=y,
num_units=num_units,
num_heads=num_heads,
dropout_rate=dropout_rate,
is_training=is_training)
#y = _feedforward(y,
# num_units=num_units)
c_mu = fc(y, code_dim)
c_log_sigma_sq = fc(y, code_dim)
return c_mu, c_log_sigma_sq
def Dec_a(code,
output_dim,
n_blocks=3,
num_units=10,
num_heads=8,
is_training=True,
dropout_rate=0):
with tf.variable_scope('Decoder_a', reuse=tf.AUTO_REUSE):
y = fc(code, num_heads)
y = lrelu(y)
for _ in range(n_blocks):
y = _multihead_attention(keys=y,
num_units=num_units,
num_heads=num_heads,
dropout_rate=dropout_rate,
is_training=is_training)
y = _feedforward(y,
num_units=num_units)
recon = fc(y, output_dim)
return recon
def Dec_b(code,
output_dim,
n_blocks=3,
num_units=10,
num_heads=8,
is_training=True,
dropout_rate=0):
with tf.variable_scope('Decoder_b', reuse=tf.AUTO_REUSE):
y = fc(code, num_heads)
y = lrelu(y)
for _ in range(n_blocks):
y = _multihead_attention(keys=y,
num_units=num_units,
num_heads=num_heads,
dropout_rate=dropout_rate,
is_training=is_training)
y = _feedforward(y,
num_units=num_units)
recon = fc(y, output_dim)
return recon
def Disc(code,
n_blocks=3,
num_units=10,
num_heads=8,
is_training=True,
dropout_rate=0):
with tf.variable_scope('discriminator', reuse=tf.AUTO_REUSE):
y = fc(code, num_heads)
y = lrelu(y)
for _ in range(n_blocks):
y = _multihead_attention(keys=y,
num_units=num_units,
num_heads=num_heads,
dropout_rate=dropout_rate,
is_training=is_training)
y = _feedforward(y,
num_units=num_units)
output = fc(y, 1)
return output
return Enc, Dec_a, Dec_b, Disc
def Cell_type_classifier():
def cell_type_classifier(inputs,
num_classes=5,
is_training=False,
dropout_keep_prob=0.5,
prediction_fn=slim.softmax,
scope='classifier'):
end_points = {}
with tf.variable_scope('cell_type_classifier', reuse=tf.AUTO_REUSE):
net = fc(inputs, 20, scope='fc1')
net = end_points['fc1'] = lrelu(net)
net = fc(net, 20, scope='fc2')
net = end_points['fc2'] = lrelu(net)
net = slim.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout')
logits = end_points['Logits'] = slim.fully_connected(
net, num_classes, activation_fn=None, scope='logits')
end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
return logits, end_points
return cell_type_classifier