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.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2019 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 cases for scalar affine"""
  16. import numpy as np
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. import mindspore.nn.probability.bijector as msb
  20. from mindspore import Tensor
  21. from mindspore import dtype
  22. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  23. class Net(nn.Cell):
  24. """
  25. Test class: forward pass of bijector.
  26. """
  27. def __init__(self):
  28. super(Net, self).__init__()
  29. self.bijector = msb.Softplus(sharpness=2.0)
  30. def construct(self, x_):
  31. return self.bijector.forward(x_)
  32. def test_forward():
  33. forward = Net()
  34. x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
  35. ans = forward(Tensor(x, dtype=dtype.float32))
  36. expected = np.log(1 + np.exp(2 * x)) * 0.5
  37. tol = 1e-6
  38. assert (np.abs(ans.asnumpy() - expected) < tol).all()
  39. class Net1(nn.Cell):
  40. """
  41. Test class: backward pass of bijector.
  42. """
  43. def __init__(self):
  44. super(Net1, self).__init__()
  45. self.bijector = msb.Softplus(sharpness=2.0)
  46. def construct(self, x_):
  47. return self.bijector.inverse(x_)
  48. def test_backward():
  49. backward = Net1()
  50. x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
  51. ans = backward(Tensor(x, dtype=dtype.float32))
  52. expected = np.log(np.exp(2 * x) - 1) * 0.5
  53. tol = 1e-6
  54. assert (np.abs(ans.asnumpy() - expected) < tol).all()
  55. class Net2(nn.Cell):
  56. """
  57. Test class: Forward Jacobian.
  58. """
  59. def __init__(self):
  60. super(Net2, self).__init__()
  61. self.bijector = msb.Softplus(sharpness=2.0)
  62. def construct(self, x_):
  63. return self.bijector.forward_log_jacobian(x_)
  64. def test_forward_jacobian():
  65. forward_jacobian = Net2()
  66. x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
  67. ans = forward_jacobian(Tensor(x, dtype=dtype.float32))
  68. expected = np.log(np.exp(2 * x) / (1 + np.exp(2.0 * x)))
  69. tol = 1e-6
  70. assert (np.abs(ans.asnumpy() - expected) < tol).all()
  71. class Net3(nn.Cell):
  72. """
  73. Test class: Backward Jacobian.
  74. """
  75. def __init__(self):
  76. super(Net3, self).__init__()
  77. self.bijector = msb.Softplus(sharpness=2.0)
  78. def construct(self, x_):
  79. return self.bijector.inverse_log_jacobian(x_)
  80. def test_backward_jacobian():
  81. backward_jacobian = Net3()
  82. x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
  83. ans = backward_jacobian(Tensor(x, dtype=dtype.float32))
  84. expected = np.log(np.exp(2.0 * x) / np.expm1(2.0 * x))
  85. tol = 1e-6
  86. assert (np.abs(ans.asnumpy() - expected) < tol).all()