PyLayerContext¶
- class paddle.autograd. PyLayerContext [source]
-
The object of this class is a context that is used in PyLayer to enhance the function.
Examples
import paddle from paddle.autograd import PyLayer class cus_tanh(PyLayer): @staticmethod def forward(ctx, x): # ctx is a object of PyLayerContext. y = paddle.tanh(x) ctx.save_for_backward(y) return y @staticmethod def backward(ctx, dy): # ctx is a object of PyLayerContext. y, = ctx.saved_tensor() grad = dy * (1 - paddle.square(y)) return grad
-
save_for_backward
(
*tensors
)
save_for_backward¶
-
Saves given tensors that backward need. Use
saved_tensor
in the backward to get the saved tensors.Note
This API should be called at most once, and only inside forward.
- Parameters
-
tensors (list of Tensors) – Tensors to be stored.
- Returns
-
None
Examples
import paddle from paddle.autograd import PyLayer class cus_tanh(PyLayer): @staticmethod def forward(ctx, x): # ctx is a context object that store some objects for backward. y = paddle.tanh(x) # Pass tensors to backward. ctx.save_for_backward(y) return y @staticmethod def backward(ctx, dy): # Get the tensors passed by forward. y, = ctx.saved_tensor() grad = dy * (1 - paddle.square(y)) return grad
-
saved_tensor
(
)
saved_tensor¶
-
Get the tensors stored by
save_for_backward
.- Returns
-
If context contains tensors stored by save_for_backward, then return these tensors, otherwise return None.
- Return type
-
list of Tensors or None
Examples
import paddle from paddle.autograd import PyLayer class cus_tanh(PyLayer): @staticmethod def forward(ctx, x): # ctx is a context object that store some objects for backward. y = paddle.tanh(x) # Pass tensors to backward. ctx.save_for_backward(y) return y @staticmethod def backward(ctx, dy): # Get the tensors passed by forward. y, = ctx.saved_tensor() grad = dy * (1 - paddle.square(y)) return grad
-
save_for_backward
(
*tensors
)