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_sigmoid.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 operations as P
  21. from mindspore.ops.operations import _grad_ops as G
  22. class NetSigmoid(nn.Cell):
  23. def __init__(self):
  24. super(NetSigmoid, self).__init__()
  25. self.sigmoid = P.Sigmoid()
  26. def construct(self, x):
  27. return self.sigmoid(x)
  28. class NetSigmoidGrad(nn.Cell):
  29. def __init__(self):
  30. super(NetSigmoidGrad, self).__init__()
  31. self.sigmoid_grad = G.SigmoidGrad()
  32. def construct(self, y, dy):
  33. return self.sigmoid_grad(y, dy)
  34. @pytest.mark.level0
  35. @pytest.mark.platform_x86_gpu_training
  36. @pytest.mark.env_onecard
  37. def test_sigmoid():
  38. x = Tensor(np.array([[[[-1, 1, 10],
  39. [1, -1, 1],
  40. [10, 1, -1]]]]).astype(np.float32))
  41. error = np.ones(shape=[1, 1, 3, 3]) * 1.0e-6
  42. context.set_context(mode=context.GRAPH_MODE,
  43. enable_graph_kernel=True, device_target="GPU")
  44. net = NetSigmoid()
  45. result_open_gk = net(x)
  46. context.set_context(mode=context.GRAPH_MODE,
  47. enable_graph_kernel=False, device_target="GPU")
  48. net_beta = NetSigmoid()
  49. result_close_gk = net_beta(x)
  50. diff = result_open_gk.asnumpy() - result_close_gk.asnumpy()
  51. assert np.all(abs(diff) < error)
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_gpu_training
  54. @pytest.mark.env_onecard
  55. def test_sigmoid_grad():
  56. y = Tensor(np.array([[[[-1, 1, 2],
  57. [1, -1, 1],
  58. [2, 1, -1]]]]).astype(np.float32))
  59. dy = Tensor(np.array([[[[-11, 2, 4],
  60. [-1, 1, -1],
  61. [-4, 4, -4]]]]).astype(np.float32))
  62. error = np.ones(shape=[1, 1, 3, 3]) * 1.0e-6
  63. context.set_context(mode=context.GRAPH_MODE,
  64. enable_graph_kernel=True, device_target="GPU")
  65. net = NetSigmoidGrad()
  66. result_open_gk = net(y, dy)
  67. context.set_context(mode=context.GRAPH_MODE,
  68. enable_graph_kernel=False, device_target="GPU")
  69. net_beta = NetSigmoidGrad()
  70. result_close_gk = net_beta(y, dy)
  71. diff = result_open_gk.asnumpy() - result_close_gk.asnumpy()
  72. assert np.all(abs(diff) < error)