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

Method stop_gradient

python/paddle/base/framework.py:2301–2331  ·  view source on GitHub ↗

Indicating if we stop gradient from current Variable **Notes: This Property has default value as** ``True`` **in** Dygraph **mode, while Parameter's default value is False. However, in Static Graph Mode all Variable's default stop_gradient value is** ``False`` Examples:

(self)

Source from the content-addressed store, hash-verified

2299
2300 @property
2301 def stop_gradient(self):
2302 """
2303 Indicating if we stop gradient from current Variable
2304
2305 **Notes: This Property has default value as** ``True`` **in** Dygraph **mode, while Parameter's default value is False. However, in Static Graph Mode all Variable's default stop_gradient value is** ``False``
2306
2307 Examples:
2308 .. code-block:: pycon
2309
2310 >>> import paddle
2311 >>> import paddle.base as base
2312 >>> import numpy as np
2313
2314 >>> with base.dygraph.guard():
2315 ... value0 = np.arange(26).reshape(2, 13).astype("float32")
2316 ... value1 = np.arange(6).reshape(2, 3).astype("float32")
2317 ... value2 = np.arange(10).reshape(2, 5).astype("float32")
2318 ... linear = paddle.nn.Linear(13, 5)
2319 ... linear2 = paddle.nn.Linear(3, 3)
2320 ... a = paddle.to_tensor(value0)
2321 ... b = paddle.to_tensor(value1)
2322 ... c = paddle.to_tensor(value2)
2323 ... out1 = linear(a)
2324 ... out2 = linear2(b)
2325 ... out1.stop_gradient = True
2326 ... out = paddle.concat(x=[out1, out2, c], axis=1)
2327 ... out.backward()
2328 ... assert linear.weight.gradient() is None
2329 ... assert out1.gradient() is None
2330 """
2331 return self.desc.stop_gradient()
2332
2333 @stop_gradient.setter
2334 def stop_gradient(self, s):

Callers 3

_copy_kwargsMethod · 0.80
_sync_with_cppMethod · 0.80
StopGradientFunction · 0.80

Calls 1

set_stop_gradientMethod · 0.80

Tested by

no test coverage detected