86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
# 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 numpy as np
|
|
|
|
import paddle
|
|
|
|
|
|
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_tensor(value)
|
|
linear = paddle.nn.Linear(13, 5)
|
|
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()
|
|
|
|
|
|
class TestAdamaxAPIWeightDecay(unittest.TestCase):
|
|
def test_weight_decay_int(self):
|
|
paddle.disable_static()
|
|
value = np.arange(26).reshape(2, 13).astype("float32")
|
|
a = paddle.to_tensor(value)
|
|
linear = paddle.nn.Linear(13, 5)
|
|
adam = paddle.optimizer.Adamax(
|
|
learning_rate=0.01,
|
|
parameters=linear.parameters(),
|
|
weight_decay=1,
|
|
)
|
|
out = linear(a)
|
|
out.backward()
|
|
adam.step()
|
|
adam.clear_gradients()
|
|
|
|
|
|
class TestAdamaxAPIGroup(TestAdamaxAPI):
|
|
def test_adamax_api_dygraph(self):
|
|
paddle.disable_static()
|
|
value = np.arange(26).reshape(2, 13).astype("float32")
|
|
a = paddle.to_tensor(value)
|
|
linear_1 = paddle.nn.Linear(13, 5)
|
|
linear_2 = paddle.nn.Linear(5, 3)
|
|
# This can be any optimizer supported by dygraph.
|
|
adam = paddle.optimizer.Adamax(
|
|
learning_rate=0.01,
|
|
parameters=[
|
|
{'params': linear_1.parameters()},
|
|
{
|
|
'params': linear_2.parameters(),
|
|
'weight_decay': 0.001,
|
|
'beta1': 0.1,
|
|
'beta2': 0.99,
|
|
},
|
|
],
|
|
weight_decay=0.1,
|
|
)
|
|
out = linear_1(a)
|
|
out = linear_2(out)
|
|
out.backward()
|
|
adam.step()
|
|
adam.clear_gradients()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|