set_grad_enabled¶
- class paddle. set_grad_enabled ( mode ) [source]
-
Create a context which enables or disables dygraph gradient calculation.
- Parameters
-
mode (bool) – whether to enable (True), or disable (False) grad.
- Returns
-
None.
Examples
>>> import paddle >>> x = paddle.to_tensor([1.], stop_gradient=False) >>> is_train = False >>> with paddle.set_grad_enabled(is_train): ... y = x * 2 >>> print(y.stop_gradient) True >>> paddle.set_grad_enabled(True) >>> y = x * 2 >>> print(y.stop_gradient) False >>> paddle.set_grad_enabled(False) >>> y = x * 2 >>> print(y.stop_gradient) True