no_grad

paddle.fluid.dygraph.no_grad(func)

This Decorator will avoid the func being decorated creating backward network in dygraph mode

Parameter:
  • func (python func): the func don’t need grad

Examples

import numpy as np
import paddle.fluid as fluid

@fluid.dygraph.no_grad
def test_layer():
    with fluid.dygraph.guard():
        inp = np.ones([3, 32, 32], dtype='float32')
        t = fluid.dygraph.base.to_variable(inp)
        fc1 = fluid.FC('fc1', size=4, bias_attr=False, num_flatten_dims=1)
        fc2 = fluid.FC('fc2', size=4)
        ret = fc1(t)
        dy_ret = fc2(ret)

test_layer()