tf.shape(tensor) 和 print(tensor) 中的形状有什么区别
What's the difference between the shapes in tf.shape(tensor) and print(tensor)
也就是说,我有一个张量matrix
:
matrix=tf.convert_to_tensor([[1,1,1,1],[0,0,0,0]],dtype=tf.float32)
我使用tf.shape(matrix)
得到矩阵的形状,结果是
<tf.Tensor 'Shape_2:0' shape=(2,) dtype=int32>
但是使用 print(matrix)
,我得到的结果是:
<tf.Tensor 'Const_257:0' shape=(2, 4) dtype=float32>.
为什么它们不一样。
我是 tensorflow 的新手,有人可以解释一下吗?
非常感谢。
方法 tf.shape() returns 包含输入张量形状的新张量。返回的张量与输入的张量完全不同。
>>> import tensorflow as tf
>>> matrix = tf.convert_to_tensor([[1,1,1,1],[0,0,0,0]],dtype=tf.float32)
>>> matrix
<tf.Tensor 'Const_5:0' shape=(2, 4) dtype=float32>
>>> matrix.get_shape()
TensorShape([Dimension(2), Dimension(4)])
>>> shape_tensor = tf.shape(matrix)
>>> shape_tensor
<tf.Tensor 'Shape_3:0' shape=(2,) dtype=int32>
也就是说,我有一个张量matrix
:
matrix=tf.convert_to_tensor([[1,1,1,1],[0,0,0,0]],dtype=tf.float32)
我使用tf.shape(matrix)
得到矩阵的形状,结果是
<tf.Tensor 'Shape_2:0' shape=(2,) dtype=int32>
但是使用 print(matrix)
,我得到的结果是:
<tf.Tensor 'Const_257:0' shape=(2, 4) dtype=float32>.
为什么它们不一样。 我是 tensorflow 的新手,有人可以解释一下吗?
非常感谢。
方法 tf.shape() returns 包含输入张量形状的新张量。返回的张量与输入的张量完全不同。
>>> import tensorflow as tf
>>> matrix = tf.convert_to_tensor([[1,1,1,1],[0,0,0,0]],dtype=tf.float32)
>>> matrix
<tf.Tensor 'Const_5:0' shape=(2, 4) dtype=float32>
>>> matrix.get_shape()
TensorShape([Dimension(2), Dimension(4)])
>>> shape_tensor = tf.shape(matrix)
>>> shape_tensor
<tf.Tensor 'Shape_3:0' shape=(2,) dtype=int32>