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

Does .flatten() copy the data of the tensor?
Is .view(-1) faster?
Is there any situation that .flatten() doesn't work?


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.


