Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key. #6

Open
NikhilMunna opened this issue Apr 6, 2020 · 4 comments

Comments

@NikhilMunna
Copy link

' ' '
15
16
---> 17 model_new = surgeon.operate()
18 return model_new

/usr/local/lib/python3.6/dist-packages/tfkerassurgeon/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/tfkerassurgeon/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/tfkerassurgeon/surgeon.py in (.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/tfkerassurgeon/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.
' ' '

@anklebreaker
Copy link

Have the same issue too. TF 2.0.1 and Keras 2.3.1.

Was able to get around it by adding .experimental_ref() to several tensors (e.g. if node_output.experimental_ref() in self._replace_tensors.keys():). However, I ultimately got stuck with other problems down the line

@medicresource
Copy link

medicresource commented Jun 4, 2020

versions
tensorflow: 2.2.0
keras: 2.3.0-tf

I also had this error:
Tensor is unhashable. Instead, use tensor.ref() as the key.

I got around it by first disabling eager execution
tf.compat.v1.disable_eager_execution()

Then I ran into an error saying
TypeError: list indices must be integers or slices, not ListWrapper

In my case this was fixed by editing surgeon.py
under the BatchNormalization class
index[layers.axis] to index[3]
and
new_input_shape[new_layer.axis]' to 'new_input_shape[3]

@ferova
Copy link

ferova commented Aug 20, 2020

versions
tensorflow: 2.2.0
keras: 2.3.0-tf

I also had this error:
Tensor is unhashable. Instead, use tensor.ref() as the key.

I got around it by first disabling eager execution
tf.compat.v1.disable_eager_execution()

Then I ran into an error saying
TypeError: list indices must be integers or slices, not ListWrapper

In my case this was fixed by editing surgeon.py
under the BatchNormalization class
index[layers.axis] to index[3]
and
new_input_shape[new_layer.axis]' to 'new_input_shape[3]

Did you get it to work without problem with the "Add layer" as in #7 ?

@devmike01
Copy link

devmike01 commented Apr 24, 2023

I'm having this issue as well and tf.compat.v1.disable_eager_execution() is no longer working.
tensorflow: 2.12.0
numpy: 1.23.5

Update: I was able to solve it by adding .ref() to my tensor(e.g: loss.ref())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants