如何在 TensorFlow 中水平连接两个张量? [英] How to concatenate two tensors horizontally in TensorFlow?

查看:34
本文介绍了如何在 TensorFlow 中水平连接两个张量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个形状为 (100, 4)(100, 2) 的张量.我想在 TensorFlow 中执行连接操作,类似于 np.hstack,在 NumPy 中,以便输出的形状为 (100, 6).是否有 TensorFlow 函数可以做到这一点?

I have 2 tensors of shape (100, 4) and (100, 2). I would like to perform a concatenation operation, in TensorFlow, similar to np.hstack, in NumPy, so that the output would be of shape (100, 6). Is there a TensorFlow function to do that?

推荐答案

您可以使用 tf.concat 用于此目的,如下所示:

You can use tf.concat for this purpose as follows:

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

这篇关于如何在 TensorFlow 中水平连接两个张量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆