1# Copyright 2020 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 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops import operations as P 23 24 25class NetSigmoid(nn.Cell): 26 def __init__(self): 27 super(NetSigmoid, self).__init__() 28 self.sigmoid = P.Sigmoid() 29 30 def construct(self, x): 31 return self.sigmoid(x) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_gpu_training 36@pytest.mark.env_onecard 37def test_sigmoid(): 38 x = Tensor(np.array([[[[-1, 1, 10], 39 [1, -1, 1], 40 [10, 1, -1]]]]).astype(np.float32)) 41 expect = np.array([[[[0.268941, 0.731059, 0.999955], 42 [0.731059, 0.268941, 0.731059], 43 [0.999955, 0.731059, 0.268941]]]]).astype(np.float32) 44 45 error = np.ones(shape=[1, 1, 3, 3]) * 1.0e-6 46 47 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 48 sigmoid = NetSigmoid() 49 output = sigmoid(x) 50 diff = output.asnumpy() - expect 51 assert np.all(abs(diff) < error) 52 53 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 54 sigmoid = NetSigmoid() 55 output = sigmoid(x) 56 diff = output.asnumpy() - expect 57 assert np.all(abs(diff) < error) 58