第一个中括号表示第一个维度,第二个中括号表示第二个维度,以此类推,对某一维度进行合并则该维个数增加
import torch
a = torch.randn(2, 3)
a = torch.unsqueeze(a, 0)
b = torch.cat((a, a), 0)
c = torch.cat((a, a), 1)
d = torch.cat((a, a), 2)
print(a)
print(b)
print(c)
print(d)
输出为
tensor([[[ 0.4198, -0.6766, -1.1429],
[ 1.5233, -0.7087, -1.0912]]])
tensor([[[ 0.4198, -0.6766, -1.1429],
[ 1.5233, -0.7087, -1.0912]],
[[ 0.4198, -0.6766, -1.1429],
[ 1.5233, -0.7087, -1.0912]]])
tensor([[[ 0.4198, -0.6766, -1.1429],
[ 1.5233, -0.7087, -1.0912],
[ 0.4198, -0.6766, -1.1429],
[ 1.5233, -0.7087, -1.0912]]])
tensor([[[ 0.4198, -0.6766, -1.1429, 0.4198, -0.6766, -1.1429],
[ 1.5233, -0.7087, -1.0912, 1.5233, -0.7087, -1.0912]]])
import torch
a = torch.randn(2, 3)
a = torch.unsqueeze(a, 0)
c = torch.vstack((a, a))
b = torch.hstack((a, a))
d = torch.dstack((a, a))
print(a)
print(b)
print(c)
print(d)
输出为:
tensor([[[ 0.1947, 1.2356, 2.2314],
[-0.7753, 1.6233, 1.1481]]])
tensor([[[ 0.1947, 1.2356, 2.2314],
[-0.7753, 1.6233, 1.1481],
[ 0.1947, 1.2356, 2.2314],
[-0.7753, 1.6233, 1.1481]]])
tensor([[[ 0.1947, 1.2356, 2.2314],
[-0.7753, 1.6233, 1.1481]],
[[ 0.1947, 1.2356, 2.2314],
[-0.7753, 1.6233, 1.1481]]])
tensor([[[ 0.1947, 1.2356, 2.2314, 0.1947, 1.2356, 2.2314],
[-0.7753, 1.6233, 1.1481, -0.7753, 1.6233, 1.1481]]])
|