• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20from mindspore import nn, Tensor
21from .optimizer_utils import build_network, default_fc1_weight_adamax, default_fc1_bias_adamax, \
22    no_default_fc1_weight_adamax, no_default_fc1_bias_adamax, default_group_fc1_weight_adamax, \
23    default_group_fc1_bias_adamax
24
25w1 = np.array([[0.03909272, 0.08893055, -0.259909, -0.459185,
26                -0.0195536, 0.12977135, -0.62942827, -0.53132117],
27               [0.1542052, 0.6513571, -0.06453168, 0.44788414,
28                -0.3775454, 0.6520292, 0.444174, -0.59306043],
29               [0.2712369, 0.20890862, 0.6859066, 0.6629662,
30                0.4724893, -0.34384444, -0.16007674, 0.21797538],
31               [-0.3865972, 0.26727962, 0.23178828, -0.24629539,
32                -0.68038213, -0.31262863, 0.10493469, -0.28973007]]).astype("float32")
33
34b1 = np.array([0., 0., 0., 0.]).astype("float32")
35
36w2 = np.array([[-0.6079024, -1.005364, 0.59004724, 0.7289244]]).astype("float32")
37
38b2 = np.array([0.]).astype("float32")
39
40
41class Net(nn.Cell):
42    """
43    build a 2-layers net to test adamax optimizer
44    """
45
46    def __init__(self):
47        super(Net, self).__init__()
48        self.fc1 = nn.Dense(8, 4, weight_init=Tensor(w1), bias_init=Tensor(b1))
49        self.fc2 = nn.Dense(4, 1, weight_init=Tensor(w2), bias_init=Tensor(b2))
50        self.relu = nn.ReLU()
51
52    def construct(self, x):
53        x = self.relu(self.fc1(x))
54        return self.fc2(x)
55
56
57@pytest.mark.level0
58@pytest.mark.platform_x86_cpu
59@pytest.mark.platform_arm_cpu
60@pytest.mark.platform_x86_gpu_training
61@pytest.mark.platform_arm_ascend_training
62@pytest.mark.platform_x86_ascend_training
63@pytest.mark.env_onecard
64@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
65def test_default_adamax(mode):
66    """
67    Feature: Test adamax optimizer
68    Description: Test adamax with default parameter
69    Expectation: Loss values and parameters conform to preset values.
70    """
71    context.set_context(mode=mode)
72    config = {'name': 'adamax', 'lr': 0.001, "beta1": 0.9, "beta2": 0.999, "eps": 1e-07,
73              'weight_decay': 0.0}
74    _, cells = build_network(config, net=Net(), loss_fn=nn.L1Loss(reduction='mean'))
75    assert np.allclose(cells.moment1[0].asnumpy(), default_fc1_weight_adamax, atol=1.e-3)
76    assert np.allclose(cells.moment1[1].asnumpy(), default_fc1_bias_adamax, atol=1.e-3)
77
78
79@pytest.mark.level0
80@pytest.mark.platform_x86_cpu
81@pytest.mark.platform_arm_cpu
82@pytest.mark.platform_x86_gpu_training
83@pytest.mark.platform_arm_ascend_training
84@pytest.mark.platform_x86_ascend_training
85@pytest.mark.env_onecard
86@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
87def test_no_default_adamax(mode):
88    """
89    Feature: Test adamax optimizer
90    Description: Test adamax with another set of parameter
91    Expectation: Loss values and parameters conform to preset values.
92    """
93    context.set_context(mode=mode)
94    config = {'name': 'adamax', 'lr': 0.01, "beta1": 0.9, "beta2": 0.98, "eps": 1e-06,
95              'weight_decay': 0.0}
96    _, cells = build_network(config, net=Net(), loss_fn=nn.L1Loss(reduction='mean'))
97    assert np.allclose(cells.moment1[0].asnumpy(), no_default_fc1_weight_adamax, atol=1.e-3)
98    assert np.allclose(cells.moment1[1].asnumpy(), no_default_fc1_bias_adamax, atol=1.e-3)
99
100
101@pytest.mark.level0
102@pytest.mark.platform_x86_cpu
103@pytest.mark.platform_arm_cpu
104@pytest.mark.platform_x86_gpu_training
105@pytest.mark.platform_arm_ascend_training
106@pytest.mark.platform_x86_ascend_training
107@pytest.mark.env_onecard
108@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
109def test_default_adamax_group(mode):
110    """
111    Feature: Test adamax optimizer
112    Description: Test adamax with parameter grouping
113    Expectation: Loss values and parameters conform to preset values.
114    """
115    context.set_context(mode=mode)
116    config = {'name': 'adamax', 'lr': 0.002, "beta1": 0.9, "beta2": 0.999, "eps": 1e-08,
117              'weight_decay': 0.0}
118    _, cells = build_network(config, is_group=True, net=Net(), loss_fn=nn.L1Loss(reduction='mean'))
119    assert np.allclose(cells.moment1[0].asnumpy(), default_group_fc1_weight_adamax, atol=1.e-3)
120    assert np.allclose(cells.moment1[1].asnumpy(), default_group_fc1_bias_adamax, atol=1.e-3)
121