NestedIOFunction
- 类torch.autograd.function.NestedIOFunction(*args, **kwargs)[源代码]
-
此类仅为了向后兼容而存在。对于任何新用例,请使用
Function
替代此类。- backward_extended(*grad_output)[源代码]
-
用户自定义的反向传播。
- forward_extended(*input)[源代码]
-
用户定义的向前传播。
- staticjvp(ctx, *grad_inputs)
-
定义一个公式,用于使用前向模式自动微分来计算操作的导数。
此函数需要被所有子类重写。它必须接受一个上下文
ctx
作为第一个参数,然后是与forward()
方法接收到的输入数量相同的参数(对于前向函数中非张量类型的输入将传递 None)。它应该返回与forward()
输出相同数量的张量。每个参数是相对于给定输入的梯度,每个返回值应该是相对于相应输出的梯度。如果某个输出不是 Tensor 或者函数对该输出不可微,则可以为该输入传递 None 作为梯度。你可以使用
ctx
对象将任何值从前进方向传递到此函数。- 返回类型
- mark_dirty(*args, **kwargs)[源代码]
-
参见
Function.mark_dirty()
。
- mark_non_differentiable(*args, **kwargs)[源代码]
-
参见
Function.mark_non_differentiable()
。
- save_for_backward(*args)[源代码]
-
参见
Function.save_for_backward()
。
- save_for_forward(*tensors)
-
保存给定的张量,以便将来调用
jvp()
时使用。save_for_forward
应该只调用一次,可以在setup_context()
或者forward()
方法中进行,并且所有参数都应该是张量。In
jvp()
, saved objects can be accessed via thesaved_tensors
attribute.参数也可以是
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)
- 属性 saved_tensors
-
参见
Function.saved_tensors()
。
- set_materialize_grads(值)
-
设置是否将梯度张量进行具体化。默认值为
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
- `static`setup_context(ctx, inputs, output)
-
有两种方式来定义autograd.Function的前向传递。
要么:
-
使用签名
forward(ctx, *args, **kwargs)
重写 forward 方法。不重写setup_context
。在forward
方法中为反向传播设置 ctx。 -
重写带有签名
forward(*args, **kwargs)
的 forward 方法,并且重写setup_context
方法。设置 ctx 以供反向传播使用应在setup_context
方法中进行(而不是在forward
方法中)。
了解更多详情,请参阅
torch.autograd.Function.forward()
和 扩展 torch.autograd。- 返回类型
-
- staticvjp(ctx, *grad_outputs)
-
定义一个公式,用于通过反向模式自动微分来区分操作。
此函数需要在所有子类中被重写。(定义此函数相当于定义了
vjp
函数。)它必须接受一个上下文
ctx
作为第一个参数,然后是与forward()
返回的输出数量相同的参数(前向函数中非张量输出将传递 None),并且应该返回与forward()
输入数量相等的张量。每个参数是相对于给定输出的梯度,每个返回值应该是相对于相应输入的梯度。如果一个输入不是张量或是一个不需要计算梯度的张量,则可以为该输入传递 None 作为梯度。可以使用上下文来检索在前向传递中保存的张量。它还有一个属性
ctx.needs_input_grad
,这是一个布尔元组,表示每个输入是否需要梯度。例如,如果forward()
的第一个输入相对于输出需要计算梯度,则backward()
将设置ctx.needs_input_grad[0] = True
。- 返回类型
- staticvmap(info, in_dims, *args)
-
在
torch.vmap()
中定义此 autograd.Function 的行为。为了使
torch.autograd.Function()
支持torch.vmap()
,你必须重写这个静态方法或将generate_vmap_rule
设置为True
(但不能同时进行)。如果你选择覆盖这个静态方法:它必须接受
-
将一个
info
对象作为第一个参数。其中,info.batch_size
指定了被vmapped的维度大小,而info.randomness
是传递给torch.vmap()
的随机性选项。 -
作为第二个参数传递一个
in_dims
元组。对于args
中的每个参数,in_dims
包含一个对应的Optional[int]
值。如果参数不是张量或不被vmapped处理,则该值为None
;否则,它是一个整数,表示正在被vmapped的张量的维度。 -
*args
,这与传给forward()
的参数相同。
vmap 静态方法的返回值是一个元组
(output, out_dims)
。类似于in_dims
,out_dims
应该与output
具有相同的结构,并且每个输出都包含一个out_dim
,以指定该输出是否具有 vmap 维度及其索引。请参见使用 autograd.Function 扩展 torch.func以获取更多详细信息。
-