Search code examples
pytorchstackconcatenationdimension

Pytorch dimension change


Is there any methods to change [1,512,1,1] to [1,512,2,2] tensor. I know it is not possible just by changing the dimensions. Are there any ways using concat or stack with PyTorch (torch.stack, torch.cat)

I make tensor with following code

a = torch.rand([1,512,1,1])

How can I change this to tensor with dimension [1,512,2,2]


Solution

  • That would be with torch.repeat, this will copy the data:

    >>> a = a.repeat(1, 1, 2, 2)
    

    If you do not wish to copy the data, then use torch.expand:

    >>> a = a.expand(-1, -1, 2, 2)