-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update optimizer for 2.0 #26288
Merged
Merged
update optimizer for 2.0 #26288
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
e45dcff
add doc; notest
MRXLT 85b3f92
fix doc; notest
MRXLT cbcd950
update doc; notest
MRXLT 9661a54
refine optimizer && adam
MRXLT f542d77
fix conflict
MRXLT 73baac0
refine optimizer; notest
MRXLT 5a55869
add adam
MRXLT fd34fbd
fix doc
MRXLT f5e6881
Merge remote-tracking branch 'upstream/develop' into 2.0-op
MRXLT a715c46
Merge remote-tracking branch 'upstream/develop' into 2.0-op
MRXLT e67cd86
fix doc && add adamw; notest
MRXLT da4025d
add error message
MRXLT f3699cb
bug fix
MRXLT 6f00384
refine rmsprop && adamax
MRXLT 654377d
fix ci
MRXLT fa7ccb1
buf fix
MRXLT 9aaf899
update comment
MRXLT b727dad
unify arguments place; notest
MRXLT 9cf4c3b
fix ut, test=develop
mapingshuo 2e8d253
bug fix
MRXLT 00c38fc
fix conflicts, test=develop
mapingshuo b75ab16
add examples code
MRXLT 84205ce
Merge remote-tracking branch 'origin/2.0-op' into 2.0-op
MRXLT b6fa771
bug fix
MRXLT 9cd1838
fix comments
MRXLT 95310f5
fix sample code
MRXLT ce31795
add sample code for Optimizer
MRXLT 0780b9c
add adamax ut, test=develop
mapingshuo 87a7f56
fix rmsprop ut, test=develop
mapingshuo 06f3c73
add ut for optimizer.py and adamw.py
MRXLT fd67080
Merge branch '2.0-op' of https://github.com/MRXLT/Paddle into 2.0-op
MRXLT b00b85f
remove TestAdamOptimizerBetaVariable
MRXLT 6cc0fc2
update api && add ut
MRXLT 5d42420
update doc && fix ut
MRXLT 9094782
add ut
MRXLT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .optimizer import Optimizer | ||
from ..fluid import core | ||
from ..fluid import framework | ||
from ..fluid.framework import Variable | ||
|
||
|
||
class Adam(Optimizer): | ||
""" | ||
The Adam optimizer uses an optimization described at the end | ||
of section 2 of `Adam paper <https://arxiv.org/abs/1412.6980>`_ , | ||
it can dynamically adjusts the learning rate of each parameter using | ||
the 1st moment estimates and the 2nd moment estimates of the gradient. | ||
|
||
The parameter ``param_out`` update rule with gradient ``grad``: | ||
|
||
.. math:: | ||
|
||
t & = t + 1 | ||
|
||
moment\_1\_out & = {\\beta}_1 * moment\_1 + (1 - {\\beta}_1) * grad | ||
|
||
moment\_2\_out & = {\\beta}_2 * moment\_2 + (1 - {\\beta}_2) * grad * grad | ||
|
||
learning\_rate & = learning\_rate * \\ | ||
\\frac{\sqrt{1 - {\\beta}_2^t}}{1 - {\\beta}_1^t} | ||
|
||
param\_out & = param - learning\_rate * \\frac{moment\_1}{\sqrt{moment\_2} + \epsilon} | ||
|
||
Related paper: `Adam: A Method for Stochastic Optimization <https://arxiv.org/abs/1412.6980>`_ | ||
|
||
Args: | ||
learning_rate (float|Tensor, optional): The learning rate used to update ``Parameter``. | ||
It can be a float value or a ``Variable`` with a float type. The default value is 0.001. | ||
beta1 (float|Variable, optional): The exponential decay rate for the 1st moment estimates. | ||
It should be a float number or a Variable with shape [1] and data type as float32. | ||
The default value is 0.9. | ||
beta2 (float|Variable, optional): The exponential decay rate for the 2nd moment estimates. | ||
It should be a float number or a Variable with shape [1] and data type as float32. | ||
The default value is 0.999. | ||
epsilon (float, optional): A small float value for numerical stability. | ||
The default value is 1e-08. | ||
parameters (Iterable, optional): Iterable of ``Tensor`` names to update to minimize ``loss``. \ | ||
This parameter is required in dygraph mode. \ | ||
The default value is None in static mode, at this time all parameters will be updated. | ||
weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ | ||
It canbe a float value as coeff of L2 regularization or \ | ||
:ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. | ||
If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ | ||
the regularization setting here in optimizer will be ignored for this parameter. \ | ||
Otherwis, the regularization setting here in optimizer will take effect. \ | ||
Default None, meaning there is no regularization. | ||
grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of | ||
some derived class of ``GradientClipBase`` . There are three cliping strategies | ||
( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , | ||
:ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. | ||
name (str, optional): Normally there is no need for user to set this property. | ||
For more information, please refer to :ref:`api_guide_Name`. | ||
The default value is None. | ||
lazy_mode (bool, optional): The official Adam algorithm has two moving-average accumulators. | ||
The accumulators are updated at every step. Every element of the two moving-average | ||
is updated in both dense mode and sparse mode. If the size of parameter is very large, | ||
then the update may be very slow. The lazy mode only update the element that has | ||
gradient in current mini-batch, so it will be much more faster. But this mode has | ||
different semantics with the original Adam algorithm and may lead to different result. | ||
The default value is False. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
import paddle | ||
import paddle.fluid as fluid | ||
|
||
place = fluid.CPUPlace() | ||
main = fluid.Program() | ||
with fluid.program_guard(main): | ||
x = fluid.data(name='x', shape=[None, 13], dtype='float32') | ||
y = fluid.data(name='y', shape=[None, 1], dtype='float32') | ||
y_predict = fluid.layers.fc(input=x, size=1, act=None) | ||
cost = fluid.layers.square_error_cost(input=y_predict, label=y) | ||
avg_cost = fluid.layers.mean(cost) | ||
|
||
adam_optimizer = paddle.optimizer.Adam(0.01) | ||
adam_optimizer.minimize(avg_cost) | ||
|
||
fetch_list = [avg_cost] | ||
train_reader = paddle.batch( | ||
paddle.dataset.uci_housing.train(), batch_size=1) | ||
feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) | ||
exe = fluid.Executor(place) | ||
exe.run(fluid.default_startup_program()) | ||
for data in train_reader(): | ||
exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) | ||
|
||
.. code-block:: python | ||
|
||
# Adam with beta1/beta2 as Variable | ||
import paddle | ||
import paddle.fluid as fluid | ||
import paddle.fluid.layers.learning_rate_scheduler as lr_scheduler | ||
|
||
place = fluid.CPUPlace() | ||
main = fluid.Program() | ||
with fluid.program_guard(main): | ||
x = fluid.data(name='x', shape=[None, 13], dtype='float32') | ||
y = fluid.data(name='y', shape=[None, 1], dtype='float32') | ||
y_predict = fluid.layers.fc(input=x, size=1, act=None) | ||
cost = fluid.layers.square_error_cost(input=y_predict, label=y) | ||
avg_cost = fluid.layers.mean(cost) | ||
|
||
# define beta decay variable | ||
def get_decayed_betas(beta1_init, beta2_init, decay_steps, decay_rate): | ||
global_step = lr_scheduler._decay_step_counter() | ||
|
||
beta1 = fluid.layers.create_global_var( | ||
shape=[1], | ||
value=float(beta1_init), | ||
dtype='float32', | ||
# set persistable for save checkpoints and resume | ||
persistable=True, | ||
name="beta1") | ||
beta2 = fluid.layers.create_global_var( | ||
shape=[1], | ||
value=float(beta2_init), | ||
dtype='float32', | ||
# set persistable for save checkpoints and resume | ||
persistable=True, | ||
name="beta2") | ||
|
||
div_res = global_step / decay_steps | ||
decayed_beta1 = beta1_init * (decay_rate**div_res) | ||
decayed_beta2 = beta2_init * (decay_rate**div_res) | ||
fluid.layers.assign(decayed_beta1, beta1) | ||
fluid.layers.assign(decayed_beta2, beta2) | ||
|
||
return beta1, beta2 | ||
|
||
beta1, beta2 = get_decayed_betas(0.9, 0.99, 1e5, 0.9) | ||
adam_optimizer = paddle.optimizer.Adam( | ||
learning_rate=0.01, | ||
beta1=beta1, | ||
beta2=beta2) | ||
adam_optimizer.minimize(avg_cost) | ||
|
||
fetch_list = [avg_cost] | ||
train_reader = paddle.batch( | ||
paddle.dataset.uci_housing.train(), batch_size=1) | ||
feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) | ||
exe = fluid.Executor(place) | ||
exe.run(fluid.default_startup_program()) | ||
for data in train_reader(): | ||
exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) | ||
""" | ||
_moment1_acc_str = "moment1" | ||
_moment2_acc_str = "moment2" | ||
_beta1_pow_acc_str = "beta1_pow_acc" | ||
_beta2_pow_acc_str = "beta2_pow_acc" | ||
|
||
def __init__(self, | ||
learning_rate=0.001, | ||
beta1=0.9, | ||
beta2=0.999, | ||
epsilon=1e-8, | ||
parameters=None, | ||
weight_decay=None, | ||
grad_clip=None, | ||
name=None, | ||
lazy_mode=False): | ||
assert learning_rate is not None | ||
assert beta1 is not None | ||
assert beta2 is not None | ||
assert epsilon is not None | ||
super(Adam, self).__init__( | ||
learning_rate=learning_rate, | ||
parameters=parameters, | ||
weight_decay=weight_decay, | ||
grad_clip=grad_clip, | ||
name=name) | ||
self.type = "adam" | ||
self._beta1 = beta1 | ||
self._beta2 = beta2 | ||
self._epsilon = epsilon | ||
self._lazy_mode = lazy_mode | ||
|
||
def _create_accumulators(self, block, parameters): | ||
assert isinstance(block, framework.Block) | ||
|
||
# Create accumulator tensors for first and second moments | ||
for p in parameters: | ||
self._add_accumulator(self._moment1_acc_str, p) | ||
self._add_accumulator(self._moment2_acc_str, p) | ||
self._add_accumulator( | ||
name=self._beta1_pow_acc_str, | ||
param=p, | ||
fill_value=0.9 if isinstance(self._beta1, Variable) \ | ||
else self._beta1, | ||
shape=[1], | ||
type=core.VarDesc.VarType.LOD_TENSOR, device='cpu') | ||
self._add_accumulator( | ||
name=self._beta2_pow_acc_str, | ||
param=p, | ||
fill_value=0.999 if isinstance(self._beta2, Variable) \ | ||
else self._beta2, | ||
shape=[1], | ||
type=core.VarDesc.VarType.LOD_TENSOR, device='cpu') | ||
|
||
def _append_optimize_op(self, block, param_and_grad): | ||
assert isinstance(block, framework.Block) | ||
|
||
moment1 = self._get_accumulator(self._moment1_acc_str, | ||
param_and_grad[0]) | ||
moment2 = self._get_accumulator(self._moment2_acc_str, | ||
param_and_grad[0]) | ||
beta1_pow_acc = self._get_accumulator(self._beta1_pow_acc_str, | ||
param_and_grad[0]) | ||
beta2_pow_acc = self._get_accumulator(self._beta2_pow_acc_str, | ||
param_and_grad[0]) | ||
lr = self._create_param_lr(param_and_grad) | ||
# create the adam optimize op | ||
|
||
if framework.in_dygraph_mode(): | ||
_beta1 = self._beta1 if not isinstance( | ||
self._beta1, Variable) else self._beta1.numpy().item(0) | ||
_beta2 = self._beta2 if not isinstance( | ||
self._beta2, Variable) else self._beta2.numpy().item(0) | ||
_, _, _, _, _ = core.ops.adam( | ||
param_and_grad[0], param_and_grad[1], lr, moment1, moment2, | ||
beta1_pow_acc, beta2_pow_acc, param_and_grad[0], moment1, | ||
moment2, beta1_pow_acc, beta2_pow_acc, 'epsilon', self._epsilon, | ||
'lazy_mode', self._lazy_mode, 'min_row_size_to_use_multithread', | ||
1000, 'beta1', _beta1, 'beta2', _beta2) | ||
|
||
return None | ||
|
||
inputs = { | ||
"Param": [param_and_grad[0]], | ||
"Grad": [param_and_grad[1]], | ||
"LearningRate": [lr], | ||
"Moment1": [moment1], | ||
"Moment2": [moment2], | ||
"Beta1Pow": [beta1_pow_acc], | ||
"Beta2Pow": [beta2_pow_acc] | ||
} | ||
outputs = { | ||
"ParamOut": [param_and_grad[0]], | ||
"Moment1Out": [moment1], | ||
"Moment2Out": [moment2], | ||
"Beta1PowOut": [beta1_pow_acc], | ||
"Beta2PowOut": [beta2_pow_acc], | ||
} | ||
attrs = { | ||
"epsilon": self._epsilon, | ||
"lazy_mode": self._lazy_mode, | ||
"min_row_size_to_use_multithread": 1000 | ||
} | ||
|
||
if isinstance(self._beta1, Variable): | ||
inputs['Beta1Tensor'] = self._beta1 | ||
else: | ||
attrs['beta1'] = self._beta1 | ||
if isinstance(self._beta2, Variable): | ||
inputs['Beta2Tensor'] = self._beta2 | ||
else: | ||
attrs['beta2'] = self._beta2 | ||
|
||
adam_op = block.append_op( | ||
type=self.type, | ||
inputs=inputs, | ||
outputs=outputs, | ||
attrs=attrs, | ||
stop_gradient=True) | ||
|
||
return adam_op |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameters 的位置能上前移动么,毕竟动态图强依赖这个参数
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了与其他优化器保持一致,暂时先不移动这个参数