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 as ms 20import mindspore.nn as nn 21 22 23class Net(nn.Cell): 24 def __init__(self): 25 super(Net, self).__init__() 26 self.pool = nn.PReLU(channel=2, w=-0.25) 27 28 def construct(self, x): 29 out = self.pool(x) 30 return out 31 32 33@pytest.mark.level2 34@pytest.mark.platform_x86_cpu 35@pytest.mark.platform_arm_cpu 36@pytest.mark.platform_x86_gpu_training 37@pytest.mark.platform_arm_ascend_training 38@pytest.mark.platform_x86_ascend_training 39@pytest.mark.env_onecard 40@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) 41def test_prelu_normal(mode): 42 """ 43 Feature: PReLU 44 Description: Verify the result of PReLU 45 Expectation: success 46 """ 47 ms.set_context(mode=mode) 48 x = ms.Tensor([[[0.9192, -0.1487], 49 [-0.3999, -0.6840]], 50 51 [[0.4745, -0.6271], 52 [-0.6547, -0.5856]], 53 54 [[-0.2572, -0.8412], 55 [0.1918, -0.6117]]]) 56 net = Net() 57 out = net(x) 58 expect_out = np.array([[[0.9192, 0.037175], 59 [0.099975, 0.171]], 60 61 [[0.4745, 0.156775], 62 [0.163675, 0.1464]], 63 64 [[0.0643, 0.2103], 65 [0.1918, 0.152925]]]) 66 assert np.allclose(out.asnumpy().astype(np.float16), expect_out.astype(np.float16)) 67