RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
You got an error like the one below in pytorch.
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor []], which is output 0 of SelectBackward, is at version 4; expected version 3 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!
It means that you use a tensor or its part to compute a part of the same tensor. Code bellow demonstrates this mistake.
import pytorch3d.transforms
import torcheuler_angles = torch.nn.Parameter(torch.Tensor([0.1, 0.03,-0.2]))target = torch.eye(3)torch.autograd.set_detect_anomaly(True)
optimizer = torch.optim.Adam([euler_angles], lr=0.05)
optimizer.zero_grad()
z = torch.empty(4)
z[3] = 1.0
z[0] = euler_angles[0] * 1
z[1] = euler_angles[1]
z[2] = euler_angles[2] * z[3] # Error originates here
matrix = pytorch3d.transforms.euler_angles_to_matrix(z[:3], "XYZ")
loss = torch.sum((matrix - target)**2)
loss.backward()
optimizer.step()
To fix this error clone the part of the tensor used on the right-hand side of the assignment expression. For the code above that would be:
z[2] = euler_angles[2] * z[3].clone()