我需要一种方法来获取 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)
Run Code Online (Sandbox Code Playgroud)
该print(dense.output_shape)语句将产生错误消息:
AttributeError: The layer has never been called and thus has no defined output shape.
Run Code Online (Sandbox Code Playgroud)
或print(dense.output)将产生:
AttributeError('Layer ' + self.name + ' has no inbound nodes.')
AttributeError: Layer dense_1 has no …Run Code Online (Sandbox Code Playgroud)