Skip to content

Conversation

@chenwhql
Copy link
Contributor

@chenwhql chenwhql commented Mar 23, 2021

PR types

Function optimization

PR changes

Others

Describe

Change trainable to stop_gradient in optimizer.

Now users need to optimize a input Tensor but not Parameter, case like:

# 输入
img = paddle.to_tensor(img, dtype='float32', place=paddle.CUDAPlace(0), stop_gradient=False)

#设置为不保存梯度值 自然也无法修改
for param in model.parameters():
    param.stop_gradient = True
    
# 设置优化器
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=img)

But our optimizer now only support to optimize Parameter. Because now our ParamBase.trainable is equal to not ParamBase.stop_gradient, so we can use stop_gradient directly in optimzier to support optimizer Tensor.

the source code are:

    @property
    def trainable(self):
        return not self.stop_gradient

    @trainable.setter
    def trainable(self, trainable):
        if isinstance(trainable, bool):
            self.stop_gradient = not trainable
        else:
            raise ValueError(
                "The type of trainable MUST be bool, but the type is ",
                type(trainable))

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@phlrain phlrain merged commit 68497e7 into PaddlePaddle:develop Mar 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants