You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_dense.py 3.7 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. # pylint: disable=E1123
  21. def test_dense_defaultbias_noactivation():
  22. weight = Tensor(np.array([[0.1, 0.3, 0.4], [0.1, 0.3, 0.4]], dtype=np.float32))
  23. dense = nn.Dense(3, 2, weight)
  24. assert dense.activation is None
  25. input_data = Tensor(np.random.randint(0, 255, [1, 3]).astype(np.float32))
  26. output = dense(input_data)
  27. output_np = output.asnumpy()
  28. assert isinstance(output_np[0][0], (np.float32, np.float64))
  29. def test_dense_defaultweight():
  30. bias = Tensor(np.array([0.5, 0.3], dtype=np.float32))
  31. dense = nn.Dense(3, 2, bias_init=bias)
  32. # batch_size 1 && 3-channel RGB
  33. input_data = Tensor(np.random.randint(0, 255, [1, 3]).astype(np.float32))
  34. output = dense(input_data)
  35. output_np = output.asnumpy()
  36. assert isinstance(output_np[0][0], (np.float32, np.float64))
  37. def test_dense_bias():
  38. weight = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]], dtype=np.float32))
  39. bias = Tensor(np.array([0.5, 0.3], dtype=np.float32))
  40. dense = nn.Dense(3, 2, weight, bias)
  41. input_data = Tensor(np.random.randint(0, 255, [2, 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. def test_dense_nobias():
  46. weight = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]], dtype=np.float32))
  47. dense = nn.Dense(3, 2, weight, has_bias=False)
  48. input_data = Tensor(np.random.randint(0, 255, [2, 3]).astype(np.float32))
  49. output = dense(input_data)
  50. output_np = output.asnumpy()
  51. assert isinstance(output_np[0][0], (np.float32, np.float64))
  52. def test_dense_none():
  53. with pytest.raises(TypeError):
  54. nn.Dense(3, 2, None, None)
  55. def test_dense_str_activation():
  56. dense = nn.Dense(1, 1, activation='relu')
  57. assert isinstance(dense.activation, nn.ReLU)
  58. input_data = Tensor(np.random.randint(0, 255, [1, 1]).astype(np.float32))
  59. output = dense(input_data)
  60. output_np = output.asnumpy()
  61. assert isinstance(output_np[0][0], np.float32)
  62. def test_dense_weight_error():
  63. dim_error = Tensor(np.array([[[0.1], [0.3], [0.6]], [[0.4], [0.5], [0.2]]]))
  64. with pytest.raises(ValueError):
  65. nn.Dense(3, 2, dim_error)
  66. shape_error = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]]))
  67. with pytest.raises(ValueError):
  68. nn.Dense(2, 2, shape_error)
  69. with pytest.raises(ValueError):
  70. nn.Dense(3, 3, shape_error)
  71. def test_dense_bias_error():
  72. dim_error = Tensor(np.array([[0.5, 0.3]]))
  73. with pytest.raises(ValueError):
  74. nn.Dense(3, 2, bias_init=dim_error)
  75. shape_error = Tensor(np.array([0.5, 0.3, 0.4]))
  76. with pytest.raises(ValueError):
  77. nn.Dense(3, 2, bias_init=shape_error)
  78. def test_dense_dtype_error():
  79. with pytest.raises(TypeError):
  80. nn.Dense(3, 2, dtype=3)
  81. def test_dense_channels_error():
  82. with pytest.raises(ValueError):
  83. nn.Dense(3, 0)
  84. with pytest.raises(ValueError):
  85. nn.Dense(-1, 2)