Is there a good way to modify some values in a pytorch tensor while preserving the autograd functionality?
Sometimes I need to modify some of the values in a pytorch tensor. For example, given a tensor x, I need to multiply its positive part by 2 and multiply its negative part by 3:
import torch

x = torch.randn(1000, requires_grad=True)
x[x&gt;0] = 2 * x[x&gt;0]
x[x&lt;0] = 3 * x[x&lt;0]

y = x.sum()
y.backward()

However such inplace operations always break the graph for autograd:
Traceback (most recent call last):
  File &quot;test_rep.py&quot;, line 4, in &lt;module&gt;
    x[x&gt;0] = 2 * x[x&gt;0]
RuntimeError: a leaf Variable that requires grad is being used in an in-place operation.

Therefore, so far I've been using the following workaround:
import torch

x = torch.randn(1000, requires_grad=True)
y = torch.zeros_like(x, device=x.device)

y[x&gt;0] = 2 * x[x&gt;0]
y[x&lt;0] = 3 * x[x&lt;0]

z = y.sum()
z.backward()

which results in manually creating new tensors. I wonder if there is a better way to do this.

How about like following?
import torch

x = torch.randn(1000, requires_grad=True)
x = torch.where(x&gt;0, x*2, x)
x = torch.where(x&lt;0, x*3, x)

y = x.sum()
y.backward()

