Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 384
nkhuyu Avatar asked Jul 02 '18 04:07

nkhuyu


2 Answers

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]])
like image 122
kmario23 Avatar answered Sep 19 '22 03:09

kmario23


Quick Answer: torch.empty() creates tensor with any data type you want, torch.Tensor() only creates tensors of type torch.FloatTensor. So torch.Tensor() is a special case of torch.empty()

Detailed Answer:

torch.empty() returns a tensor filled with uninitialized data. With arguments you can specify the shape of the tensor, the output tensor, the data type... (see tensor.empty() documentation )

This means you can create a tensor of floats, int... If no data type is specified then the chosen one is your default torch.Tensor type (which is torch.FloatTensor by default and you can change it using torch.set_default_tensor_type())

torch.Tensor() is simply a special case of torch.empty() where the data type is torch.FloatTensor.

like image 32
HLeb Avatar answered Sep 19 '22 03:09

HLeb