title: What is the difference between view() and unsqueeze() in Torch?
body: <p>Use of <a href="https://github.com/torch/nn/blob/master/doc/simple.md#unsqueeze" rel="noreferrer"><code>unsqueeze()</code></a>:</p>
  <pre><code>input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
  print(input.unsqueeze(0).size()) # prints - torch.size([1, 2, 4, 3])
  </code></pre>
  <p>Use of <a href="https://github.com/torch/torch7/blob/master/doc/tensor.md#result-viewresult-tensor-sizes" rel="noreferrer"><code>view()</code></a>:</p>
  <pre><code>input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
  print(input.view(1, -1, -1, -1).size()) # prints - torch.size([1, 2, 4, 3])
  </code></pre>
  <p>According to documentation, <code>unsqueeze()</code> inserts singleton dim at position given as parameter and <code>view()</code> creates a view with different dimensions of the storage associated with <code>tensor</code>.</p>
  <p>What <code>view()</code> does is clear to me, but I am unable to distinguish it from <code>unsqueeze()</code>. Moreover, I don't understand when to use <code>view()</code> and when to use <code>unsqueeze()</code>?</p>
  <p>Any help with good explanation would be appreciated!</p>