no_grad

Note: This API is only avaliable in [Dynamic Graph] mode

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, 1024], dtype='float32')
        t = fluid.dygraph.base.to_variable(inp)
        linear1 = fluid.Linear(1024, 4, bias_attr=False)
        linear2 = fluid.Linear(4, 4)
        ret = linear1(t)
        dy_ret = linear2(ret)

test_layer()