title: PyTorch: How to get around the RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables
body: <p>With PyTorch I'm having a problem doing an operation with two Variables:</p>
  
  <pre><code>sub_patch  : [torch.FloatTensor of size 9x9x32]
  
  pred_patch : [torch.FloatTensor of size 5x5x32]
  </code></pre>
  
  <p>sub_patch is a Variable made by torch.zeros
  pred_patch is a Variable of which I index each of the 25 nodes with a nested for-loop, and that I multiply with its corresponding unique filter (sub_filt_patch) of size [5,5,32]. The result is added to its respective place in sub_patch.</p>
  
  <p>This is a piece of my code:</p>
  
  <pre><code>for i in range(filter_sz):
      for j in range(filter_sz):
  
          # index correct filter from filter tensor
          sub_filt_col = (patch_col + j) * filter_sz
          sub_filt_row = (patch_row + i) * filter_sz
  
          sub_filt_patch = sub_filt[sub_filt_row:(sub_filt_row + filter_sz), sub_filt_col:(sub_filt_col+filter_sz), :]
  
          # multiply filter and pred_patch and sum onto sub patch
          sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] += (sub_filt_patch * pred_patch[i,j]).sum(dim=3)
  </code></pre>
  
  <p>The error I get from the bottom line of the piece of code here is</p>
  
  <pre><code>RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables, but detected that there are 2 objects sharing it
  </code></pre>
  
  <p>I get why it happens, since sub_patch is a Variable, and pred_patch is a Variable too, but how can I get around this error? Any help would be greatly appreciated!</p>
  
  <p>Thank you!</p>