[Thx Solved] Use of 1 and -1 in torch.repeat and torch.expand

tl;dr: why torch.repeat() use 1 while torch.expand() use -1 for retaining dimension size of a input tensor??

a = torch.Tensor(range(10)).view(5,2)
print(a.expand(64,-1,-1).shape) # 64, 5, 2
print(a.repeat(64,1,1).shape) # 64, 5, 2

Is it intended to that functions take -1 for expand() but 1 for repeat()? it is quite confusing sometimes. Considering use of -1 in .view(), shouldn’t 1 be replaced with -1 in repeat() either?

actually we can place 1 at the first dim of the torch.repeat() but for torch.expand() it is not allowed to place -1 at the 0th dimension. would that be where the reason of this comes from?

Hi,

The two functions do very different things.

  • repeat will repeat the value on a dimension of size 1 to the size you want. For dimensions that you do not change, you can use -1 to keep the current size. If you ask for more dimensions than there are on the original tensor, it will create a new dimension of size 1 and expand it.
  • repeat will repeat a dimension of any size that many times. to keep a dimension with it’s current size, you only need to give 1 as you want to repeat it once. Here again if you ask for more dimensions than there are in the original Tensor, a new dimension of size 1 is created and repeated that number of times.

You should try and use these functions and ask for the same number of output dimensions as the input to have a clearer behavior.

2 Likes