diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 317cae815f48a0acc58798722d347a133f5c200e..2c9e9a12b058bcd72ae0548f80ba1cbde681bc39 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -2858,6 +2858,12 @@ class Block(object): param = ParamBase(*args, **kwargs) else: param = Parameter(global_block, *args, **kwargs) + # NOTE: Why only set stop_gradient=False in static mode + # Because in dygraph mode, the `stop_gradient` and `trainable` + # are related, and `trainable` default vallue is `True` or + # it is specified by users, there is no need to set + # `stop_gradient` for ParamBase here. + param.stop_gradient = False if 'initializer' in kwargs: def _is_inited_by(block, var): @@ -2884,7 +2890,6 @@ class Block(object): pass else: initializer(param, self) - param.stop_gradient = False return param def append_op(self, *args, **kwargs): diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 3908d65229afe41c1c0421c4c70a389dba8c2916..8ae5264381e822062b2237507f94efc9b9daf15a 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3683,5 +3683,24 @@ class TestMetricsDetectionMap(unittest.TestCase): print(str(program)) +class ExampleNet(paddle.nn.Layer): + def __init__(self): + super(ExampleNet, self).__init__() + self.weight = self.create_parameter( + shape=[1, 1], attr=paddle.ParamAttr(trainable=False)) + + def forward(self): + # only for test parameter trainable attr + pass + + +class TestLayerParameterTrainableSet(unittest.TestCase): + def test_layer_parameter_set(self): + with fluid.dygraph.guard(): + net = ExampleNet() + self.assertFalse(net.weight.trainable) + + if __name__ == '__main__': + paddle.enable_static() unittest.main()