I have a problem where I need to index a Variable that requires gradient using a ByteTensor. However, this seems to be an in-place operation, and so PyTorch throws an error:
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
The following piece of code reproduces the error in a very simple way:
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)
y[x == 0] = Variable(torch.Tensor([-5]))
print('y',y)
Clearly, the intended output would be:
y Variable containing:
-5
0
-5
0
0
-5
[torch.FloatTensor of size (6,)]
As a workaround, we can use an explicit for-loop over the elements of x:
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)
for i in range(x.size(0)):
if x[i] == 0:
y[i] = Variable(torch.Tensor([-5]))
print('y',y)
Here, y[i] = Variable(torch.Tensor([-5]))
is not an in-place operation (don’t know why) and therefore it works. However, this for-loop is very inefficient, taking a lot of time if x
has a large dimension.
Are there any alternatives? Thanks in advance.