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 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)
z = torch.empty(4)
z = 1.0
z = euler_angles * 1
z = euler_angles
z = euler_angles * z # Error originates here
matrix = pytorch3d.transforms.euler_angles_to_matrix(z[:3], "XYZ")
loss = torch.sum((matrix - target)**2)
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 = euler_angles * z.clone()