I am trying to repeat a tensor in torch in two ways. For example repeating the tensor {1,2,3,4}
3 times both ways to yield;
{1,2,3,4,1,2,3,4,1,2,3,4}
{1,1,1,2,2,2,3,3,3,4,4,4}
There is a built in torch:repeatTensor function which will generate the first of the two (like numpy.tile()
) but I can't find one for the latter (like numpy.repeat()
). I'm sure that I could call sort on the first to give the second but I think this might be computationally expensive for larger arrays?
Thanks.
Try torch.repeat_interleave() method: https://pytorch.org/docs/stable/torch.html#torch.repeat_interleave
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])