pytorch

Frustrated with pytorch data type in basic tensor operation, how to make it easier?


I am new to pytorch. I am quite frustrated with basic operation with different data types,

for example, in calculating basic determinant,

t2=torch.tensor([[1,2],[3,4]])
print(t2)
tensor([[1, 2],
        [3, 4]])

det=torch.det(t2)
print(det)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[30], line 1
----> 1 det=torch.det(t2)
      2 print(det)

RuntimeError: linalg.det: Expected a floating point or complex tensor as input. Got Long

why calculating this determinant has to require input floating point matrix?

Similarly, below dot product has an error too,

t1=torch.tensor([1,2,3])
print(t1)
np_array=np.array([5,6,7])
t3=torch.from_numpy(np_array)
print(t3)
tensor([5, 6, 7], dtype=torch.int32)

dot_product=torch.dot(t1,t3)
print(dot_product)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[56], line 1
----> 1 dot_product=torch.dot(t1,t3)
      2 print(dot_product)

RuntimeError: dot : expected both vectors to have same dtype, but found Long and Int

similarly

inverse=torch.inverse(t2)
print(inverse)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[57], line 1
----> 1 inverse=torch.inverse(t2)
      2 print(inverse)

RuntimeError: linalg.inv: Expected a floating point or complex tensor as input. Got Long

The above are just basic matrix operation with all integer variables. Why doing this basic operation involves these basic data type error? How to make it easier?

Thanks


Solution

  • Pytorch supports a number of dtypes - you can see the documentation here.

    Specific operations (like taking the determinant) are implemented for specific dtypes.

    From the torch.linalg.det documentation:

    Computes the determinant of a square matrix.
    
    Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions.
    

    Your input t2 is of type int64, also called long:

    t2 = torch.tensor([[1,2],[3,4]])
    print(t2.dtype)
    >torch.int64
    

    int64 is not a supported dtype for torch.linalg.det, so it throws the error. You can fix this by casting to a supported dtype, for example a float tensor:

    t2 = torch.tensor([[1,2],[3,4]]).float() # `.float()` casts to float dtype
    det=torch.det(t2)
    print(det)
    >tensor(-2.)
    

    For your second example, torch.dot expects the inputs to have the same dtype, while your tensors have different dtypes. t1 is of dtype int64, while t3 is of dtype int32. This is because your numpy array is of dtype int32 and torch.from_numpy preserves the numpy dtype. You can fix this by casting t3 to the same dtype as t1

    t1 = torch.tensor([1,2,3])
    np_array = np.array([5,6,7])
    t3 = torch.from_numpy(np_array).long() # `.long()` casts to `int64`
    dot_product = torch.dot(t1,t3)
    print(dot_product)
    > tensor(38)