**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)
| 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 |
no outgoing calls