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# ============================================================================ 15import numpy as np 16import pytest 17 18import mindspore as ms 19from mindspore import Tensor, nn, ops 20 21 22class Net(nn.Cell): 23 def construct(self, x, beta=1, threshold=20): 24 return ops.softplus(x, beta, threshold) 25 26 27@pytest.mark.level2 28@pytest.mark.platform_x86_cpu 29@pytest.mark.platform_arm_cpu 30@pytest.mark.platform_x86_gpu_training 31@pytest.mark.platform_arm_ascend_training 32@pytest.mark.platform_x86_ascend_training 33@pytest.mark.env_onecard 34@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) 35def test_ops_softplus(mode): 36 """ 37 Feature: test ops.softplus 38 Description: verify the result of softplus 39 Expectation: success 40 """ 41 ms.set_context(mode=mode) 42 softplus = Net() 43 x = Tensor(np.array([0.1, 0.2, 30, 25]), ms.float32) 44 45 output = softplus(x) 46 expect_output = np.array([0.7443967, 0.7981389, 30.0000000, 25.0000000]) 47 assert np.allclose(output.asnumpy(), expect_output) 48 49 output = softplus(x, 0.3, 100) 50 expect_output = np.array([2.3608654, 2.4119902, 30.0004082, 25.0018444]) 51 assert np.allclose(output.asnumpy(), expect_output) 52