Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key. in Keras Surgeon

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.**
like image 547
Nikhil Munna Avatar asked Apr 06 '20 09:04

Nikhil Munna


2 Answers

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

like image 131
mingyang ou Avatar answered Dec 20 '22 11:12

mingyang ou


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.

like image 23
Parth Rangarajan Avatar answered Dec 20 '22 13:12

Parth Rangarajan