What does "*" operator do before the tensor size in PyTorch?

Viewed 825

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.

2 Answers

* used like this in Python denotes (argument) unpacking. When you prepend it to an iterable (which is what x.size() returns), it unpacks and (here) passes its items to the function as positional arguments. E.g.:

def f(a1, a2):
    print(a1, a2)

f(*["Hello", "World"])

You can check out the Docs links for another example and more detailed description.

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()

Related