BackwardC 函数 ¶
- 类 torch.autograd.function.BackwardCFunction[source][source] ¶
此类用于内部 autograd 工作。请勿使用。
- mark_dirty(*args)[source]¶
将给定的张量标记为就地操作已修改。
应在
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
- mark_non_differentiable(*args)[source]¶
标记输出为不可微分。
应在
setup_context()
或forward()
方法中最多调用一次,并且所有参数应为张量输出。这将标记输出为不需要梯度,从而提高反向计算的效率。您仍然需要在
backward()
中接受每个输出的梯度,但它始终将是一个与相应输出形状相同的零张量。- 这通常用于从排序返回的索引。例如,请参阅示例::
>>> class Func(Function): >>> @staticmethod >>> def forward(ctx, x): >>> sorted, idx = x.sort() >>> ctx.mark_non_differentiable(idx) >>> ctx.save_for_backward(x, idx) >>> return sorted, idx >>> >>> @staticmethod >>> @once_differentiable >>> def backward(ctx, g1, g2): # still need to accept g2 >>> x, idx = ctx.saved_tensors >>> grad_input = torch.zeros_like(x) >>> grad_input.index_add_(0, idx, g1) >>> return grad_input
- save_for_backward(*tensors)[source]¶
保存给定的张量以供未来调用
backward()
。应最多调用一次,在
setup_context()
或forward()
方法中,并且仅与张量一起调用。所有打算在反向传播中使用的张量都应使用
save_for_backward
(而不是直接在ctx
上)保存,以防止梯度错误和内存泄漏,并启用保存的张量钩子。请参阅torch.autograd.graph.saved_tensors_hooks
。注意,如果保存了中间张量(既不是
forward()
的输入也不是输出),则您的自定义函数可能不支持双反向。不支持双反向的自定义函数应使用@once_differentiable
装饰其backward()
方法,以便在执行双反向时引发错误。如果您想支持双反向,可以基于输入重新计算中间张量,或者将中间张量作为自定义函数的输出返回。有关详细信息,请参阅双反向教程。在
backward()
中,可以通过saved_tensors
属性访问保存的张量。在将它们返回给用户之前,会进行检查以确保它们没有被用于任何修改其内容的就地操作。参数也可以
None
。这是一个空操作。有关如何使用此方法的详细信息,请参阅扩展 torch.autograd。
- 示例::
>>> class Func(Function): >>> @staticmethod >>> def forward(ctx, x: torch.Tensor, y: torch.Tensor, z: int): >>> w = x * z >>> out = x * y + y * z + w * y >>> ctx.save_for_backward(x, y, w, out) >>> ctx.z = z # z is not a tensor >>> return out >>> >>> @staticmethod >>> @once_differentiable >>> def backward(ctx, grad_out): >>> x, y, w, out = ctx.saved_tensors >>> z = ctx.z >>> gx = grad_out * (y + y * z) >>> gy = grad_out * (x + z + w) >>> gz = None >>> return gx, gy, gz >>> >>> a = torch.tensor(1., requires_grad=True, dtype=torch.double) >>> b = torch.tensor(2., requires_grad=True, dtype=torch.double) >>> c = 4 >>> d = Func.apply(a, b, c)
- save_for_forward(*tensors)[源代码] ¶
保存给定的张量以供未来调用
jvp()
。save_for_forward
应最多只调用一次,在setup_context()
或forward()
方法中,并且所有参数应为张量。在
jvp()
中,保存的对象可以通过saved_tensors
属性访问。参数也可以
None
。这是一个空操作。查看 torch.autograd 的扩展部分以获取此方法的使用详情。
- 示例::
>>> class Func(torch.autograd.Function): >>> @staticmethod >>> def forward(ctx, x: torch.Tensor, y: torch.Tensor, z: int): >>> ctx.save_for_backward(x, y) >>> ctx.save_for_forward(x, y) >>> ctx.z = z >>> return x * y * z >>> >>> @staticmethod >>> def jvp(ctx, x_t, y_t, _): >>> x, y = ctx.saved_tensors >>> z = ctx.z >>> return z * (y * x_t + x * y_t) >>> >>> @staticmethod >>> def vjp(ctx, grad_out): >>> x, y = ctx.saved_tensors >>> z = ctx.z >>> return z * grad_out * y, z * grad_out * x, None >>> >>> a = torch.tensor(1., requires_grad=True, dtype=torch.double) >>> t = torch.tensor(1., dtype=torch.double) >>> b = torch.tensor(2., requires_grad=True, dtype=torch.double) >>> c = 4 >>> >>> with fwAD.dual_level(): >>> a_dual = fwAD.make_dual(a, t) >>> d = Func.apply(a_dual, b, c)
- set_materialize_grads(value)[source]¶
设置是否将梯度张量进行显式化。默认为
True
。应仅从
setup_context()
或forward()
方法中调用此方法。如果
True
,未定义的梯度张量在调用backward()
和jvp()
方法之前将被扩展为零张量。- 示例::
>>> class SimpleFunc(Function): >>> @staticmethod >>> def forward(ctx, x): >>> return x.clone(), x.clone() >>> >>> @staticmethod >>> @once_differentiable >>> def backward(ctx, g1, g2): >>> return g1 + g2 # No check for None necessary >>> >>> # We modify SimpleFunc to handle non-materialized grad outputs >>> class Func(Function): >>> @staticmethod >>> def forward(ctx, x): >>> ctx.set_materialize_grads(False) >>> ctx.save_for_backward(x) >>> return x.clone(), x.clone() >>> >>> @staticmethod >>> @once_differentiable >>> def backward(ctx, g1, g2): >>> x, = ctx.saved_tensors >>> grad_input = torch.zeros_like(x) >>> if g1 is not None: # We must check for None now >>> grad_input += g1 >>> if g2 is not None: >>> grad_input += g2 >>> return grad_input >>> >>> a = torch.tensor(1., requires_grad=True) >>> b, _ = Func.apply(a) # induces g2 to be undefined