PyTorch: difference between type(a), a.type, a.type()

Viewed 190

suppose a is a tensor, then what's the difference between:

type(a)

a.type

a.type()

I couldn't find a document differentiating these.

2 Answers

type is the python in-built method.

  • It will return type of object. like <class 'torch.Tensor'>

torch.Tensor.type (x.type()) is pytorch in-built method.

  • It will return type of data stored inside tensor. like torch.DoubleTensor, etc.

Edit:

And about x.type() vs x.type - When you write a function name with parentheses x.type () it will actually execute the function and return its value. Whereas without parentheses x.type it is simply a reference to function.

type(a) - returns the class type
a.type - returns built method type
a.type() - return the data type

>>> a = torch.Tensor()
>>> type(a)
<class 'torch.Tensor'>
>>> a.type
<built-in method type of Tensor object at 0x7f6a0acfc140>
>>> a.type()    
'torch.FloatTensor'                                                     
Related