我需要一种方法来获取 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.
hoặcprint(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 tài liệu ,如果一个层只有一个节点,你可以通过以下方式得到它的输入张量、输出张量、输入形状和输出形状:
- 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是吗?
MỘT nút 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/
Tôi là một lập trình viên xuất sắc, rất giỏi!