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""" test Activations """ 16import numpy as np 17 18import mindspore.nn as nn 19from mindspore import Tensor 20 21 22# test activation 23def test_relu_default(): 24 relu = nn.ReLU() 25 input_data = Tensor(np.random.rand(1, 3, 4, 4).astype(np.float32) - 0.5) 26 output = relu.construct(input_data) 27 output_np = output.asnumpy() 28 assert isinstance(output_np[0][0][0][0], (np.float32, np.float64)) 29 30 31def test_activation_str(): 32 relu = nn.get_activation('relu') 33 34 input_data = Tensor(np.random.rand(1, 3, 4, 4).astype(np.float32) - 0.5) 35 output = relu.construct(input_data) 36 output_np = output.asnumpy() 37 assert isinstance(output_np[0][0][0][0], (np.float32, np.float64)) 38 39 40def test_activation_param(): 41 relu = nn.get_activation('relu') 42 43 input_data = Tensor(np.random.rand(1, 3, 4, 4).astype(np.float32) - 0.5) 44 output = relu.construct(input_data) 45 output_np = output.asnumpy() 46 assert isinstance(output_np[0][0][0][0], (np.float32, np.float64)) 47 48 49# test softmax 50def test_softmax_axis(): 51 layer = nn.Softmax(1) 52 x = Tensor(np.ones([3, 3]).astype(np.float32)) 53 assert layer.softmax.axis == (1,) 54 output = layer.construct(x) 55 output_np = output.asnumpy() 56 assert isinstance(output_np[0][0], (np.float32, np.float64)) 57 58 59def test_softmax_axis_none(): 60 layer = nn.Softmax() 61 x = Tensor(np.ones([3, 2]).astype(np.float32)) 62 assert layer.softmax.axis == (-1,) 63 output = layer.construct(x) 64 output_np = output.asnumpy() 65 assert isinstance(output_np[0][0], (np.float32, np.float64)) 66