Since version 0.4.0, it is possible to use torch.tensor
and torch.Tensor
What is the difference? What was the reasoning for providing these two very similar and confusing alternatives?
A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
A PyTorch tensor is homogenous, i.e., all the elements of a tensor are of the same data type. We can access the data type of a tensor using the ". dtype" attribute of the tensor. It returns the data type of the tensor.
torch. cat (tensors, dim=0, *, out=None) → Tensor. Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty. torch.cat() can be seen as an inverse operation for torch.
In PyTorch torch.Tensor
is the main tensor class. So all tensors are just instances of torch.Tensor
.
When you call torch.Tensor()
you will get an empty tensor without any data
.
In contrast torch.tensor
is a function which returns a tensor. In the documentation it says:
torch.tensor(data, dtype=None, device=None, requires_grad=False) → Tensor
Constructs a tensor with
data
.
tensor_without_data = torch.Tensor()
But on the other side:
tensor_without_data = torch.tensor()
Will lead to an error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-ebc3ceaa76d2> in <module>() ----> 1 torch.tensor() TypeError: tensor() missing 1 required positional arguments: "data"
Similar behaviour for creating a tensor without data
like with: torch.Tensor()
can be achieved using:
torch.tensor(())
Output:
tensor([])
According to discussion on pytorch discussion torch.Tensor
constructor is overloaded to do the same thing as both torch.tensor
and torch.empty
. It is thought this overload would make code confusing, so split torch.Tensor
into torch.tensor
and torch.empty
.
So yes, to some extent, torch.tensor
works similarly to torch.Tensor (when you pass in data). no, neither should be more efficient than the other. It's just that the torch.empty
and torch.tensor
have a nicer API than torch.Tensor
constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With