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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. # test softmax
  42. def test_softmax_axis():
  43. layer = nn.Softmax(1)
  44. x = Tensor(np.ones([3, 3]))
  45. assert layer.softmax.axis == (1,)
  46. output = layer.construct(x)
  47. output_np = output.asnumpy()
  48. assert isinstance(output_np[0][0], (np.float32, np.float64))
  49. def test_softmax_axis_none():
  50. layer = nn.Softmax()
  51. x = Tensor(np.ones([3, 2]))
  52. assert layer.softmax.axis == (-1,)
  53. output = layer.construct(x)
  54. output_np = output.asnumpy()
  55. assert isinstance(output_np[0][0], (np.float32, np.float64))