-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] update optimizer for 2.0 (#26288)
refine Optimizer/Adam/Admax/RMSProp && add Admw * buf fix * update comment * unify arguments place; notest * fix ut, test=develop * bug fix * fix conflicts, test=develop * add examples code * bug fix * fix comments * fix sample code * add sample code for Optimizer * add adamax ut, test=develop * fix rmsprop ut, test=develop * add ut for optimizer.py and adamw.py * remove TestAdamOptimizerBetaVariable * update api && add ut * update doc && fix ut * add ut Co-authored-by: mapingshuo <mps2012@yeah.net>
- Loading branch information
1 parent
e2b82e0
commit eeda90d
Showing
28 changed files
with
3,992 additions
and
76 deletions.
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,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() |
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,81 @@ | ||
# 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_op_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_coverage(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.0, | ||
parameters=linear.parameters(), | ||
apply_decay_param_fun=lambda name: True, | ||
weight_decay=0.01) | ||
assert (adam.__str__() is not None) | ||
|
||
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() |
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
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
Oops, something went wrong.