快捷方式

BackwardCFunction

class torch.autograd.function.BackwardCFunction[source][source]

此類用於內部 autograd 工作。請勿使用。

apply(*args)[source][source]

在反向傳播期間執行此節點時使用的 apply 方法

apply_jvp(*args)[source][source]

在前向傳播期間執行前向模式 AD 時使用的 apply_jvp 方法

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() 儲存給定的張量。

save_for_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)[source]

為將來呼叫 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

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源