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 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.context as context
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. from mindspore.ops import operations as P
  22. from mindspore.common.api import _executor
  23. from ..ut_filter import non_graph_engine
  24. def test_dense_none():
  25. with pytest.raises(TypeError):
  26. nn.Dense(3, 2, None, None)
  27. @non_graph_engine
  28. def test_dense_str_activation():
  29. dense = nn.Dense(1, 1, activation='relu')
  30. assert isinstance(dense.activation, nn.ReLU)
  31. input_data = Tensor(np.random.randint(0, 255, [1, 1]).astype(np.float32))
  32. dense(input_data)
  33. @non_graph_engine
  34. def test_dense_nn_activation_():
  35. dense = nn.Dense(1, 1, activation=nn.ReLU())
  36. assert isinstance(dense.activation, nn.ReLU)
  37. input_data = Tensor(np.random.randint(0, 255, [1, 1]).astype(np.float32))
  38. dense(input_data)
  39. @non_graph_engine
  40. def test_dense_ops_activation_():
  41. dense = nn.Dense(1, 1, activation=P.ReLU())
  42. assert isinstance(dense.activation, P.ReLU)
  43. input_data = Tensor(np.random.randint(0, 255, [1, 1]).astype(np.float32))
  44. dense(input_data)
  45. def test_dense_weight_error():
  46. dim_error = Tensor(np.array([[[0.1], [0.3], [0.6]], [[0.4], [0.5], [0.2]]]))
  47. with pytest.raises(ValueError):
  48. nn.Dense(3, 2, dim_error)
  49. shape_error = Tensor(np.array([[0.1, 0.3, 0.6], [0.4, 0.5, 0.2]]))
  50. with pytest.raises(ValueError):
  51. nn.Dense(2, 2, shape_error)
  52. with pytest.raises(ValueError):
  53. nn.Dense(3, 3, shape_error)
  54. def test_dense_bias_error():
  55. dim_error = Tensor(np.array([[0.5, 0.3]]))
  56. with pytest.raises(ValueError):
  57. nn.Dense(3, 2, bias_init=dim_error)
  58. shape_error = Tensor(np.array([0.5, 0.3, 0.4]))
  59. with pytest.raises(ValueError):
  60. nn.Dense(3, 2, bias_init=shape_error)
  61. def test_dense_channels_error():
  62. with pytest.raises(ValueError):
  63. nn.Dense(3, 0)
  64. with pytest.raises(ValueError):
  65. nn.Dense(-1, 2)
  66. class Net(nn.Cell):
  67. """ Net definition """
  68. def __init__(self,
  69. input_channels,
  70. output_channels,
  71. weight='normal',
  72. bias='zeros',
  73. has_bias=True,
  74. activation=None):
  75. super(Net, self).__init__()
  76. self.dense = nn.Dense(input_channels,
  77. output_channels,
  78. weight,
  79. bias,
  80. has_bias,
  81. activation=activation)
  82. def construct(self, input_x):
  83. return self.dense(input_x)
  84. def test_compile():
  85. """ test_compile """
  86. # has bias
  87. weight = Tensor(np.random.randint(0, 255, [8, 64]).astype(np.float32))
  88. bias = Tensor(np.random.randint(0, 255, [8]).astype(np.float32))
  89. net = Net(64, 8, weight=weight, bias=bias)
  90. input_data = Tensor(np.random.randint(0, 255, [128, 64]).astype(np.float32))
  91. _executor.compile(net, input_data)
  92. # training
  93. net_train = Net(64, 8, weight=weight, bias=bias)
  94. net_train.set_train()
  95. _executor.compile(net_train, input_data)
  96. def test_compile_2():
  97. """ test_compile_2 """
  98. # no bias
  99. weight = Tensor(np.random.randint(0, 255, [8, 64]).astype(np.float32))
  100. net = Net(64, 8, weight=weight, has_bias=False)
  101. input_data = Tensor(np.random.randint(0, 255, [128, 64]).astype(np.float32))
  102. _executor.compile(net, input_data)
  103. # training
  104. net_train = Net(64, 8, weight=weight, has_bias=False)
  105. net_train.set_train()
  106. _executor.compile(net_train, input_data)
  107. def test_compile_3():
  108. """ test_compile_3 """
  109. # test for Graph mode
  110. # has bias
  111. context.set_context(mode=context.GRAPH_MODE)
  112. net = Net(128, 10)
  113. input_data = Tensor(np.random.randint(0, 255, [128, 128]).astype(np.float32))
  114. _executor.compile(net, input_data)
  115. # training
  116. net_train = Net(128, 10)
  117. net_train.set_train()
  118. _executor.compile(net_train, input_data)
  119. def test_compile_4():
  120. """ test_compile_4 """
  121. # test for Graph mode
  122. # no bias
  123. context.set_context(mode=context.GRAPH_MODE)
  124. net = Net(128, 10, has_bias=False)
  125. input_data = Tensor(np.random.randint(0, 255, [128, 128]).astype(np.float32))
  126. _executor.compile(net, input_data)
  127. # training
  128. net_train = Net(128, 10, has_bias=False)
  129. net_train.set_train()
  130. _executor.compile(net_train, input_data)