Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Torch - repeat tensor like numpy repeat

Tags:

torch

lua

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.

like image 459
mattdns Avatar asked Feb 05 '16 15:02

mattdns


People also ask

Is PyTorch Tensor faster than Numpy?

Tensors in CPU and GPU Below is the quick comparison between GPU and CPU. It is nearly 15 times faster than Numpy for simple matrix multiplication!

What does torch Tensor () mean?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

How do you transpose Tensor PyTorch?

To transpose a tensor, we need two dimensions to be transposed. If a tensor is 0-D or 1-D tensor, the transpose of the tensor is same as is. For a 2-D tensor, the transpose is computed using the two dimensions 0 and 1 as transpose(input, 0, 1).


1 Answers

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])
like image 152
GreMal Avatar answered Sep 28 '22 03:09

GreMal