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_softplus_grad_op.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. 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. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  23. class SoftplusNet(nn.Cell):
  24. def __init__(self):
  25. super(SoftplusNet, self).__init__()
  26. self.softplus = P.Softplus()
  27. def construct(self, x):
  28. return self.softplus(x)
  29. class Grad(nn.Cell):
  30. def __init__(self, network):
  31. super(Grad, self).__init__()
  32. self.grad = C.GradOperation(get_all=True, sens_param=True)
  33. self.network = network
  34. def construct(self, input_data, sens):
  35. gout = self.grad(self.network)(input_data, sens)
  36. return gout
  37. @pytest.mark.level0
  38. @pytest.mark.platform_x86_gpu_training
  39. @pytest.mark.env_onecard
  40. def test_softplusgrad():
  41. x = np.array([0.58401114, 0.68800163, 0.9760397, 0.14702141, 0.46563736, 0.9607501,
  42. 0.14567593, 0.12261796, 0.37054458, 0.46421242]).astype(np.float32)
  43. dy = np.array([0.5559598, 0.96994054, 0.24770357, 0.34646875, 0.2984393, 0.03287048,
  44. 0.55681044, 0.966908, 0.06015943, 0.6099489]).astype(np.float32)
  45. x_ms = Tensor(x)
  46. dy_ms = Tensor(dy)
  47. net = SoftplusNet()
  48. grad = Grad(net)
  49. output = grad(x_ms, dy_ms)
  50. expect = dy * np.exp(x) / (1 + np.exp(x))
  51. assert np.allclose(output[0].asnumpy(), expect, rtol=1e-3)
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_gpu_training
  54. @pytest.mark.env_onecard
  55. def test_softplusgrad_fp16():
  56. np.random.seed(42)
  57. x_np = np.random.randn(5, 3, 6).astype(np.float16)
  58. dy_np = np.random.randn(5, 3, 6).astype(np.float16)
  59. net = SoftplusNet()
  60. grad = Grad(net)
  61. output = grad(Tensor(x_np), Tensor(dy_np))
  62. expect = dy_np * np.exp(x_np) / (1 + np.exp(x_np))
  63. assert np.allclose(output[0].asnumpy(), expect, rtol=1e-2)