Both .flatten()
and .view(-1)
flatten a tensor in PyTorch. What's the difference?
.flatten()
copy the data of the tensor?.view(-1)
faster?.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.