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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)