首页 > 解决方案 > Tensorflow-tf.concat() 显示不同的结果

问题描述

with tf.Session() as sess:
t1 = [
    [[0],[1],[2]],
    [[3],[4],[5]],
]
print(sess.run(tf.shape(t1)))
print(sess.run(tf.concat(t1,1)))
print("**********")
t2 = np.arange(6).reshape([2,3,1])
print(sess.run(tf.shape(t2)))
print(sess.run(tf.concat(t2,1)))

然后它显示

[2 3 1]
[[0 3] [1 4] [2 5]]
[2 3 1]
[[[0] [1] [2]] [[3] [4] [5]]]

t1 和 t2 具有相同的形状和值,为什么结果不同?

标签: pythonpython-3.xtensorflow

解决方案


因为t1listt2不是。

tf.concat连接张量列表。因此t1被视为3x1要连接的 2 个大小张量的列表。另一方面,t2是一个 numpy 数组,它被转换为一个2x3x1 Tensor,并且没有其他东西可以连接这个张量。

我认为可能让您感到惊讶的是,t1根据上下文的不同,它的解释也不同。tf.shape期望 aTensor作为参数,而不是张量列表,因此当传递给此函数时,t1被解释为2x3x1张量。


推荐阅读