如何沿给定轴连接张量?
How do I concatenate tensors along a given axis?
import torch
a = torch.Tensor(2,2,2)
b = myfunction(a)
print(a)
>>> [[[1,2],
[5,6]],
[[7,8],
[9,10]]]
print(b)
>>> [[1,2,7,8],
[5,6,9,10]]
如何编写函数代码以从 a 获取 b?
是否有一些pytorch函数可以这样转换a?
您可以通过使用 transpose
交换前两个轴(参见例如 np.swapaxes
)和 reshape
来获得您想要的形状来实现此目的:
In [12]: a
Out[12]:
tensor([[[ 1., 2.],
[ 5., 6.]],
[[ 7., 8.],
[ 9., 10.]]])
In [13]: a.transpose(0, 1).reshape(2, 4)
Out[13]:
tensor([[ 1., 2., 7., 8.],
[ 5., 6., 9., 10.]])
import torch
a = torch.Tensor(2,2,2)
b = myfunction(a)
print(a)
>>> [[[1,2],
[5,6]],
[[7,8],
[9,10]]]
print(b)
>>> [[1,2,7,8],
[5,6,9,10]]
如何编写函数代码以从 a 获取 b?
是否有一些pytorch函数可以这样转换a?
您可以通过使用 transpose
交换前两个轴(参见例如 np.swapaxes
)和 reshape
来获得您想要的形状来实现此目的:
In [12]: a
Out[12]:
tensor([[[ 1., 2.],
[ 5., 6.]],
[[ 7., 8.],
[ 9., 10.]]])
In [13]: a.transpose(0, 1).reshape(2, 4)
Out[13]:
tensor([[ 1., 2., 7., 8.],
[ 5., 6., 9., 10.]])