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

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