MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / clear_gradient

Method clear_gradient

python/paddle/base/framework.py:2074–2108  ·  view source on GitHub ↗

**Notes**: **1. This API is ONLY available in Dygraph mode** **2. Use it only Variable has gradient, normally we use this for Parameters since other temporal Variable will be deleted by Python's GC** Clear (set to ``0`` ) the Gradient of Current Variable

(self)

Source from the content-addressed store, hash-verified

2072
2073 @fake_interface_only
2074 def clear_gradient(self):
2075 """
2076 **Notes**:
2077 **1. This API is ONLY available in Dygraph mode**
2078
2079 **2. Use it only Variable has gradient, normally we use this for Parameters since other temporal Variable will be deleted by Python's GC**
2080
2081 Clear (set to ``0`` ) the Gradient of Current Variable
2082
2083 Returns: None
2084
2085 Examples:
2086 .. code-block:: pycon
2087
2088 >>> import paddle
2089 >>> import paddle.base as base
2090 >>> import numpy as np
2091
2092 >>> x = np.ones([2, 2], np.float32)
2093 >>> inputs2 = []
2094 >>> for _ in range(10):
2095 >>> tmp = paddle.to_tensor(x)
2096 >>> tmp.stop_gradient=False
2097 >>> inputs2.append(tmp)
2098 >>> ret2 = paddle.add_n(inputs2)
2099 >>> loss2 = paddle.sum(ret2)
2100 >>> loss2.retain_grads()
2101 >>> loss2.backward()
2102 >>> print(loss2.gradient())
2103 >>> loss2.clear_gradient()
2104 >>> print("After clear {}".format(loss2.gradient()))
2105 1.0
2106 After clear 0.0
2107 """
2108 pass
2109
2110 def register_hook(self, hook):
2111 import paddle

Callers 15

clear_gradMethod · 0.80
clear_grad_funcMethod · 0.80
clear_gradMethod · 0.80
clear_grad_funcMethod · 0.80
toMethod · 0.80
manual_releaseMethod · 0.80
_clear_gradientsMethod · 0.80
_update_paramsMethod · 0.80
allreduce_Method · 0.80
cleanupMethod · 0.80
_clear_gradientsMethod · 0.80
allreduce_Method · 0.80

Calls

no outgoing calls

Tested by 15

trainFunction · 0.64
tensor_numpyFunction · 0.64
test_recomputeMethod · 0.64
test_3d_backwardMethod · 0.64
test_backwardMethod · 0.64
test_3d_backwardMethod · 0.64
test_backwardMethod · 0.64
tensor_clear_gradientFunction · 0.64
_cal_input_gradsMethod · 0.64