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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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.
torch.Tensor()
is just an alias totorch.FloatTensor()
which is the default type of tensor, when nodtype
is specified during tensor construction.From the torch for numpy users notes, it seems that
torch.Tensor()
is a drop-in replacement ofnumpy.empty()
So, in essence
torch.FloatTensor()
andtorch.empty()
does the same job of returning a tensor filled with garbage values of dtypetorch.float32
. Below is a small run: