Clarification : tensor.view(*size)

The documentation of torch.view has the input parameter as (*size).

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())
> torch.Size([10, 10])

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

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

Got it. Thanks a lot. :slight_smile: