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?It'll modify the tensor metadata and will not create a copy of it.
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.
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.
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.
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.
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.
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.
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.
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.
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.
reshape
under the hood.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With