目录
显示
unsqueeze/repeat
>>> x = torch.tensor([[1, 2], [3, 4]])
>>> x.shape
torch.Size([2, 2])
>>> y = x.repeat(2, 3) # repeat 2 times in dim-0; repeat 3 times in dim-1;
>>> y
tensor([[1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4],
[1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4]])
>>> x
tensor([[1, 2],
[3, 4]])
>>> x.unsqueeze(1) # insert
tensor([[[1, 2]],
[[3, 4]]])
>>> x.unsqueeze(1).shape # insert into the dim-1 one dimention
torch.Size([2, 1, 2])
>>> x.unsqueeze(1).repeat(1,2,1) # repeat 1 times in dim-0; repeat 2 times in dim-1;repeat 1 times in dim-2;
tensor([[[1, 2],
[1, 2]],
[[3, 4],
[3, 4]]])
打赏作者