title: what is the difference between if-else statement and torch.where in pytorch?
body: <p>See the code snippet:</p>
  
  <pre class="lang-py prettyprint-override"><code>import torch
  x = torch.tensor([-1.], requires_grad=True)
  y = torch.where(x &gt; 0., x, torch.tensor([2.], requires_grad=True))
  y.backward()
  print(x.grad)
  </code></pre>
  
  <p>The output is <code>tensor([0.])</code>, but </p>
  
  <pre class="lang-py prettyprint-override"><code>import torch
  x = torch.tensor([-1.], requires_grad=True)
  if x &gt; 0.:
      y = x
  else:
      y = torch.tensor([2.], requires_grad=True)
  y.backward()
  print(x.grad)
  </code></pre>
  
  <p>The output is <code>None</code>.</p>
  
  <p>I'm confused that why the output of <code>torch.where</code> is <code>tensor([0.])</code>? </p>
  
  <h2>update</h2>
  
  <pre class="lang-py prettyprint-override"><code>import torch
  a = torch.tensor([[1,2.], [3., 4]])
  b = torch.tensor([-1., -1], requires_grad=True)
  a[:,0] = b
  
  (a[0, 0] * a[0, 1]).backward()
  print(b.grad)
  </code></pre>
  
  <p>The output is <code>tensor([2., 0.])</code>. <code>(a[0, 0] * a[0, 1])</code> is not in any way related to <code>b[1]</code>, but the gradient of <code>b[1]</code> is <code>0</code> not <code>None</code>.</p>