Hi,
Since gradients are needed for y itself, your can’t modify it inplace.
What you can do is clone it before, that way the cloned version will not be a leaf Variable and can thus be changed inplace:
import torch
from torch.autograd import Variable

x = torch.LongTensor([0,1,0,1,1,0])
y = Variable(torch.zeros(x.size()), requires_grad=True)

tmp = y.clone()
tmp[x == 0] = Variable(torch.Tensor([-5]))

print('y',y)
print('tmp',tmp)