Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between torch.tensor and torch.Tensor?

Tags:

python

pytorch

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?

like image 851
Juan Leni Avatar asked Aug 18 '18 19:08

Juan Leni


People also ask

What is torch tensor?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

How do I know what type of torch tensor I need?

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.

What is torch cat?

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.


2 Answers

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.


This also also explains why it is no problem creating an empty tensor instance of `torch.Tensor` without `data` by calling:
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" 

But in general there is no reason to choose `torch.Tensor` over `torch.tensor`. Also `torch.Tensor` lacks a docstring.

Similar behaviour for creating a tensor without data like with: torch.Tensor() can be achieved using:

torch.tensor(()) 

Output:

tensor([]) 
like image 163
MBT Avatar answered Sep 20 '22 00:09

MBT


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.

like image 41
Neelisha SAXENA Avatar answered Sep 21 '22 00:09

Neelisha SAXENA