TypeError:Tensor是不可散列的,相反,使用tensor.ref()作为键,关于Keras Surgeon

7qhs6swi  于 2023-06-23  发布在  其他
关注(0)|答案(5)|浏览(103)

我正在使用Kerassurgery模块进行修剪。我在谷歌colab中使用VGG-16时遇到了这个错误。它适用于其他型号。有人可以帮助我修复这个问题。

---> 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.**
qltillow

qltillow1#

我在尝试Deep learning example with GradientExplainer时解决了类似的问题。这是由于版本不兼容造成的。
添加下面的代码可能会有帮助:

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

tf版本为2.3.1
kerase版本为2.4.0
形状版本为0.36

falq053o

falq053o2#

请尝试以下代码:

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

1.compat 允许您编写在TensorFlow 1中都能使用的代码。x和2,并应解决基于 * 版本导入 * 的任何错误。
2.eager_execution 是一个接口,只要从Python调用它,它就允许操作。打开它可以让Tensorflow更直观。
3.那么为什么要禁用 eager_execution 呢?
->eager_executiongraph_execution 慢。它逐行运行操作,这使得潜在的加速机会变得无用。
4.运行tf.executing_early(),检查 eager_execution 是开还是关。
希望这有助于减少你的错误。

nbysray5

nbysray53#

我解决了这个错误。这是由于版本变更所致。使用Kerassurgery代替tfkerassurgery。
使用以下版本

tf 1.x , keras > 2.2 , kerassurgeon
bqf10yzr

bqf10yzr4#

我遇到了类似的问题,我已经解决了它使用下一行:

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

y3bcpkx15#

当我尝试运行yolo3模型时遇到了类似的问题。我已经解决了:
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
似乎是tensorflow从V1.x更新到V2.x时版本不兼容造成的
希望能帮上忙。

相关问题