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

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

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.