Khanna111
Khanna111

Reputation: 3913

What does *variable.shape mean in python

I know "*variable_name" assists in packing and unpacking.

But how does variable_name.shape work? Unable to visualize why the second dimension is squeezed out when prefixing with ""?

print("top_class.shape {}".format(top_class.shape))
top_class.shape torch.Size([64, 1])

print("*top_class.shape {}".format(*top_class.shape))
*top_class.shape 64

Upvotes: 1

Views: 3567

Answers (1)

lenik
lenik

Reputation: 23528

for numpy.array that is extensively used in math-related and image processing programs, .shape describes the size of the array for all existing dimensions:

>>> import numpy as np
>>> a = np.zeros((3,3,3))
>>> a
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]]])
>>> a.shape
(3, 3, 3)
>>> 

The asterisk "unpacks" the tuple into several separate arguments, in your case (64,1) becomes 64, 1, so only the first one get printed because there's only one format specification.

Upvotes: 1

Related Questions