gpt4 book ai didi

python - AttributeError : Layer has no inbound nodes, 或 AttributeError: 该层从未被调用

转载 作者:行者123 更新时间:2023-11-28 17:01:26 33 4
gpt4 key购买 nike

我需要一种方法来获取 TensorFlow 中任何类型层(即 Dense、Conv2D 等)的输出张量的形状。根据文档,有 output_shape 属性可以解决这个问题。但是,每次我访问它时,我都会得到 AttributedError

这是显示问题的代码示例:

import numpy as np
import tensorflow as tf


x = np.arange(0, 8, dtype=np.float32).reshape((1, 8))
x = tf.constant(value=x, dtype=tf.float32, verify_shape=True)

dense = tf.layers.Dense(units=2)

out = dense(x)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(fetches=out)
print(res)
print(dense.output_shape)

print(dense.output_shape) 语句会产生错误信息:

AttributeError: The layer has never been called and thus has no defined output shape.

print(dense.output) 将产生:

AttributeError('Layer ' + self.name + ' has no inbound nodes.')
AttributeError: Layer dense_1 has no inbound nodes.

有什么办法可以解决这个错误吗?

附注:我知道在上面的示例中,我可以通过 out.get_shape() 获取输出张量的形状。但是我想知道为什么 output_shape 属性不起作用,我该如何解决?

最佳答案

长话短说

我该如何解决?定义一个输入层:

x = tf.keras.layers.Input(tensor=tf.ones(shape=(1, 8)))
dense = tf.layers.Dense(units=2)

out = dense(x)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(fetches=out)
print(dense.output_shape) # shape = (1, 2)

根据 Keras documentation ,如果一个层只有一个节点,你可以通过以下方式得到它的输入张量、输出张量、输入形状和输出形状:

  • layer.input
  • 图层输出
  • layer.input_shape
  • layer.output_shape

但是在上面的例子中,当我们调用layer.output_shape或者其他属性时,它会抛出异常,看起来有点奇怪。

如果我们深入 source code ,由入站节点引起的错误。

if not self._inbound_nodes:
raise AttributeError('The layer has never been called '
'and thus has no defined output shape.')

这些是什么inbound nodes是吗?

A Node describes the connectivity between two layers. Each time a layer is connected to some new input,a node is added to layer._inbound_nodes.Each time the output of a layer is used by another layer,a node is added to layer._outbound_nodes.

正如您在上面看到的,当 self._inbounds_nodes 为 None 时,它​​会抛出异常。 这意味着当一个层没有连接到输入层时,或者更一般地说,前面的层都没有连接到输入层,self._inbounds_nodes 是空的,这导致了问题

请注意,在您的示例中,x 是张量而不是输入层。请参阅另一个示例以获得更多说明:

x = tf.keras.layers.Input(shape=(8,))
dense = tf.layers.Dense(units=2)

out = dense(x)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(fetches=out, feed_dict={x: np.ones(shape=(1, 8))})
print(res)
print(res.shape) # shape = (1,2)
print(dense.output_shape) # shape = (None,2)

非常好,因为输入层已定义。


请注意,在您的示例中,out 是一个张量。 tf.shape() 函数和.shape =(get_shape()) 的区别是:

tf.shape(x) returns a 1-D integer tensor representing the dynamicshape of x. A dynamic shape will be known only at graph execution time.

x.shape returns a Python tuple representing the staticshape of x. A static shape, known at graph definition time.

阅读更多关于张量形状的信息:https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

关于python - AttributeError : Layer has no inbound nodes, 或 AttributeError: 该层从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54194053/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com