no_grad

class paddle.fluid.dygraph.base. no_grad_ [source]
Api_attr

imperative

Create a context which disables dygraph gradient calculation. In this mode, the result of every computation will have stop_gradient set to True.

Also functions as a decorator. (Make sure to use an instance.)

Examples

import numpy as np
import paddle

# use as generator

data = np.array([[2, 3], [4, 5]]).astype('float32')
l0 = paddle.nn.Linear(2, 2)  # l0.weight.gradient() is None
l1 = paddle.nn.Linear(2, 2)
with paddle.no_grad():
    # l1.weight.stop_gradient is False
    tmp = l1.weight * 2  # tmp.stop_gradient is True
x = paddle.to_tensor(data)
y = l0(x) + tmp
o = l1(y)
o.backward()
print(tmp.gradient() is None)  # True
print(l0.weight.gradient() is None)  # False

# use as decorator

@paddle.no_grad()
def test_layer():
    inp = np.ones([3, 1024], dtype='float32')
    t = paddle.to_tensor(inp)
    linear1 = paddle.nn.Linear(1024, 4, bias_attr=False)
    linear2 = paddle.nn.Linear(4, 4)
    ret = linear1(t)
    dy_ret = linear2(ret)

test_layer()