In pytorch, can two tensors with different dtype multiply?

import torch
import numpy as np
num_input = 2
num_example = 1000
true_w = np.array([2, -3.4])
true_b = 4.2

features = torch.normal(torch.zeros(num_example, num_input), 1).float()
w = torch.tensor([true_w]).t()

Xw = features.mm(w)


I run the code in Pycharm, get the error:
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat2’
So I want to know whether two tensors( features and w )with different dtype can multiply?

Hi,

No this is not supported.
Note that the problem here is that np.array created a float64 number while .float() returns a float32. So either changing the dtype in np.array or changing .float() to .double() will solve your issue.

1 Like

Thank you for your replying!

Any reason for not supporting these type of operations? In my case I neet a multiplication between a quasy binary matrix and a float32 matrix. Converting the binary/int matrix to float seems like a huge waste of memory.