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_activation.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Activations """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. # test activation
  21. def test_relu_default():
  22. relu = nn.ReLU()
  23. input_data = Tensor(np.random.rand(1, 3, 4, 4).astype(np.float32) - 0.5)
  24. output = relu.construct(input_data)
  25. output_np = output.asnumpy()
  26. assert isinstance(output_np[0][0][0][0], (np.float32, np.float64))
  27. def test_activation_str():
  28. relu = nn.get_activation('relu')
  29. input_data = Tensor(np.random.rand(1, 3, 4, 4).astype(np.float32) - 0.5)
  30. output = relu.construct(input_data)
  31. output_np = output.asnumpy()
  32. assert isinstance(output_np[0][0][0][0], (np.float32, np.float64))
  33. def test_activation_param():
  34. relu = nn.get_activation('relu')
  35. input_data = Tensor(np.random.rand(1, 3, 4, 4).astype(np.float32) - 0.5)
  36. output = relu.construct(input_data)
  37. output_np = output.asnumpy()
  38. assert isinstance(output_np[0][0][0][0], (np.float32, np.float64))
  39. def test_activation_empty():
  40. assert nn.get_activation('') is None
  41. def test_activation_invalid():
  42. with pytest.raises(KeyError):
  43. nn.get_activation('relu6')
  44. # test softmax
  45. def test_softmax_axis():
  46. layer = nn.Softmax(1)
  47. x = Tensor(np.ones([3, 3]))
  48. assert layer.softmax.axis == (1,)
  49. output = layer.construct(x)
  50. output_np = output.asnumpy()
  51. assert isinstance(output_np[0][0], (np.float32, np.float64))
  52. def test_softmax_axis_none():
  53. layer = nn.Softmax()
  54. x = Tensor(np.ones([3, 2]))
  55. assert layer.softmax.axis == (-1,)
  56. output = layer.construct(x)
  57. output_np = output.asnumpy()
  58. assert isinstance(output_np[0][0], (np.float32, np.float64))