让我们来看看这个简单的组成操作:
data = np.random.rand(1,2,3)x = tf.placeholder(tf.float32,shape = [None,None,None],name =‘x_pl’)out = xprint(‘shape:’,tf.shape(out))sess = tf ….
这是因为 tf.shape() 是一个操作,你必须在一个会话中运行它。
tf.shape()
data = np.random.rand(1,2,3) x = tf.placeholder(tf.float32, shape=[None, None, None], name='x_pl') out = x print ('shape:', tf.shape(out)) z = tf.shape(out) sess = tf.Session() out_, z_ =sess.run([out,z], feed_dict={x: data}) print(f"shape of out: {z_}")
将返回
shape: Tensor("Shape:0", shape=(3,), dtype=int32) shape of out: [1 2 3]
即使你看一下文档中的例子( https://www.tensorflow.org/api_docs/python/tf/shape ):
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]) tf.shape(t)
如果你像那样运行它会返回类似的东西
<tf.Tensor 'Shape_4:0' shape=(3,) dtype=int32>
但如果你在一个会话中运行它,那么你将得到预期的结果
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]) print(sess.run(tf.shape(t))) [2 2 3]