title: PyTorch memory model: "torch.from_numpy()" vs "torch.Tensor()"
body: <p>I'm trying to have an in-depth understanding of how PyTorch Tensor memory model works.</p>
  
  <pre><code># input numpy array
  In [91]: arr = np.arange(10, dtype=float32).reshape(5, 2)
  
  # input tensors in two different ways
  In [92]: t1, t2 = torch.Tensor(arr), torch.from_numpy(arr)
  
  # their types
  In [93]: type(arr), type(t1), type(t2)
  Out[93]: (numpy.ndarray, torch.FloatTensor, torch.FloatTensor)
  
  # ndarray 
  In [94]: arr
  Out[94]: 
  array([[ 0.,  1.],
         [ 2.,  3.],
         [ 4.,  5.],
         [ 6.,  7.],
         [ 8.,  9.]], dtype=float32)
  </code></pre>
  
  <hr>
  
  <p>I know that PyTorch tensors <em>share the memory buffer</em> of NumPy ndarrays. Thus, changing one will be reflected in the other. So, here I'm slicing and updating some values in the Tensor <code>t2</code></p>
  
  <pre><code>In [98]: t2[:, 1] = 23.0
  </code></pre>
  
  <p>And as expected, it's updated in <code>t2</code> and <code>arr</code> since they share the same memory buffer.</p>
  
  <pre><code>In [99]: t2
  Out[99]: 
  
    0  23
    2  23
    4  23
    6  23
    8  23
  [torch.FloatTensor of size 5x2]
  
  
  In [101]: arr
  Out[101]: 
  array([[  0.,  23.],
         [  2.,  23.],
         [  4.,  23.],
         [  6.,  23.],
         [  8.,  23.]], dtype=float32)
  </code></pre>
  
  <p>But, <strong><code>t1</code> is also updated</strong>. Remember that <code>t1</code> was constructed using <code>torch.Tensor()</code> whereas <code>t2</code> was constructed using <code>torch.from_numpy()</code></p>
  
  <pre><code>In [100]: t1
  Out[100]: 
  
    0  23
    2  23
    4  23
    6  23
    8  23
  [torch.FloatTensor of size 5x2]
  </code></pre>
  
  <p>So, no matter whether we use <a href="http://pytorch.org/docs/master/torch.html#torch.from_numpy" rel="noreferrer"><code>torch.from_numpy()</code></a> or <a href="http://pytorch.org/docs/master/tensors.html#torch-tensor" rel="noreferrer"><code>torch.Tensor()</code></a> to construct a tensor from an ndarray, <strong>all</strong> such tensors and ndarrays share the same memory buffer.</p>
  
  <p>Based on this understanding, my question is why does a dedicated function <a href="http://pytorch.org/docs/master/torch.html#torch.from_numpy" rel="noreferrer"><code>torch.from_numpy()</code></a> exists when simply <a href="http://pytorch.org/docs/master/tensors.html#torch-tensor" rel="noreferrer"><code>torch.Tensor()</code></a> can do the job?</p>
  
  <p>I looked at the PyTorch documentation but it doesn't mention anything about this? Any ideas/suggestions?</p>