Difference between `torch.dtype` and `torch.tensortype`

Problem

Recently I ran into an error when I tried to convert an array into float tensor using torch.tensor(arr, dtype=torch.FloatTensor).

TypeError: tensor(): argument 'dtype' must be torch.dtype, not torch.tensortype

I checked out the documentation here and it turns out I did not have correct understanding about data type. I previously thought torch.float is equivalent to torch.FloatTensor.

However, I still do not get the difference between those two and rationale behind this design, which appears to be redundant at first glimpse. Maybe torch.FloatTensor depends on particular storage type whereas torch.float doe not?

1 Like

Hi,

dtype is a datatype, like torch.float or torch.double
tensortype is a type of tensor, like torch.FloatTensor, torch.DoubleTensor.

5 Likes

This below two code does the same thing. You can either explicitly provide the datatype or use that type of tensor.

float_tensor = torch.tensor([4,5,6], dtype=torch.float)

float_tensor = torch.FloatTensor([4,5,6])

dtype is how actually pytorch call data type within itself when we try create a tensor.
When we check the documentation, it says there, dtype=dtype,

When we check the documentation of torch.FloatTensor
It is actually is torch.tensor([array], dtype= torch.float)

Here, internally, pytorch refers torch.float as dtype and for ease, they also created another function to create tensor of float.