What is the meaning of parameter -1?
You can read -1
as dynamic number of parameters or "anything". Because of that there can be only one parameter -1
in view()
.
If you ask x.view(-1,1)
this will output tensor shape [anything, 1]
depending on the number of elements in x
. For example:
import torch
x = torch.tensor([1, 2, 3, 4])
print(x,x.shape)
print("...")
print(x.view(-1,1), x.view(-1,1).shape)
print(x.view(1,-1), x.view(1,-1).shape)
Will output:
tensor([1, 2, 3, 4]) torch.Size([4])
...
tensor([[1],
[2],
[3],
[4]]) torch.Size([4, 1])
tensor([[1, 2, 3, 4]]) torch.Size([1, 4])