Skip to content
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 35 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e45dcff
add doc; notest
MRXLT Aug 14, 2020
85b3f92
fix doc; notest
MRXLT Aug 14, 2020
cbcd950
update doc; notest
MRXLT Aug 14, 2020
9661a54
refine optimizer && adam
MRXLT Aug 14, 2020
f542d77
fix conflict
MRXLT Aug 17, 2020
73baac0
refine optimizer; notest
MRXLT Aug 18, 2020
5a55869
add adam
MRXLT Aug 18, 2020
fd34fbd
fix doc
MRXLT Aug 18, 2020
f5e6881
Merge remote-tracking branch 'upstream/develop' into 2.0-op
MRXLT Aug 18, 2020
a715c46
Merge remote-tracking branch 'upstream/develop' into 2.0-op
MRXLT Aug 19, 2020
e67cd86
fix doc && add adamw; notest
MRXLT Aug 19, 2020
da4025d
add error message
MRXLT Aug 19, 2020
f3699cb
bug fix
MRXLT Aug 19, 2020
6f00384
refine rmsprop && adamax
MRXLT Aug 19, 2020
654377d
fix ci
MRXLT Aug 19, 2020
fa7ccb1
buf fix
MRXLT Aug 19, 2020
9aaf899
update comment
MRXLT Aug 19, 2020
b727dad
unify arguments place; notest
MRXLT Aug 20, 2020
9cf4c3b
fix ut, test=develop
mapingshuo Aug 20, 2020
2e8d253
bug fix
MRXLT Aug 20, 2020
00c38fc
fix conflicts, test=develop
mapingshuo Aug 20, 2020
b75ab16
add examples code
MRXLT Aug 20, 2020
84205ce
Merge remote-tracking branch 'origin/2.0-op' into 2.0-op
MRXLT Aug 20, 2020
b6fa771
bug fix
MRXLT Aug 20, 2020
9cd1838
fix comments
MRXLT Aug 20, 2020
95310f5
fix sample code
MRXLT Aug 20, 2020
ce31795
add sample code for Optimizer
MRXLT Aug 20, 2020
0780b9c
add adamax ut, test=develop
mapingshuo Aug 21, 2020
87a7f56
fix rmsprop ut, test=develop
mapingshuo Aug 21, 2020
06f3c73
add ut for optimizer.py and adamw.py
MRXLT Aug 21, 2020
fd67080
Merge branch '2.0-op' of https://github.com/MRXLT/Paddle into 2.0-op
MRXLT Aug 21, 2020
b00b85f
remove TestAdamOptimizerBetaVariable
MRXLT Aug 21, 2020
6cc0fc2
update api && add ut
MRXLT Aug 21, 2020
5d42420
update doc && fix ut
MRXLT Aug 21, 2020
9094782
add ut
MRXLT Aug 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions python/paddle/fluid/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from functools import reduce
from .wrapped_decorator import signature_safe_contextmanager
from .. import compat as cpt
import paddle

__all__ = [
'SGD', 'Momentum', 'Adagrad', 'Adam', 'Adamax', 'Dpsgd', 'DecayedAdagrad',
Expand Down Expand Up @@ -1141,7 +1142,7 @@ def _append_optimize_op(self, block, param_and_grad):

class DGCMomentumOptimizer(Optimizer):
"""
:api_attr: Static Graph
:api_attr: Static Graph

DGC (Deep Gradient Compression) Momentum Optimizer. Original paper is https://arxiv.org/abs/1712.01887

Expand Down Expand Up @@ -3067,7 +3068,7 @@ def _append_optimize_op(self, block, param_and_grad):

class ModelAverage(Optimizer):
"""
:api_attr: Static Graph
:api_attr: Static Graph

The ModelAverage optimizer accumulates specific continuous historical parameters
during training. The accumulated historical range can be controlled by the passed
Expand Down Expand Up @@ -3376,7 +3377,7 @@ def restore(self, executor):

class ExponentialMovingAverage(object):
"""
:api_attr: Static Graph
:api_attr: Static Graph

Compute the moving average of parameters with exponential decay.
Given a parameter :math:`\\theta`, its exponential moving average (EMA)
Expand Down Expand Up @@ -3626,7 +3627,7 @@ def restore(self, executor):

class PipelineOptimizer(object):
"""
:api_attr: Static Graph
:api_attr: Static Graph

Pipeline Optimizer: Make a program to run as pipeline, that is splitting a
program into multiple sections (sub-programs) and each section run on a
Expand Down Expand Up @@ -3690,7 +3691,8 @@ def train_reader():
def __init__(self, optimizer, num_microbatches=1, start_cpu_core_id=0):
if framework.in_dygraph_mode():
raise Exception("In dygraph, don't support PipelineOptimizer.")
if not isinstance(optimizer, Optimizer):
if not isinstance(optimizer, Optimizer) and not isinstance(
optimizer, paddle.optimizer.Optimizer):
raise ValueError("The 'optimizer' parameter for "
"PipelineOptimizer must be an instance of "
"Optimizer, but the given type is {}.".format(
Expand Down Expand Up @@ -4477,7 +4479,7 @@ def minimize(self,

class RecomputeOptimizer(Optimizer):
"""
:api_attr: Static Graph
:api_attr: Static Graph

Recompute Optimizer Wrapper

Expand Down Expand Up @@ -4562,7 +4564,7 @@ def _set_checkpoints(self, checkpoints):

def load(self, stat_dict):
"""
:api_attr: Static Graph
:api_attr: Static Graph

load function is not supported by Recompute Optimizer for now.
:return: None
Expand Down Expand Up @@ -4786,7 +4788,7 @@ def minimize(self,

class LookaheadOptimizer(object):
"""
:api_attr: Static Graph
:api_attr: Static Graph

This implements the Lookahead optimizer of the
paper : https://arxiv.org/abs/1907.08610.
Expand Down
122 changes: 83 additions & 39 deletions python/paddle/fluid/tests/unittests/test_adam_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from paddle.fluid import core
from paddle.fluid.op import Operator
import paddle.fluid as fluid
import paddle


class TestAdamOp1(OpTest):
Expand Down Expand Up @@ -401,46 +402,89 @@ def test_check_output(self):
self.check_output()


class TestAdamOptimizerBetaVariable(unittest.TestCase):
def test_adam_optimizer(self):
def test_with_place(place, shape):
exe = fluid.Executor(place)

train_prog = fluid.Program()
startup = fluid.Program()
with fluid.program_guard(train_prog, startup):
with fluid.unique_name.guard():
data = fluid.data(name="data", shape=shape)
conv = fluid.layers.conv2d(data, 8, 3)
loss = fluid.layers.reduce_mean(conv)

beta1 = fluid.layers.create_global_var(
shape=[1],
value=0.85,
dtype='float32',
persistable=True)
beta2 = fluid.layers.create_global_var(
shape=[1],
value=0.95,
dtype='float32',
persistable=True)
opt = fluid.optimizer.Adam(
learning_rate=1e-5, beta1=beta1, beta2=beta2)
opt.minimize(loss)

exe.run(startup)
data_np = np.random.random(shape).astype('float32')
rets = exe.run(train_prog,
feed={"data": data_np},
fetch_list=[loss])
assert rets[0] is not None

class TestAdamOpV2(unittest.TestCase):
def test_adam_op(self):
place = fluid.CPUPlace()
shape = [2, 3, 8, 8]
places = [fluid.CPUPlace()]
if core.is_compiled_with_cuda():
places.append(fluid.CUDAPlace(0))
for place in places:
test_with_place(place, shape)
exe = fluid.Executor(place)
train_prog = fluid.Program()
startup = fluid.Program()
with fluid.program_guard(train_prog, startup):
with fluid.unique_name.guard():
data = fluid.data(name="data", shape=shape)
conv = fluid.layers.conv2d(data, 8, 3)
loss = fluid.layers.reduce_mean(conv)

beta1 = fluid.layers.create_global_var(
shape=[1], value=0.85, dtype='float32', persistable=True)
beta2 = fluid.layers.create_global_var(
shape=[1], value=0.95, dtype='float32', persistable=True)
betas = [beta1, beta2]
opt = paddle.optimizer.Adam(
learning_rate=1e-5,
beta1=beta1,
beta2=beta2,
weight_decay=0.01,
epsilon=1e-8)
opt.minimize(loss)

exe.run(startup)
data_np = np.random.random(shape).astype('float32')
rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss])
assert rets[0] is not None

def test_adam_op_dygraph(self):
paddle.disable_static()
value = np.arange(26).reshape(2, 13).astype("float32")
a = fluid.dygraph.to_variable(value)
linear = fluid.Linear(13, 5, dtype="float32")

adam = paddle.optimizer.Adam(
learning_rate=0.01, parameters=linear.parameters())
out = linear(a)
out.backward()
adam.step()
adam.clear_gradients()

def test_adam_op_with_state_dict(self):

import paddle
paddle.disable_static()
emb = paddle.nn.Embedding([10, 10])

adam = paddle.optimizer.Adam(0.001, parameters=emb.parameters())
state_dict = adam.state_dict()

adam.set_state_dict(state_dict)

#learning_rate is Decay
from paddle.fluid.regularizer import L2Decay
adam = paddle.optimizer.Adam(
learning_rate=0.01,
weight_decay=L2Decay(0.001),
parameters=emb.parameters())

state_dict = adam.state_dict()
adam.set_state_dict(state_dict)

params = adam.get_opti_var_name_list()
assert (params is not None)

def test_adam_op_with_set_lr(self):
import paddle
paddle.disable_static()
linear = paddle.nn.Linear(10, 10)
adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters())

lr = 0.01
adam.set_lr(lr)
cur_lr = adam.current_step_lr()
assert (lr == cur_lr)

lr_var = paddle.create_global_var(shape=[1], value=lr, dtype='float32')
adam.set_lr(lr_var)
cur_lr = adam.current_step_lr()
assert (np.float32(lr) == cur_lr)


if __name__ == "__main__":
Expand Down
67 changes: 67 additions & 0 deletions python/paddle/fluid/tests/unittests/test_adamax_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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 __future__ import print_function

import unittest
import numpy as np
from op_test import OpTest
import paddle
import paddle.fluid as fluid


class TestAdamaxAPI(unittest.TestCase):
def test_adamax_api_dygraph(self):
paddle.disable_static()
value = np.arange(26).reshape(2, 13).astype("float32")
a = paddle.to_variable(value)
linear = paddle.nn.Linear(13, 5, dtype="float32")
adam = paddle.optimizer.Adamax(
learning_rate=0.01,
parameters=linear.parameters(),
weight_decay=0.01)
out = linear(a)
out.backward()
adam.step()
adam.clear_gradients()

def test_adamax_api(self):
place = fluid.CPUPlace()
shape = [2, 3, 8, 8]
exe = fluid.Executor(place)
train_prog = fluid.Program()
startup = fluid.Program()
with fluid.program_guard(train_prog, startup):
with fluid.unique_name.guard():
data = fluid.data(name="data", shape=shape)
conv = fluid.layers.conv2d(data, 8, 3)
loss = paddle.mean(conv)
beta1 = 0.85
beta2 = 0.95
opt = paddle.optimizer.Adamax(
learning_rate=1e-5,
beta1=beta1,
beta2=beta2,
weight_decay=0.01,
epsilon=1e-8)
opt.minimize(loss)

exe.run(startup)
data_np = np.random.random(shape).astype('float32')
rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss])
assert rets[0] is not None


if __name__ == "__main__":
unittest.main()
69 changes: 69 additions & 0 deletions python/paddle/fluid/tests/unittests/test_adamw_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.

import unittest
import paddle
import numpy as np
import paddle.fluid as fluid


class TestAdamWOp(unittest.TestCase):
def test_adamw_opi_dygraph(self):
paddle.disable_static()
value = np.arange(26).reshape(2, 13).astype("float32")
a = paddle.to_variable(value)
linear = paddle.nn.Linear(13, 5, dtype="float32")
adam = paddle.optimizer.AdamW(
learning_rate=0.01,
parameters=linear.parameters(),
apply_decay_param_fun=lambda name: True,
weight_decay=0.01)
out = linear(a)
out.backward()
adam.step()
adam.clear_gradients()

def test_adamw_op(self):
place = fluid.CPUPlace()
shape = [2, 3, 8, 8]
exe = fluid.Executor(place)
train_prog = fluid.Program()
startup = fluid.Program()
with fluid.program_guard(train_prog, startup):
with fluid.unique_name.guard():
data = fluid.data(name="data", shape=shape)
conv = fluid.layers.conv2d(data, 8, 3)
loss = paddle.mean(conv)

beta1 = fluid.layers.create_global_var(
shape=[1], value=0.85, dtype='float32', persistable=True)
beta2 = fluid.layers.create_global_var(
shape=[1], value=0.95, dtype='float32', persistable=True)
betas = [beta1, beta2]
opt = paddle.optimizer.AdamW(
learning_rate=1e-5,
beta1=beta1,
beta2=beta2,
weight_decay=0.01,
epsilon=1e-8)
opt.minimize(loss)

exe.run(startup)
data_np = np.random.random(shape).astype('float32')
rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss])
assert rets[0] is not None


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_a_sync_optimizer_trainer(self):

strategy = paddle.distributed.fleet.DistributedStrategy()
strategy.a_sync = True
optimizer = paddle.optimizer.SGD(learning_rate=0.01)
optimizer = paddle.fluid.optimizer.SGD(learning_rate=0.01)
optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy)
optimizer.minimize(avg_cost)

Expand Down Expand Up @@ -100,7 +100,7 @@ def test_a_sync_optimizer_pserver(self):

strategy = paddle.distributed.fleet.DistributedStrategy()
strategy.a_sync = True
optimizer = paddle.optimizer.SGD(learning_rate=0.01)
optimizer = paddle.fluid.optimizer.SGD(learning_rate=0.01)
optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy)
optimizer.minimize(avg_cost)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_a_sync_optimizer_trainer(self):
strategy = paddle.distributed.fleet.DistributedStrategy()
strategy.a_sync = True
strategy.a_sync_configs = {"k_steps": 100}
optimizer = paddle.optimizer.SGD(learning_rate=0.01)
optimizer = paddle.fluid.optimizer.SGD(learning_rate=0.01)
optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy)
optimizer.minimize(avg_cost)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_gradient_merge_optimizer(self):

strategy = paddle.distributed.fleet.DistributedStrategy()
strategy.a_sync = False
optimizer = paddle.optimizer.SGD(learning_rate=0.01)
optimizer = paddle.fluid.optimizer.SGD(learning_rate=0.01)
optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy)
optimizer.minimize(avg_cost)

Expand Down
Loading