重复张量
torch.Tensor.repeat(*sizes)
在张量的某个维度上复制
张量维度 等于 repeat参数个数
#创建一个二维张量
x = torch.randn((2,3)) #torch.Size([2, 3])
#在x的两个维度上分别重复4次,2次,即得目标维度:4x2=8, 2x3=6,即(8,6)
x= x.repeat(4, 2) #torch.Size([8, 6])
张量维度 不等于 repeat参数个数
例如:
#创建一个二维张量
t1 = torch.randn((1,2)) #torch.Size([1, 2])
#维度为2,小于repeat参数数量3,则结果大小:(3,1x5, 2x6)
t2 = t1.repeat(3,5,6) #torch.Size([3, 5, 12])
再如:
#创建一个三维张量
t1 = torch.randn((2,2,3)) #torch.Size([2, 2, 3])
#目标大小(3, 2x4, 2x5, 3x6)
t2 = t1.repeat(3,4,5,6)#torch.Size([3, 8, 10, 18])
repeat类似于平铺操作 tile,repeat参数指定了在每个维度(dimention)上平铺(tile)的次数,如果张量维度 < repeat参数数量,则添加新的维度。如上例所示。
张量连接
torch.cat(tensors, dim=0, out=None) → Tensor
在指定的维度(dim)上,将张量连接