1# Copyright 2023 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# ============================================================================ 15import numpy as np 16import pytest 17 18import mindspore.context as context 19from .optimizer_utils import FakeNet, build_network 20from tests.st.utils import test_utils 21 22 23@pytest.mark.level0 24@pytest.mark.platform_x86_cpu 25@pytest.mark.platform_arm_cpu 26@pytest.mark.platform_x86_gpu_training 27@pytest.mark.platform_arm_ascend_training 28@pytest.mark.platform_x86_ascend_training 29@pytest.mark.env_onecard 30@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 31@test_utils.run_test_with_On 32def test_default_asgd(mode): 33 """ 34 Feature: Test SGD optimizer 35 Description: Test SGD with group weight decay 36 Expectation: Parameters conform to preset values. 37 """ 38 from .optimizer_utils import default_fc1_weight_sgd, default_fc2_weight_sgd 39 context.set_context(mode=mode) 40 config = {'name': 'SGD', "weight_decay": 0.1} 41 _, cells = build_network(config, FakeNet(), is_group=True) 42 assert np.allclose(cells.accum[0].asnumpy(), default_fc1_weight_sgd, atol=1.e-4) 43 assert np.allclose(cells.accum[2].asnumpy(), default_fc2_weight_sgd, atol=1.e-4) 44