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_logsoftmax_op.py 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright 2021 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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import composite as C
  21. from mindspore.ops import operations as P
  22. @pytest.mark.level0
  23. @pytest.mark.platform_x86_cpu
  24. @pytest.mark.env_onecard
  25. def test_logsoftmax():
  26. x = np.array([[-0.08082921, -0.13706027, -0.4711177, -0.05606057],
  27. [-0.46082982, 1.1761844, -1.016654, -1.743829],
  28. [-1.5062045, 0.6910976, 0.4839723, 1.1502692]]).astype(np.float32)
  29. expect = np.array([[-1.2939762, -1.3502073, -1.6842647, -1.2692076],
  30. [-1.9445671, -0.3075528, -2.5003912, -3.2275662],
  31. [-3.452001, -1.2546989, -1.4618242, -0.79552734]]).astype(np.float32)
  32. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  33. logSoftmax = P.LogSoftmax()
  34. output = logSoftmax(Tensor(x))
  35. diff = output.asnumpy() - expect
  36. err = np.ones(shape=expect.shape) * 1.0e-5
  37. assert np.all(diff < err)
  38. class LogSoftmax(nn.Cell):
  39. def __init__(self, axis=-1):
  40. super(LogSoftmax, self).__init__()
  41. self.logsoftmax = P.LogSoftmax(axis)
  42. def construct(self, x):
  43. return self.logsoftmax(x)
  44. class Grad(nn.Cell):
  45. def __init__(self, network):
  46. super(Grad, self).__init__()
  47. self.grad = C.GradOperation(get_all=True, sens_param=True)
  48. self.network = network
  49. def construct(self, input_data, sens):
  50. gout = self.grad(self.network)(input_data, sens)
  51. return gout
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_cpu
  54. @pytest.mark.env_onecard
  55. def test_logsoftmaxgrad():
  56. x = np.array([[-0.47705367, 0.48267725, -1.0453935, 1.574488, 0.20362134, 0.4435456, -0.23984082, -0.43684655,
  57. -0.7725506, 1.4481013],
  58. [1.1012247, 1.7069651, 0.55062026, 0.3361901, -1.1082426, -0.5001939, -0.3255393, -0.7972024,
  59. -0.27965206, -0.702805],
  60. [0.19450496, 0.87596166, 0.6467245, -1.044987, 0.5248943, -2.6166635, 1.6719198, 0.06600758,
  61. -0.4099178, 1.1861311],
  62. [1.1305193, -1.97308, 2.1047623, -1.5105937, 0.93052036, 1.2467804, 0.5310002, 0.7084912, -1.3681422,
  63. -0.9686862],
  64. [1.871408, 0.14219497, -0.41050452, -0.749807, 1.4900619, -1.8172716, -0.73839617, 0.17565694,
  65. -0.4553867, -1.5423119]]).astype(np.float32)
  66. dy = np.array([[1.516363, -0.15196544, 0.598733, 0.64357865, 0.16265012, -1.3521105, 0.22621834, 0.7168259,
  67. -0.6709239, 0.79757756],
  68. [-0.32457778, 1.2831115, 1.1211495, -0.02665559, 1.9170904, -1.3397789, 1.4124829, -1.4298155,
  69. 0.758519, -0.25322974],
  70. [-0.24226122, -1.2555921, 0.6492511, -0.34847677, 0.19916506, 0.628554, -0.19658111, 0.44939864,
  71. -0.11677749, -1.2131723],
  72. [0.24267715, 0.28106326, 1.1075432, -0.29006946, 0.31335673, 0.8833154, 0.13152207, 1.5482179,
  73. 0.29770762, -0.16246222],
  74. [0.02145994, 0.80424, -0.95061, 1.5875458, -0.00308682, 0.17964548, 0.49912593, 0.46977136,
  75. 0.2151897, 0.30908248]]).astype(np.float32)
  76. expect = np.array([[1.4219905, -0.39837134, 0.5452743, -0.09062839, -0.02375537, -1.5890603, 0.10658137, 0.6185817,
  77. -0.7411523, 0.15054005],
  78. [-0.94926417, 0.13830578, 0.7609547, -0.31733334, 1.8485254, -1.4657221, 1.2625053, -1.523396,
  79. 0.601499, -0.35607445],
  80. [-0.14447737, -1.0622973, 0.80294746, -0.32016528, 0.33523226, 0.63443416, 0.23186903,
  81. 0.53539133, -0.0633494, -0.9495847],
  82. [-0.36894822, 0.253609, -0.5127511, -0.33366728, -0.18740037, 0.19628316, -0.20430653, 1.1471655,
  83. 0.24743511, -0.23741922],
  84. [-1.2582518, 0.57718843, -1.0812542, 1.4944922, -0.8770549, 0.1476463, 0.40500447, 0.23499368,
  85. 0.09027944, 0.26695627]]).astype(np.float32)
  86. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  87. net = LogSoftmax()
  88. dx = Grad(net)(Tensor(x), Tensor(dy))
  89. diff = dx[0].asnumpy() - expect
  90. err = np.ones(shape=expect.shape) * 1.0e-5
  91. assert np.all(diff < err)
  92. @pytest.mark.level0
  93. @pytest.mark.platform_x86_cpu
  94. @pytest.mark.env_onecard
  95. def test_logsoftmaxgrad1():
  96. x = np.array([[-0.47705367, 0.48267725, -1.0453935, 1.574488, 0.20362134, 0.4435456, -0.23984082, -0.43684655,
  97. -0.7725506, 1.4481013],
  98. [1.1012247, 1.7069651, 0.55062026, 0.3361901, -1.1082426, -0.5001939, -0.3255393, -0.7972024,
  99. -0.27965206, -0.702805],
  100. [0.19450496, 0.87596166, 0.6467245, -1.044987, 0.5248943, -2.6166635, 1.6719198, 0.06600758,
  101. -0.4099178, 1.1861311],
  102. [1.1305193, -1.97308, 2.1047623, -1.5105937, 0.93052036, 1.2467804, 0.5310002, 0.7084912, -1.3681422,
  103. -0.9686862],
  104. [1.871408, 0.14219497, -0.41050452, -0.749807, 1.4900619, -1.8172716, -0.73839617, 0.17565694,
  105. -0.4553867, -1.5423119]]).astype(np.float32)
  106. dy = np.array([[1.516363, -0.15196544, 0.598733, 0.64357865, 0.16265012, -1.3521105, 0.22621834, 0.7168259,
  107. -0.6709239, 0.79757756],
  108. [-0.32457778, 1.2831115, 1.1211495, -0.02665559, 1.9170904, -1.3397789, 1.4124829, -1.4298155,
  109. 0.758519, -0.25322974],
  110. [-0.24226122, -1.2555921, 0.6492511, -0.34847677, 0.19916506, 0.628554, -0.19658111, 0.44939864,
  111. -0.11677749, -1.2131723],
  112. [0.24267715, 0.28106326, 1.1075432, -0.29006946, 0.31335673, 0.8833154, 0.13152207, 1.5482179,
  113. 0.29770762, -0.16246222],
  114. [0.02145994, 0.80424, -0.95061, 1.5875458, -0.00308682, 0.17964548, 0.49912593, 0.46977136,
  115. 0.2151897, 0.30908248]]).astype(np.float32)
  116. expect = np.array([[1.464194, -0.29578894, 0.5296974, -0.39600563, -0.1479242, -1.0869746, 0.04521982, 0.5064515,
  117. -0.7515615, 1.0554069],
  118. [-0.5774203, 0.793861, 0.7805745, -0.32800734, 1.8334473, -1.236596, 1.2463496, -1.5765365,
  119. 0.6265108, -0.22322391],
  120. [-0.34437084, -1.4687154, 0.27432096, -0.42420125, -0.22908019, 0.640983, -1.4210342, 0.10155854,
  121. -0.23266247, -1.0147638],
  122. [-0.01768187, 0.26872346, -0.5037259, -0.3376058, -0.3291146, 1.4752979, -0.25972134, 0.8869053,
  123. 0.25325722, -0.13946185],
  124. [-0.5247209, 0.70192003, -1.0808672, 1.4858199, -1.1273282, 0.20728993, 0.38918605, 0.08162117,
  125. 0.10445589, 0.3220427]],).astype(np.float32)
  126. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  127. net = LogSoftmax(0)
  128. dx = Grad(net)(Tensor(x), Tensor(dy))
  129. diff = dx[0].asnumpy() - expect
  130. err = np.ones(shape=expect.shape) * 1.0e-5
  131. assert np.all(diff < err)