Difference between torch.tensor and torch.Tensor

In PyTorch docs it is written that torch.Tensor is an alias for torch.FloatTensor. This is not the case when using torch.tensor. For example:

>>> torch.Tensor([1,2,3]).dtype
torch.float32
>>> torch.tensor([1, 2, 3]).dtype
Out[32]: torch.int64
>>> torch.Tensor([True, False]).dtype
torch.float32
>>> torch.tensor([True, False]).dtype
torch.uint8

Isn’t this unnecessarily confusing?

2 Likes

torch.tensor infers the dtype automatically, while torch.Tensor returns a torch.FloatTensor.
I would recommend to stick to torch.tensor, which also has arguments like dtype, if you would like to change the type.

14 Likes

Agree, but my point is about the need of defining an alias torch.FloatTensor, which happens to be very similar in name to torch.tensor but has a very different behaviour and can generate confusion. Is it really needed?

1 Like

I agree, i costs me some time to find the problem caused by that.

The same here: https://stackoverflow.com/questions/48482787/pytorch-memory-model-torch-from-numpy-vs-torch-tensor


arr = np.arange(10, dtype=np.float32).reshape(5, 2)

t0 = torch.Tensor(arr)
t1 = torch.tensor(arr)
t2 = torch.from_numpy(arr)

arr
t0
t1
t2

t2[:, 1] = 23.0

arr
t0
t1
t2

arr
array([[ 0., 23.],
       [ 2., 23.],
       [ 4., 23.],
       [ 6., 23.],
       [ 8., 23.]], dtype=float32)

t0
tensor([[ 0., 23.],
        [ 2., 23.],
        [ 4., 23.],
        [ 6., 23.],
        [ 8., 23.]])

t1
tensor([[0., 1.],
        [2., 3.],
        [4., 5.],
        [6., 7.],
        [8., 9.]])

t2
tensor([[ 0., 23.],
        [ 2., 23.],
        [ 4., 23.],
        [ 6., 23.],
        [ 8., 23.]])

It’s confusing when your wife’s name is Katja, but you call her Tanja right? Unclear side effects are possible in both cases …

Searching for “torch tensor” can lead to different results containing torch.tensor and torch.Tensor results as well…

Agreed that it’s confusing, I get tripped up by this as well.

In addition to the confusion discussed here, it also makes pylint complaining.

a = torch.tensor([1,2,3]) results in the following error:

[pylint E1102] torch.tensor is not callable (not-callable) [E]

but a=torch.Tensor([1,2,3]) works fine.