torch.autograd.function.FunctionCtx.mark_dirty

FunctionCtx.mark_dirty(*args)[源代码]

将给定的张量标记为在原地操作中被修改。

这应当最多调用一次,要么在 setup_context() 方法中,要么在 forward() 方法中,并且所有的参数都应该作为输入。

在调用forward()时被就地修改的每个张量都应传递给此函数,以确保检查的准确性。函数的调用时间(是在修改前还是后)无关紧要。

示例:
>>> class Inplace(Function):
>>>     @staticmethod
>>>     def forward(ctx, x):
>>>         x_npy = x.numpy() # x_npy shares storage with x
>>>         x_npy += 1
>>>         ctx.mark_dirty(x)
>>>         return x
>>>
>>>     @staticmethod
>>>     @once_differentiable
>>>     def backward(ctx, grad_output):
>>>         return grad_output
>>>
>>> a = torch.tensor(1., requires_grad=True, dtype=torch.double).clone()
>>> b = a * a
>>> Inplace.apply(a)  # This would lead to wrong gradients!
>>>                   # but the engine would not know unless we mark_dirty
>>> b.backward() # RuntimeError: one of the variables needed for gradient
>>>              # computation has been modified by an inplace operation
本页目录