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_exp.py 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 exp"""
  16. import pytest
  17. import mindspore.nn as nn
  18. import mindspore.nn.probability.bijector as msb
  19. from mindspore import Tensor
  20. from mindspore import dtype
  21. def test_init():
  22. b = msb.Exp()
  23. assert isinstance(b, msb.Bijector)
  24. def test_type():
  25. with pytest.raises(TypeError):
  26. msb.Exp(name=0.1)
  27. class Net(nn.Cell):
  28. """
  29. Test class: forward and inverse pass of bijector.
  30. """
  31. def __init__(self):
  32. super(Net, self).__init__()
  33. self.b1 = msb.Exp()
  34. self.b2 = msb.Exp()
  35. def construct(self, x_):
  36. forward = self.b1.forward(x_)
  37. inverse = self.b1.inverse(forward)
  38. return x_ - inverse
  39. def test1():
  40. """
  41. Test forward and inverse pass of exp bijector.
  42. """
  43. net = Net()
  44. x = Tensor([2.0, 3.0, 4.0, 5.0], dtype=dtype.float32)
  45. ans = net(x)
  46. assert isinstance(ans, Tensor)
  47. class Jacobian(nn.Cell):
  48. """
  49. Test class: forward and inverse pass of bijector.
  50. """
  51. def __init__(self):
  52. super(Jacobian, self).__init__()
  53. self.b1 = msb.Exp()
  54. self.b2 = msb.Exp()
  55. def construct(self, x_):
  56. ans1 = self.b1.forward_log_jacobian(x_)
  57. ans2 = self.b1.inverse_log_jacobian(x_)
  58. return ans1 + ans2
  59. def test2():
  60. """
  61. Test jacobians of exp bijector.
  62. """
  63. net = Jacobian()
  64. x = Tensor([2.0, 3.0, 4.0, 5.0], dtype=dtype.float32)
  65. ans = net(x)
  66. assert isinstance(ans, Tensor)