【问题标题】:AttributeError: Layer has no inbound nodes - occurred in TF >= 2.4 but works in TF < 2.4?AttributeError:图层没有入站节点 - 发生在 TF >= 2.4 但在 TF < 2.4 中有效?
【发布时间】:2021-03-30 14:01:26
【问题描述】:

这里有一个简化的代码示例:

class MyCustomLayer(keras.layers.Layer):
    def __init__(self, num_filters=64, kernel_size=3):
        keras.layers.Layer.__init__(self)
        self.conv_1 = keras.layers.Conv2D(filters=num_filters,
                                          kernel_size=kernel_size)
        
    def call(self, inputs):
        return self.conv_1(inputs)

x = keras.Input(shape=(None, None, 3))
my_custom_layer = MyCustomLayer()
y = my_custom_layer(x)

以下行有效:

my_custom_layer.output
# Out: <KerasTensor: shape=(None, None, None, 64) dtype=float32 (created by layer 'my_custom_layer')>

但是,这在 Tensorflow 2.4.1 中中断:

my_custom_layer.conv_1.output

出现以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-74d9fc3b4fbb> in <module>
----> 1 my_custom_layer.conv_1.output

~/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in output(self)
   2152     """
   2153     if not self._inbound_nodes:
-> 2154       raise AttributeError('Layer ' + self.name + ' has no inbound nodes.')
   2155     return self._get_node_attribute_at_index(0, 'output_tensors', 'output')
   2156 

AttributeError: Layer conv2d has no inbound nodes.

如果我在 Tensorflow 2.3.1 中运行相同的代码,它可以正常工作。我查看了更新日志,但没有发现什么可以破坏它。可能是什么原因?


GitHub 问题 #48196:https://github.com/tensorflow/tensorflow/issues/48196

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0


    【解决方案1】:

    我也遇到过类似的问题。现在,我还在 CPU 和 GPU 模式下使用 tf 2.02.12.22.3 测试了您的代码,并且它正在工作。但它在tf 2.4tf-nightly 2.6.0-dev20210330中断。这似乎是一个错误。使用tf &lt; 2.4my_custom_layer.conv_1.inbound_nodes 返回

    [<tensorflow.python.keras.engine.node.Node at 0x7f751f301bd0>]
    

    tf &gt; = 2.4 中的空 [] 不同。

    【讨论】:

    • 感谢您的检查!那么,这对我来说不是问题......很高兴知道。
    • 我建议在 Github 上提出问题。
    • @desa - 将这篇文章链接到 github 问题(并在这篇文章中链接问题)会很有帮助,以便其他用户可以跟踪讨论
    【解决方案2】:

    这里有一个临时的修复让它工作(仅在 TF2.4 上测试过),你可以使用以下命令禁用 KerasTensor:

    from tensorflow.python.keras.engine import keras_tensor
    keras_tensor.disable_keras_tensors()
    
    # .... Rest of your code
    

    使用您的示例,您可以访问内部组件:

    my_custom_layer.conv_1.output 
    # <tf.Tensor 'my_custom_layer_2/conv2d_2/BiasAdd:0' shape=(None, None, None, 64) dtype=float32>
    

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多