what's the difference between torch.Tensor() vs torch.empty() in pytorch?

nkhuyu picture nkhuyu · Jul 2, 2018 · Viewed 14.4k times · Source

I have tried it out as below. It seems to me they're the same. What's the difference between torch.Tensor() vs torch.empty() in pytorch?

enter image description here

Answer

kmario23 picture kmario23 · Jul 2, 2018

torch.Tensor() is just an alias to torch.FloatTensor() which is the default type of tensor, when no dtype is specified during tensor construction.

From the torch for numpy users notes, it seems that torch.Tensor() is a drop-in replacement of numpy.empty()

So, in essence torch.FloatTensor() and torch.empty() does the same job of returning a tensor filled with garbage values of dtype torch.float32. Below is a small run:

In [87]: torch.FloatTensor(2, 3)
Out[87]: 
tensor([[-1.0049e+08,  4.5688e-41, -8.9389e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

In [88]: torch.FloatTensor(2, 3)
Out[88]: 
tensor([[-1.0049e+08,  4.5688e-41, -1.6512e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

In [89]: torch.empty(2, 3)
Out[89]: 
tensor([[-1.0049e+08,  4.5688e-41, -9.0400e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

In [90]: torch.empty(2, 3)
Out[90]: 
tensor([[-1.0049e+08,  4.5688e-41, -9.2852e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])