I am now learning building neural networks in PyTorch. Here are the codes cut from the .py
file:
x = torch.unsqueeze(torch.linspace(-1, 1, 1000), dim=1)
y = x.pow(2) + 0.1*torch.normal(torch.zeros(*x.size()))
I am quite comfused about the utility of the *
operator before x.size()
. I tried to delete it and plot the scatter graph, which was proved the same as the one with *
not removed.
I also checked the official documentation of size
in https://pytorch.org/docs/stable/tensors.html but I couldn't figure it out.
Image of torch.size item in documentation
I'd appreciate it very much if you may help me.
the reason that * makes no difference in the results here is because torch.zero
except both a variable number of arguments and a collection like a list or tuple as mentioned here. It does not mean * itself is useless.
Then, since torch.Size()
class is a subclass of python tuple, one can unpack it using *. (x.size()
will return a torch.Size()
object)
So to wrap up, x.size()
would give you (1000, 1)
and *x.size()
in the argument would give you 1000, 1
and both are accepted by torch.zeros()