【发布时间】:2017-04-13 01:43:55
【问题描述】:
我有 2 个形状张量 (100, 4) 和 (100, 2)。
我想在 TensorFlow 中执行连接操作,类似于在 NumPy 中的np.hstack,以便输出的形状为(100, 6)。有没有 TensorFlow 函数可以做到这一点?
【问题讨论】:
标签: tensorflow
我有 2 个形状张量 (100, 4) 和 (100, 2)。
我想在 TensorFlow 中执行连接操作,类似于在 NumPy 中的np.hstack,以便输出的形状为(100, 6)。有没有 TensorFlow 函数可以做到这一点?
【问题讨论】:
标签: tensorflow
您可以按如下方式使用tf.concat:
sess=tf.Session()
t1 = [[1, 2], [4, 5]]
t2 = [[7, 8, 9], [10, 11, 12]]
res=tf.concat(concat_dim=1,values=[t1, t2])
print(res.eval(session=sess))
打印出来
[[ 1 2 7 8 9]
[ 4 5 10 11 12]]
【讨论】:
tf.stack()。
axis=1,not concat_dim=1
我尝试了上面的代码,我得到了一些错误。以下代码在 tf 1.15 版本下运行良好:
x = tf.constant( [[1, 2,4],[7,8,12]])
y = tf.constant([[88],[99]])
res=tf.concat([x, y],1)
【讨论】:
我也有类似的问题。我尝试将两个图像张量与 Keras 连接起来。
Tensor type and shape
两者是相同的,但它表示 Layer concatenate_X(数字 X 的变化)是使用不是符号张量的输入调用的。接收类型:
X=concatenate([X1,X2],axis=-1)
【讨论】:
在 PyTorch 中有一种更简单的方法。假设 t1 是您的 100x4 张量,而 t2 是 100x2 张量。您可以执行以下操作:
result= torch.concat((t1,t2), axis=1)
结果是一个 100x6 的张量。
【讨论】: