python - tf.constant([1,2,3]) 和 tf.constant([[1,2,3]]) 有什么区别

标签 python tensorflow

>>> 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/

相关文章:

python - inception v3 网络的标签平滑

python - 拟合神经网络时出现 UnboundLocalError - TensorFlow 错误?

python - 将预测的张量保存到 TensorFlow 中的图像 - 图形最终确定

python - 在带有 cPickle 的 python 3.7 上使用 python 2.7 代码时出现 UnicodeDecodeError

python - 如何突然终止正在进行的处理

python - C中 "for line in sys.stdin: "的替换是什么?

python - 将固定宽度、非定界浮点字符串转换为逗号分隔值

android - 如何保存图像分类模型并将其用于 android

python - TensorFlow:为下一批记住 LSTM 状态(有状态 LSTM)

Python ctypes : Passed arguments are null when using restype