Clarification : tensor.view(*size)

The documentation of torch.view has the input parameter as (*size).
https://pytorch.org/docs/stable/tensors.html#torch.Tensor.view

What exactly does * represent here?
Why does not just input of type <class ‘torch.Size’> work?

The asterisk (*) is used to unwrap values in Python and you could thus pass the desired shapes as separate values to view.

torch.Size works fine as an input also:

x = torch.randn(100)
y = torch.randn(10, 10)
x = x.view(y.size())
print(x.shape)
> torch.Size([10, 10])

x = x.view(torch.Size([100]))
print(x.shape)
> torch.Size([100])

x = x.view(1, 5, 20)
print(x.shape)
> torch.Size([1, 5, 20])
1 Like

Got it. Thanks a lot. :slight_smile: