Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between .flatten() and .view(-1) in PyTorch?

Tags:

Both .flatten() and .view(-1) flatten a tensor in PyTorch. What's the difference?

  1. Does .flatten() copy the data of the tensor?
  2. Is .view(-1) faster?
  3. Is there any situation that .flatten() doesn't work?
like image 763
ipid Avatar asked Jul 27 '19 16:07

ipid


People also ask

What does view (- 1 do in PyTorch?

It'll modify the tensor metadata and will not create a copy of it.

What is flatten in PyTorch?

PyTorch Flatten is used to reshape any tensor with different dimensions to a single dimension so that we can do further operations on the same input data. The shape of the tensor will be the same as that of the number of elements in the tensor.

What does view () do in PyTorch?

PyTorch allows a tensor to be a View of an existing tensor. View tensor shares the same underlying data with its base tensor. Supporting View avoids explicit data copy, thus allows us to do fast and memory efficient reshaping, slicing and element-wise operations.

What is the difference between View and reshape PyTorch?

The semantics of reshape() are that it may or may not share the storage and you don't know beforehand. Another difference is that reshape() can operate on both contiguous and non-contiguous tensor while view() can only operate on contiguous tensor.

What is PyTorch flatten and how to use it?

PyTorch Flatten | What is PyTorch Flatten along with Examples? PyTorch Flatten is used to reshape any tensor with different dimensions to a single dimension so that we can do further operations on the same input data. The shape of the tensor will be the same as that of the number of elements in the tensor.

What is a PyTorch view?

In PyTorch, you can create a view on top of the existing tensor. View does not explicitly copy the data but shares the same underlying data of the base tensor. Not keeping a separate copy allows for faster reshaping, slicing, and element-wise operations in the memory. In this example of the PyTorch view, we create a 1-D tensor that has 16 elements.

Does the PyTorch view share memory with the base tensor?

In this example, we show that the PyTorch view shares the same memory of the base tensor and any change in view effects the base tensor as well.

How many elements are there in a PyTorch view?

In this example of the PyTorch view, we create a 1-D tensor that has 16 elements. We can create a view on top of this tensor of shape 2×8.


2 Answers

In addition to @adeelh's comment, there is another difference: torch.flatten() results in a .reshape(), and the differences between .reshape() and .view() are:

  • [...] torch.reshape may return a copy or a view of the original tensor. You can not count on that to return a view or a copy.

  • Another difference is that reshape() can operate on both contiguous and non-contiguous tensor while view() can only operate on contiguous tensor. Also see here about the meaning of contiguous

For context:

  • The community requested for a flatten function for a while, and after Issue #7743, the feature was implemented in the PR #8578.

  • You can see the implementation of flatten here, where a call to .reshape() can be seen in return line.

like image 79
Berriel Avatar answered Sep 22 '22 07:09

Berriel


flatten is simply a convenient alias of a common use-case of view.1

There are several others:

Function Equivalent view logic
flatten() view(-1)
flatten(start, end) view(*t.shape[:start], -1, *t.shape[end+1:])
squeeze() view(*[s for s in t.shape if s != 1])
unsqueeze(i) view(*t.shape[:i-1], 1, *t.shape[i:])

Note that flatten allows you to flatten a specific contiguous subset of dimensions, with the start_dim and end_dim arguments.


  1. Actually the superficially equivalent reshape under the hood.
like image 30
iacob Avatar answered Sep 21 '22 07:09

iacob