Issue
I followed the tutorial A Primer on Deep Learning in Genomics - Public.ipynb in colab but got TypeError: Cannot convert a symbolic Keras input/output to a numpy array...
as I tried to execute the step 4.Interpret
at line sal = compute_salient_bases(model, input_features[sequence_index])
.
import tensorflow.keras.backend as K
def compute_salient_bases(model, x):
input_tensors = [model.input]
gradients = model.optimizer.get_gradients(model.output[0][1], model.input)
compute_gradients = K.function(inputs = input_tensors, outputs = gradients)
x_value = np.expand_dims(x, axis=0)
gradients = compute_gradients([x_value])[0][0]
sal = np.clip(np.sum(np.multiply(gradients,x), axis=1),a_min=0, a_max=None)
return sal
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])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-b6400cc2276d> in <module>()
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)
14 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/keras_tensor.py in __array__(self)
272 def __array__(self):
273 raise TypeError(
--> 274 'Cannot convert a symbolic Keras input/output to a numpy array. '
275 'This error may indicate that you\'re trying to pass a symbolic value '
276 'to a NumPy call, which is not supported. Or, '
TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.
The problem is at model.optimizer.get_gradients(model.output[0][1], model.input)
. I think it is the right usage according to https://www.tensorflow.org/api_docs/python/tf/gradients
get_gradients(
loss, params
)
I am quite confused about the error. Or is there an alternative approach to compute_salient_bases
?
Solution
Downgrade TensorFlow version, restart runtime and run the notebook again.
!pip install tensorflow==1.13.2
import tensorflow as tf
print(tf.__version__)
Answered By - fuad021
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.