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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore.common.api import _executor
  20. from ..ut_filter import non_graph_engine
  21. class SoftmaxNet(nn.Cell):
  22. def __init__(self, dim):
  23. super(SoftmaxNet, self).__init__()
  24. self.softmax = nn.Softmax(dim)
  25. def construct(self, x):
  26. return self.softmax(x)
  27. @non_graph_engine
  28. def test_compile():
  29. net = SoftmaxNet(0)
  30. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  31. net(input_tensor)
  32. @non_graph_engine
  33. def test_compile_axis():
  34. net = SoftmaxNet(-1)
  35. prob = 355
  36. input_data = np.random.randn(4, 16, 1, 1).astype(np.float32) * prob
  37. input_tensor = Tensor(input_data)
  38. net(input_tensor)
  39. class LogSoftmaxNet(nn.Cell):
  40. def __init__(self, dim):
  41. super(LogSoftmaxNet, self).__init__()
  42. self.logsoftmax = nn.LogSoftmax(dim)
  43. def construct(self, x):
  44. return self.logsoftmax(x)
  45. @non_graph_engine
  46. def test_compile_logsoftmax():
  47. net = LogSoftmaxNet(0)
  48. input_tensor = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]))
  49. net(input_tensor)
  50. class Net1(nn.Cell):
  51. def __init__(self):
  52. super(Net1, self).__init__()
  53. self.relu = nn.ReLU()
  54. def construct(self, x):
  55. return self.relu(x)
  56. def test_compile_relu():
  57. net = Net1()
  58. input_data = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  59. _executor.compile(net, input_data)
  60. class Net_gelu(nn.Cell):
  61. def __init__(self):
  62. super(Net_gelu, self).__init__()
  63. self.gelu = nn.GELU()
  64. def construct(self, x):
  65. return self.gelu(x)
  66. def test_compile_gelu():
  67. net = Net_gelu()
  68. input_data = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]], dtype=np.float32))
  69. _executor.compile(net, input_data)
  70. class NetLeakyReLU(nn.Cell):
  71. def __init__(self, alpha):
  72. super(NetLeakyReLU, self).__init__()
  73. self.leaky_relu = nn.LeakyReLU(alpha)
  74. def construct(self, x):
  75. return self.leaky_relu(x)
  76. def test_compile_leaky_relu():
  77. net = NetLeakyReLU(alpha=0.1)
  78. input_data = Tensor(np.array([[1.6, 0, 0.6], [6, 0, -6]], dtype=np.float32))
  79. _executor.compile(net, input_data)