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 nn.Dense """ 16import numpy as np 17import pytest 18 19import mindspore.nn as nn 20from mindspore import Tensor 21 22 23# pylint: disable=E1123 24 25 26def test_dense_defaultbias_noactivation(): 27 weight = Tensor(np.array([[0.1, 0.3, 0.4], [0.1, 0.3, 0.4]], dtype=np.float32)) 28 dense = nn.Dense(3, 2, weight) 29 assert dense.activation is None 30 31 input_data = Tensor(np.random.randint(0, 255, [1, 3]).astype(np.float32)) 32 output = dense(input_data) 33 output_np = output.asnumpy() 34 assert isinstance(output_np[0][0], (np.float32, np.float64)) 35 36 37def test_dense_defaultweight(): 38 bias = Tensor(np.array([0.5, 0.3], dtype=np.float32)) 39 dense = nn.Dense(3, 2, bias_init=bias) 40 # batch_size 1 && 3-channel RGB 41 input_data = Tensor(np.random.randint(0, 255, [1, 3]).astype(np.float32)) 42 output = dense(input_data) 43 output_np = output.asnumpy() 44 assert isinstance(output_np[0][0], (np.float32, np.float64)) 45 46 47def test_dense_bias(): 48 weight = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]], dtype=np.float32)) 49 bias = Tensor(np.array([0.5, 0.3], dtype=np.float32)) 50 dense = nn.Dense(3, 2, weight, bias) 51 52 input_data = Tensor(np.random.randint(0, 255, [2, 3]).astype(np.float32)) 53 output = dense(input_data) 54 output_np = output.asnumpy() 55 assert isinstance(output_np[0][0], (np.float32, np.float64)) 56 57 58def test_dense_nobias(): 59 weight = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]], dtype=np.float32)) 60 dense = nn.Dense(3, 2, weight, has_bias=False) 61 62 input_data = Tensor(np.random.randint(0, 255, [2, 3]).astype(np.float32)) 63 output = dense(input_data) 64 output_np = output.asnumpy() 65 assert isinstance(output_np[0][0], (np.float32, np.float64)) 66 67 68def test_dense_none(): 69 with pytest.raises(TypeError): 70 nn.Dense(3, 2, None, None) 71 72 73def test_dense_str_activation(): 74 dense = nn.Dense(1, 1, activation='relu') 75 assert isinstance(dense.activation, nn.ReLU) 76 77 input_data = Tensor(np.random.randint(0, 255, [1, 1]).astype(np.float32)) 78 output = dense(input_data) 79 output_np = output.asnumpy() 80 assert isinstance(output_np[0][0], np.float32) 81 82 83def test_dense_weight_error(): 84 dim_error = Tensor(np.array([[[0.1], [0.3], [0.6]], [[0.4], [0.5], [0.2]]])) 85 with pytest.raises(ValueError): 86 nn.Dense(3, 2, dim_error) 87 88 shape_error = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]])) 89 with pytest.raises(ValueError): 90 nn.Dense(2, 2, shape_error) 91 with pytest.raises(ValueError): 92 nn.Dense(3, 3, shape_error) 93 94 95def test_dense_bias_error(): 96 dim_error = Tensor(np.array([[0.5, 0.3]])) 97 with pytest.raises(ValueError): 98 nn.Dense(3, 2, bias_init=dim_error) 99 100 shape_error = Tensor(np.array([0.5, 0.3, 0.4])) 101 with pytest.raises(ValueError): 102 nn.Dense(3, 2, bias_init=shape_error) 103 104 105def test_dense_dtype_error(): 106 with pytest.raises(TypeError): 107 nn.Dense(3, 2, dtype=3) 108 109 110def test_dense_channels_error(): 111 with pytest.raises(ValueError): 112 nn.Dense(3, 0) 113 114 with pytest.raises(ValueError): 115 nn.Dense(-1, 2) 116