11

I'm using Kerassurgeon module for pruning.I encountered this error while i'm working with VGG-16 in google colab.It works fine for other models.Can someone help me fix this.

---> 17   model_new = surgeon.operate()<br>
     18   return model_new

>>/usr/local/lib/python3.6/dist-packages/kerassurgeon/surgeon.py in operate(self)
    152             sub_output_nodes = utils.get_node_inbound_nodes(node)
    153             outputs, output_masks = self._rebuild_graph(self.model.inputs,
--> 154                                                         sub_output_nodes)
    155 
    156             # Perform surgery at this node

>>/usr/local/lib/python3.6/dist-packages/kerassurgeon/surgeon.py in _rebuild_graph(self, graph_inputs, output_nodes, graph_input_masks)
    264         # Call the recursive _rebuild_rec method to rebuild the submodel up to
    265         # each output layer
--> 266         outputs, output_masks = zip(*[_rebuild_rec(n) for n in output_nodes])
    267         return outputs, output_masks
    268 

>>/usr/local/lib/python3.6/dist-packages/kerassurgeon/surgeon.py in <listcomp>(.0)
    264         # Call the recursive _rebuild_rec method to rebuild the submodel up to
    265         # each output layer
--> 266         outputs, output_masks = zip(*[_rebuild_rec(n) for n in output_nodes])
    267         return outputs, output_masks
    268 

>>/usr/local/lib/python3.6/dist-packages/kerassurgeon/surgeon.py in _rebuild_rec(node)
    216             # Check for replaced tensors before any other checks:
    217             # these are created by the surgery methods.
--> 218             if node_output in self._replace_tensors.keys():
    219                 logging.debug('bottomed out at replaced output: {0}'.format(
    220                     node_output))

>>/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in __hash__(self)
    724     if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and
    725         (g is None or g.building_function)):
--> 726       raise TypeError("Tensor is unhashable. "
    727                       "Instead, use tensor.ref() as the key.")
    728     else:

**TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.**
2
  • It will be helpful to get at least a snippet of code that is responsible for the error you post.
    – adamconkey
    Apr 6, 2020 at 20:55
  • If you got to solve the issue please let us know, I am stuck at this!
    – HaneenSu
    Apr 26, 2020 at 8:39

5 Answers 5

14

I have solved a similar problem when I try the Deep learning example with GradientExplainer. This is caused by version incompatibility.
Adding the code below may be helpful:

import tensorflow.compat.v1.keras.backend as K
import tensorflow as tf
tf.compat.v1.disable_eager_execution()

tf version is 2.3.1
kerase version is 2.4.0
Shap version is 0.36

0
2

Please try the code below:

import tensorflow.compat.v1.keras.backend as K
import tensorflow as tf
tf.compat.v1.disable_eager_execution()

1.compat allows you to write code that works both in TensorFlow 1. x and 2 and should solve any errors based on the version import.

2.eager_execution is an interface which allows for operations as soon as it is called from Python. Turning it on allows for Tensorflow to be more intuitive.

3.But then why should eager_execution be disabled?
->eager_execution is slower than graph_execution. It runs operations line-by-line which renders the potential acceleration oppurtunities useless.

4.Run tf.executing_eagerly() to check is eager_execution is on or off.

Hopefully this helps in allaying your errors.

1

I solved this error. this is due to change of versions.Use Kerassurgeon instead of tfkerassurgeon.

Use the following versions

tf 1.x , keras > 2.2 , kerassurgeon 
1

I was facing similar problem, I have solved it using next lines:

import tensorflow.compat.v1.keras.backend as K
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
0

I encounter the similar problem when I try to run yolo3 model. I have solved with:
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()

seems it is caused by the version incompatibility when tensorflow updated fron V1.x to V2.x

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.