>>> tf.constant([1,2,3])
<tf.Tensor 'Const:0' shape=(3,) dtype=int32>
>>> tf.constant([[1,2,3]])
<tf.Tensor 'Const_1:0' shape=(1, 3) dtype=int32>
是tf.constant([1,2,3])
创建标量和 tf.constant([[1,2,3]])
创建数组?
最佳答案
不完全是。 tf.constant([1, 2, 3])
创建一个 1 阶常量张量(向量)。因此形状是(3,)
。
>>> sess = tf.InteractiveSession()
>>> tf.constant([1, 2, 3]).eval()
array([1, 2, 3], dtype=int32)
而tf.constant([[1, 2, 3]])
创建了一个2阶常量张量(矩阵),有1行3列..所以它的形状是(1, 3)
。
>>> tf.constant([[1, 2, 3]]).eval()
array([[1, 2, 3]], dtype=int32)
如果您确实想要一个标量(等级 0),您不会用序列来构造它,而只是用标量值来构造它。
>>> tf.constant(3)
<tf.Tensor 'Const_5:0' shape=() dtype=int32>
注意这里的空形状,清楚地表明它的等级为 0。
参见Tensor/Rank在文档中。
关于python - tf.constant([1,2,3]) 和 tf.constant([[1,2,3]]) 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808208/