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]]])
打赏作者

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

CAPTCHA