Python 如何连接两个张量

Python 如何连接两个张量,python,pytorch,concatenation,Python,Pytorch,Concatenation,我有两个维度为[3,1]的张量。我需要把它们作为[3,2]张量连接起来 t = torch.tensor([[3.],[1],[2]], requires_grad=True) x = torch.tensor([[1.],[4],[5]], requires_grad=True) 我尝试了torch.cat和torch.stack,但这两种方法对我都不起作用。使用cat时,需要指定张量连接的尺寸。默认情况下,这是0,但您希望使用1: import torch res = torch.cat(

我有两个维度为[3,1]的张量。我需要把它们作为[3,2]张量连接起来

t = torch.tensor([[3.],[1],[2]], requires_grad=True)
x = torch.tensor([[1.],[4],[5]], requires_grad=True)

我尝试了
torch.cat
torch.stack
,但这两种方法对我都不起作用。

使用
cat
时,需要指定张量连接的尺寸。默认情况下,这是
0
,但您希望使用
1

import torch
res = torch.cat([t,x], axis=1)

torch.cat([t,x],axis=1)
如果我的答案解决了你的问题,你介意点击“接受答案”按钮吗?