TypeError:Tensor无法散列。而是使用tensor.ref()作为键。在Keras外科医生

时间:2020-04-06 09:34:40

标签: keras tf.keras vgg-net pruning

我正在使用Kerassurgeon模块进行修剪。当我在Google 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.**

4 个答案:

答案 0 :(得分:3)

尝试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

答案 1 :(得分:1)

我解决了这个错误。这是由于版本更改所致。请使用Kerassurgeon代替tfkerassurgeon。

使用以下版本

country_iso

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

请尝试以下代码:

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_eagerly() 以检查 eager_execution 是打开还是关闭。

希望这有助于减少您的错误。