繁体   English   中英

TensorFlow:张量形状与 Numpy 数组形状

[英]TensorFlow: Tensor Shape vs. Numpy Array Shape

在 tf.shape() 中,张量的形状与 numpy 数组的形状有何不同?

示例 1:

t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]) 
tf.shape(t) 

 <tf.Tensor: shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>

注意张量的形状是(3,)而数组的形状是[2, 2, 3] :2 行,2 列,每行 3 层深。 (3,)从哪里来? 为什么第二个维度在 3 之后是None

示例 2:

c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
tf.shape(c)

  <tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 3], dtype=int32)>

请注意,张量的形状是(2,)匹配数组[2, 3][2, 3] 为什么它在这里匹配,而在示例 1 中不匹配?

谢谢。

张量的形状由 numpy 数组形状的长度定义。 例如 -

t = tf.constant([[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]],[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]) 
tf.shape(t)

会输出

<tf.Tensor: shape=(4,), dtype=int32, numpy=array([2, 2, 2, 3], dtype=int32)>

要获得张量的大小,请使用

t.shape

反而。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM