Tensor.size Operation

I am trying to understand the Tensor.size operation. Below is code snippet, and corresponding output of the print statements. I am not able to comprehend, what is happening when I use torch.Tensor(input.size()).

import torch

input = torch.Tensor([[1, 2, 3], [1, 2, 3]])
print(input.shape)
print(input.size())
print(torch.Tensor(input.shape))
print(torch.Tensor(input.size()))
torch.Size([2, 3])
torch.Size([2, 3])
tensor([[-7.9238e-35,  3.0887e-41, -7.9239e-35],
        [ 3.0887e-41,  0.0000e+00,  0.0000e+00]])
tensor([[ 0.0000e+00,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00, -7.8882e-35,  3.0887e-41]])

Thanks

When you do torch.Tensor(input.shape) or torch.Tensor(input.size()) you create an empty Tensor that is the same shape as input, except it’s filled with near zero values. Ideally, if you want a Tensor of the same shape use torch.zeros_like(input) instead.

1 Like

Thanks it makes sense now, I was expecting it to have zero values but random near zero values confused me.