In v4 of their API, torch has introduced reshape()
, to align more with the style of numpy. Previously, changing the shape of a torch tensor was done with view()
.
I wondered whether view()
was going to be deprecated now and looked at the docs. Turns out that reshape()
is not just a numpy-friendly alias for view()
, actually, it has a different semantic. Torch tries to give you contiguous memory where possible. If the new view dimensions violate a contiguity constraint, you have to explicitly call contiguous()
before view()
. Reshape
will work even if this constraint is violated, but will silently make a copy of the data.
This is the same behaviour as in numpy, where reshape
can produce copies, too.
A question on view()
vs reshape()
in torch is here: What's the difference between reshape and view in pytorch?
if you need a copy use clone() if you need the same storage use view(). The semantics of reshape() are that it may or may not share the storage and you don't know beforehand.
Up until now, torch only offered view()
. Maybe to intentionally force their developers to care about memory layout. Which makes me wonder how reshape()
works in tensorflow.
In torch, the distinction between a view and a copy could produce complicated bugs. You assume that tensors share data, but they don't. In tensorflow this problem shouldn't exist. A tensorflow tensor is symbolic and doesn't hold a value. A reshape is just an Op in the tensorflow graph. While the graph is evaluated, data in placeholders and variables don't change, so it is clear what data you are working on.
But I don't know if this could hurt performance. Copying a huge tensor can be very expensive. Do I have to be careful when using reshape()
, not to duplicate memory ?