title: tensorflow: can reshape() create a copy?
body: <p>In v4 of their API, torch has introduced <code>reshape()</code>, to align more with the style of numpy. Previously, changing the shape of a torch tensor was done with <code>view()</code>.</p>
  
  <p>I wondered whether <code>view()</code> was going to be deprecated now and looked at the <a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.view" rel="nofollow noreferrer">docs</a>. Turns out that <code>reshape()</code> is not just a numpy-friendly alias for <code>view()</code>, 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 <code>contiguous()</code> before <code>view()</code>. <code>Reshape</code> 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 <code>reshape</code> <a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.reshape.html#numpy-reshape" rel="nofollow noreferrer">can produce copies, too</a>.</p>
  
  <p>A question on <code>view()</code> vs <code>reshape()</code> in torch is here: <a href="https://stackoverflow.com/questions/49643225/whats-the-difference-between-reshape-and-view-in-pytorch">What&#39;s the difference between reshape and view in pytorch?</a></p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Up until now, torch only offered <code>view()</code>. Maybe to intentionally force their developers to care about memory layout. Which makes me wonder how <a href="https://www.tensorflow.org/api_docs/python/tf/reshape" rel="nofollow noreferrer"><code>reshape()</code></a> works in tensorflow.</p>
  
  <p>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.</p>
  
  <p>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 <code>reshape()</code>, not to duplicate memory ?</p>