deep-learning-genomics-primer
deep-learning-genomics-primer copied to clipboard
Last Code Block Doesn't Work
Hello,
Thank you for sharing the primer.
Everything works except the last code block.
sequence_index = 1999 # You can change this to compute the gradient for a different example. But if so, change the coloring below as well.
sal = compute_salient_bases(model, input_features[sequence_index])
plt.figure(figsize=[16,5])
barlist = plt.bar(np.arange(len(sal)), sal)
[barlist[i].set_color('C1') for i in range(5,17)] # Change the coloring here if you change the sequence index.
plt.xlabel('Bases')
plt.ylabel('Magnitude of saliency values')
plt.xticks(np.arange(len(sal)), list(sequences[sequence_index]));
plt.title('Saliency map for bases in one of the positive sequences'
' (green indicates the actual bases in motif)');
Running this on Google Colab returns the error below:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
[<ipython-input-10-b6400cc2276d>](https://localhost:8080/#) in <cell line: 2>()
1 sequence_index = 1999 # You can change this to compute the gradient for a different example. But if so, change the coloring below as well.
----> 2 sal = compute_salient_bases(model, input_features[sequence_index])
3
4 plt.figure(figsize=[16,5])
5 barlist = plt.bar(np.arange(len(sal)), sal)
[<ipython-input-9-9666bf2dbd7f>](https://localhost:8080/#) in compute_salient_bases(model, x)
3 def compute_salient_bases(model, x):
4 input_tensors = [model.input]
----> 5 gradients = model.optimizer.get_gradients(model.output[0][1], model.input)
6 compute_gradients = K.function(inputs = input_tensors, outputs = gradients)
7
AttributeError: 'Adam' object has no attribute 'get_gradients'
Looks like it's an issue of TensorFlow 1.x vs 2.x
Changing the last two code blocks to the below works:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
def compute_salient_bases(model, x):
@tf.function
def compute_gradients(x):
with tf.GradientTape() as tape:
tape.watch(x)
logits = model(x)
prob = logits[:, 1] # Assuming binary classification, change the index if needed
gradients = tape.gradient(prob, x)
return gradients
x_value = np.expand_dims(x, axis=0)
gradients = compute_gradients(x_value)
gradients = tf.where(gradients == None, tf.zeros_like(gradients), gradients)
sal = tf.reduce_sum(gradients * x, axis=2)
sal = tf.clip_by_value(sal, clip_value_min=0, clip_value_max=tf.reduce_max(sal))
return sal.numpy()
sequence_index = 1999 # You can change this to compute the gradient for a different example. But if so, change the coloring below as well.
sal = compute_salient_bases(model, input_features[sequence_index])
plt.figure(figsize=[16, 5])
barlist = plt.bar(np.arange(len(sal[0])), sal[0])
[barlist[i].set_color('C1') for i in range(5, 17)] # Change the coloring here if you change the sequence index.
plt.xlabel('Bases')
plt.ylabel('Magnitude of saliency values')
plt.xticks(np.arange(len(sal[0])), list(sequences[sequence_index]))
plt.title('Saliency map for bases in one of the positive sequences'
' (green indicates the actual bases in the motif)')
plt.show()