forked from Hughes-Genome-Group/deepC
-
Notifications
You must be signed in to change notification settings - Fork 5
/
deepCregr.py
472 lines (426 loc) · 19.2 KB
/
deepCregr.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
"""Tensorflow implementation of DeepC using convolutional and dilated layers.
------------------------------------------------------------------
Acknowledgement:
Code for performing dilated convolutions has been adapted from
https://github.com/ibab/tensorflow-wavenet
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import re
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# OPERATIONS DEFINITION ============================================
def time_to_batch(value, dilation, name=None):
with tf.name_scope('time_to_batch'):
shape = tf.shape(value)
pad_elements = dilation - 1 - (shape[1] + dilation - 1) % dilation
padded = tf.pad(value, [[0, 0], [0, pad_elements], [0, 0]])
reshaped = tf.reshape(padded, [-1, dilation, shape[2]])
transposed = tf.transpose(reshaped, perm=[1, 0, 2])
return tf.reshape(transposed, [shape[0] * dilation, -1, shape[2]])
def batch_to_time(value, dilation, name=None):
with tf.name_scope('batch_to_time'):
shape = tf.shape(value)
prepared = tf.reshape(value, [dilation, -1, shape[2]])
transposed = tf.transpose(prepared, perm=[1, 0, 2])
return tf.reshape(transposed,
[tf.math.divide(shape[0], dilation), -1, shape[2]])
def dilated_conv(value, filter_, dilation, name='dilated_conv'):
with tf.name_scope(name):
filter_width = tf.shape(filter_)[0]
if dilation > 1:
transformed = time_to_batch(value, dilation)
conv = tf.nn.conv1d(transformed, filter_, stride=1,
padding='SAME')
restored = batch_to_time(conv, dilation)
else:
restored = tf.nn.conv1d(value, filter_, stride=1, padding='SAME')
# Remove excess elements at the ends.
# desired width
out_width = tf.shape(value)[1]
# get difference
diff = out_width - tf.shape(restored)[1]
# get half difference to remove SAME padding zeros
diff = diff//2
result = tf.slice(restored,
[0, 0, 0],
[-1, out_width, -1])
return result
def _activation_summary(x):
"""Helper to create summaries for activations.
Creates a summary that provides a histogram of activations.
Creates a summary that measures the sparsity of activations.
Args:
x: Tensor
Returns:
nothing
"""
# session. This helps the clarity of presentation on tensorboard.
tensor_name = x.op.name
tf.compat.v1.summary.histogram(tensor_name + '/activations', x)
tf.compat.v1.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x))
def preload_variable(name, data):
'''Create variable from numpy data.'''
variable = tf.Variable(data, name=name)
return variable
def create_variable(name, shape):
'''Create a convolution filter variable with the specified name and shape,
and initialize it using Xavier initialition.'''
initializer = tf.compat.v1.keras.initializers.glorot_normal()
variable = tf.Variable(initializer(shape=shape), name=name)
# variable = tf.Variable(tf.truncated_normal(shape), name=name)
return variable
def create_bias_variable(name, shape):
'''Create a bias variable with the specified name and shape and initialize
it to zero.'''
initializer = tf.compat.v1.constant_initializer(value=0, dtype=tf.float32)
return tf.Variable(initializer(shape=shape), name=name)
def slice_seqs(positions, remainder, bp_context, batch_size, chrom_seq, translation_tensor):
with tf.name_scope('Get_Chrom_Seq'):
# adjust positions to match 3 base coding -->
# slice sequence from pre stored whole chrom sequence on gpu memory
indices = (tf.range(bp_context) + positions[:,tf.newaxis])[...,tf.newaxis]
print(indices.shape)
seqs = tf.gather_nd(chrom_seq, indices)
print('sequence shape after fetching from chrom')
print(seqs.get_shape().as_list())
seqs = tf.cast(seqs, 'int32')
# TRANSLATE BACK FROM 3 base chunk uin8 ENCODING
seqs = seqs[...,tf.newaxis]
seqs = tf.gather_nd(translation_tensor, seqs)
print('sequence shape fetching hot encoding')
print(seqs.get_shape().as_list())
seqs = tf.transpose(seqs, perm=[0,1,3,2])
seqs = tf.reshape(seqs, [batch_size,(bp_context*3),5])
# strip Ns / strip additional bases extracted from 3 base basis (remainder)
seqs = seqs[:,:,1:6]
print('sequence shape after translation')
print(seqs.get_shape().as_list())
# return sequences as batch size
return(seqs)
# Define Dilated Convolutional Layer
def dilated_layer(name,
input_batch,
dilation,
dilation_width,
dilation_units,
residual=False,
dense_residual=False,
to_batch_norm=False):
'''Create a dilation layer:
INPUT:
name: must be unique for graph purpose
input_batch: 3D input tensor batch, length, channels/width
Current implementation keeps the channels/hidden units intakt
dialton: dialtion rate to apply
dilation_width: with of the dilation filter (only 2 supported?)
dilation_units: dilation_units or channels
residual: True/False --> select if to propagate residual in that layer/stack
to_batch_norm: True/False select of to perform batch norm at every layer
RETURNS:
3D tensor batch, length-dilation rate, channels width'''
with tf.name_scope(name):
# get shapes
channels = input_batch.get_shape().as_list()[2]
# create variables
dilation_weights = create_variable('dilation_weights', [dilation_width, channels, dilation_units])
dilation_biases = create_bias_variable('dilation_biases', [dilation_units])
gate_weights = create_variable('gate_weights', [dilation_width, channels, dilation_units])
gate_biases = create_bias_variable('gate_biases', [dilation_units])
# redisual and skip
if residual == True:
if dense_residual == True:
dense_weights = create_variable('dense_weights', [dilation_units, channels, dilation_units])
dense_biases = create_bias_variable('dense_biases', [dilation_units])
# skip_weights = create_variable('skip_weights', [1, dilation_units, skip_units])
# skip_biases = create_bias_variable('skip_biases', [skip_units])
# define convolutional steps
dilated = tf.add(dilated_conv(input_batch, dilation_weights, dilation=dilation), dilation_biases)
gated = tf.add(dilated_conv(input_batch, gate_weights, dilation=dilation), gate_biases)
dilated_gated = tf.tanh(dilated) * tf.sigmoid(gated)
if residual == True:
# if dense residual connection desired make a 1x1 convolutiion before adding
if dense_residual == True:
# 1x1 dense convolution for residual
transformed = tf.nn.conv1d(
dilated_gated, dense_weights, stride=1, padding="SAME", name="dense")
transformed = transformed + dense_biases
# add up residual to 1x1 transformed output
out = input_batch + transformed
else:
# else just add input_batch shortcut to dilated/gated
out = input_batch + dilated_gated
else:
# else dilated gated is out
out = dilated_gated
# # The 1x1 conv to produce the skip output
# skip_cut = tf.shape(out)[1] - output_width
# out_skip = tf.slice(out, [0, skip_cut, 0], [-1, -1, -1])
# weights_skip = variables['skip']
# skip_contribution = tf.nn.conv1d(
# out_skip, weights_skip, stride=1, padding="SAME", name="skip")
# # batch norm
if to_batch_norm == True:
out = tf.layers.batch_normalization(out)
# make summary histograms of weights
tf.compat.v1.summary.histogram(name + '_dilation_weights', dilation_weights)
tf.compat.v1.summary.histogram(name + '_dilation_biases', dilation_biases)
tf.compat.v1.summary.histogram(name + '_gate_weights', gate_weights)
tf.compat.v1.summary.histogram(name + '_gate_biases', gate_biases)
if dense_residual == True:
tf.compat.v1.summary.histogram(name + '_dense_weights', dense_weights)
tf.compat.v1.summary.histogram(name + '_dense_biases', dense_biases)
# tf.compat.v1.summary.histogram(name + '_skip_weights', skip_weights)
# tf.compat.v1.summary.histogram(name + '_skip_biases', skip_biases)
return out
# Define Convolutional Layer
def convolutional_layer(name,
input_batch,
units,
kernel_width,
pool_width,
keep_prob,
to_seed,
seed_weights,
seed_biases,
to_batch_norm=False):
'''Create a convolutional layer:
INPUT:
name: must be unique for graph purpose
input_batch: 3D input tensor batch, length, channels/width
units: hidden units (# kernels)
kernel_width: width of the convolutional kernels/filters
pool_width: (max) pool width
keep_prob: dropout keep probability
to_seed: True / False if to pre seed weights and biases in this layer
seed_weights: numpy array of seed weights
seed_biases: numpy array of seed biases
to_batch_norm: True/False select of to perform batch norm at every layer
RETURNS:
3D tensor batch, length/pool_width, channels width'''
with tf.name_scope(name):
# get shapes
channels = input_batch.get_shape().as_list()[2]
# create variables
if to_seed:
weights = preload_variable("weights", seed_weights)
biases = preload_variable("biases", seed_biases)
print("Seeding ...")
else:
weights = create_variable('weights', [kernel_width, channels, units])
biases = create_bias_variable('biases', [units])
print("Not seeding ...")
# define convolutional steps
conv = tf.add(tf.nn.conv1d(input_batch, weights, stride=1, padding='SAME'), biases)
conv = tf.nn.relu(conv)
# make summary histograms of weights
tf.compat.v1.summary.histogram(name + '_conv_weights', weights)
tf.compat.v1.summary.histogram(name + '_conv_biases', biases)
# activation summary
_activation_summary(conv)
# Max Pool
conv = tf.compat.v1.layers.max_pooling1d(conv, pool_width, strides=pool_width, padding='same', name=str(name+'max_pool'))
# Dropout
out = tf.compat.v1.nn.dropout(conv, rate = 1 - keep_prob)
# # batch norm
if to_batch_norm == True:
out = tf.layers.batch_normalization(out)
return out
# INFERENCE ===================================================================
def inference(seqs,
conv_layers,
hidden_units_scheme,
kernel_width_scheme,
max_pool_scheme,
dilation_scheme,
dilation_units,
dilation_width,
dilation_residual,
dilation_residual_dense,
dilation_batch_norm,
num_classes,
batch_size,
keep_prob_inner,
keep_prob_outer,
seed_weights,
seed_scheme,
seed_weights_list
):
"""INFERENCE
Args:
Returns:
softmax_linear: Output tensor with the computed logits.
"""
print('seqs shape')
print(seqs.get_shape().as_list())
current_layer = tf.cast(seqs, tf.float32)
# Convolutional Stack with Max Pooling =====================================
# run an inital dilated layer with dilation 1 to map to the dilational unit output
with tf.name_scope('Convolutional_stack'):
for i in range(conv_layers):
j = i + 1
k = i * 2
if seed_weights and seed_scheme[i] == 1:
weights_load_string = 'arr_' + str(k)
biases_load_string = 'arr_' + str(k+1)
print('Pre-seeding Layer: ' + str(j))
current_layer = convolutional_layer(
'conv_layer{}'.format(j),
current_layer,
hidden_units_scheme[i],
kernel_width_scheme[i],
max_pool_scheme[i],
keep_prob_inner,
True,
seed_weights_list[weights_load_string],
seed_weights_list[biases_load_string],
to_batch_norm=False)
else:
current_layer = convolutional_layer(
'conv_layer{}'.format(j),
current_layer,
hidden_units_scheme[i],
kernel_width_scheme[i],
max_pool_scheme[i],
keep_prob_inner,
False,
"dummy",
"dummy",
to_batch_norm=False)
print('Conv %s shape' % j)
print(current_layer.get_shape().as_list())
# Dilational Layers stack ==================================================
# run an inital dilated layer with dilation 1 to map to the dilational unit output
with tf.name_scope('dilated_stack'):
current_layer = dilated_layer(
'dilated_layer1',
current_layer,
1,
dilation_width,
dilation_units,
residual = dilation_residual,
dense_residual = dilation_residual_dense,
to_batch_norm = dilation_batch_norm)
print('Dilated shape')
print(current_layer.get_shape().as_list())
for i, dilation in enumerate(dilation_scheme):
i = i+1 # skipping 0 count as this is pre-established
current_layer = dilated_layer(
'dilated_layer{}'.format(i),
current_layer,
dilation,
dilation_width,
dilation_units,
residual = dilation_residual,
dense_residual = dilation_residual_dense,
to_batch_norm=dilation_batch_norm)
print('Dilated shape')
print(current_layer.get_shape().as_list())
# reshape for FC layer
with tf.name_scope('reshape_layer'):
fully_connected_width = current_layer.get_shape().as_list()[1] * dilation_units
current_layer = tf.reshape(current_layer, [batch_size, fully_connected_width])
print('fully connection reshaped')
print(current_layer.get_shape().as_list())
# Final full connection(s) into logits
with tf.name_scope('final_dense'):
weights = create_variable('weights', [fully_connected_width, num_classes])
biases = create_bias_variable('biases', [num_classes])
regression_score = tf.add(tf.matmul(current_layer, weights), biases)
print('Regression score shape')
print(regression_score.get_shape().as_list())
_activation_summary(regression_score)
tf.compat.v1.summary.histogram('final_dense_weights', weights)
return regression_score
def loss(regression_score, labels, l2_regularization_strength, batch_size):
"""Calculates the loss from the logits and the labels.
Args:
regression_score
labels: Labels tensor, int32 - [batch_size, NUM_CLASSES].
Returns:
loss: Loss tensor of type float.
"""
with tf.name_scope('Loss'):
# labels = tf.to_float(labels) # old version
labels = tf.cast(labels, dtype="float32")
# MSQE
mean_squared_error = tf.compat.v1.losses.mean_squared_error(labels, regression_score, reduction=tf.compat.v1.losses.Reduction.SUM)
# mean over batch size
mean_squared_error = mean_squared_error/batch_size
# add summarizers if training case (loss for test is reported per epoch)
tf.compat.v1.summary.scalar('mean_squared_error', mean_squared_error)
# add regularizer:
if l2_regularization_strength == 0:
return mean_squared_error
else:
# L2 regularization for all trainable parameters
l2_loss = tf.add_n([tf.nn.l2_loss(v)
for v in tf.compat.v1.trainable_variables()
if not('bias' in v.name)])
# Add the regularization term to the loss
total_loss = (mean_squared_error + l2_regularization_strength * l2_loss)
# add summarizers
tf.compat.v1.summary.scalar('l2_loss', l2_loss)
tf.compat.v1.summary.scalar('total_loss', total_loss)
return total_loss
def loss_test(regression_score, labels, batch_size):
"""Calculates the loss from the logits and the labels for training case without summaries.
Args:
regression_score
labels: Labels tensor, int32 - [batch_size, NUM_CLASSES].
Returns:
loss: Loss tensor of type float.
"""
with tf.name_scope('Test_Loss'):
# labels = tf.to_float(labels)
labels = tf.cast(labels, dtype="float32")
test_mean_squared_error = tf.compat.v1.losses.mean_squared_error(labels, regression_score, reduction=tf.compat.v1.losses.Reduction.SUM)
test_mean_squared_error = test_mean_squared_error/batch_size
return test_mean_squared_error
def training(loss, learning_rate, beta_1, beta_2, epsilon, global_step):
"""Sets up the training Operations.
Creates a summarizer to track the loss over time in TensorBoard.
Creates an optimizer and applies the gradients to all trainable variables.
The Op returned by this function is what must be passed to the
`sess.run()` call to cause the model to train.
Args:
loss: Loss tensor, from loss().
learning_rate: The learning rate to use for gradient descent.
Returns:
train_op: The Op for training.
"""
# with Learning Rate decay
# learning_rate = tf.train.exponential_decay(learning_rate, global_step, learning_rate_decay_steps, 0.96)
optimizer = tf.compat.v1.train.AdamOptimizer(
learning_rate = learning_rate,
beta1 = beta_1,
beta2 = beta_2,
epsilon = epsilon)
trainables = tf.compat.v1.trainable_variables()
train_op = optimizer.minimize(loss, var_list=trainables, global_step=global_step)
return train_op
# def evaluation(logits, labels):
# """Evaluate the quality of the logits at predicting the label.
#
# Args:
# logits: Logits tensor, float - [batch_size, NUM_CLASSES].
# labels: Labels tensor, int32 - [batch_size], with values in the
# range [0, NUM_CLASSES).
#
# Returns:
# A scalar int32 tensor with the number of examples (out of batch_size)
# that were predicted correctly.
# """
# # For a classifier model, we can use the in_top_k Op.
# # It returns a bool tensor with shape [batch_size] that is true for
# # the examples where the label is in the top k (here k=1)
# # of all logits for that example.
# # correct = tf.nn.in_top_k(logits, tf.argmax(labels, 1), 1)
# # return tf.reduce_sum(tf.cast(correct, tf.int32))
# correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(labels,1))
# mean_correct = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#
# return mean_correct