Difference between torch.tensor() and torch.Tensor()

Hi,

My question is the title, in Pytorch’s document, as I understand, the performance of torch.tensor() and torch.Tensor() is similar.

However, when I train my model, my data was read by torch.tensor() and the performance was very poor, but when I switch to torch.Tensor(), the model predicts correctly what I want.

In my undertanding, torch.tensor() is just a wrapper to torch.Tensor with configurable options.
Your observation seems strange.

Can you check if there is any difference in data loaded when you use torch.tensor() vs. torch.Tensor?

1 Like

@InnovArul Thank you for your reply,

I just change torch.tensor() to torch.Tensor(), I am training a regression model and no matter what architecture I used, after a short time of training, all the output will tend to be similar. (at first, I think it is the batch norm problem)

Then I decided to create a new project and code again, this time when I use torch.Tensor(my_target), I got something like this

>>> torch.Tensor(10)
tensor([2.8958e+32, 3.4970e-38, 1.0141e+31, 1.1210e-44, 9.9920e-38, 1.9793e-18,
        4.5036e+16, 4.0556e-08, 2.5038e-12, 4.0058e-11])

Which is normal in case torch.tensor()

>>> torch.tensor(10)
tensor(10)

Therefore, I decided to come back to my old repository and change tensor to Tensor and the miracle happened.

For additional information, I am using pytorch 1.7.0

torch.Tensor(10) will return an uninitialized FloatTensor with 10 values, while torch.tensor(10) will return a LongTensor containing a single value (10).
I would recommend to use the second approach (lowercase t) or any other factory method instead of creating uninitialized tensors via torch.Tensor or torch.FloatTensor.
To explicitly create a tensor with uninitialized memory, you could still use torch.empty.

3 Likes