官网链接
https://tensorflow.google.cn/api_docs/python/tf/shape
tf.shape(
input,
name=None,
out_type=tf.int32
)
返回一个一维整数Tensor,用以表示输入input的shape
例1
import tensorflow as tf
a=tf.constant([[1,2],
[3,4],
[5,6]])
with tf.Session() as sess:
print(a.shape)
print(tf.shape(a))
print(sess.run(tf.shape(a)))
(3, 2)
Tensor("Shape:0", shape=(2,), dtype=int32)
[3 2]
例2
import tensorflow as tf
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
with tf.Session() as sess:
print(tf.shape(t))
print(sess.run(tf.shape(t)))
Tensor("Shape:0", shape=(3,), dtype=int32)
[2 2 3]