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_get_dist_args.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Normal distribution"""
  16. import numpy as np
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. import mindspore.nn.probability.distribution as msd
  20. from mindspore import Tensor
  21. from mindspore import dtype
  22. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  23. class Net1(nn.Cell):
  24. """
  25. Test class: Normal distribution. `dist_spec_args` are `mean`, `sd`.
  26. """
  27. def __init__(self):
  28. super(Net1, self).__init__()
  29. self.normal = msd.Normal(dtype=dtype.float32)
  30. self.normal1 = msd.Normal(0.0, 1.0, dtype=dtype.float32)
  31. self.normal2 = msd.Normal(3.0, 4.0, dtype=dtype.float32)
  32. def construct(self, value, mean, sd, mean_a, sd_a):
  33. args_list = self.normal.get_dist_args(mean, sd)
  34. prob = self.normal1.prob(value, *args_list)
  35. args_list1 = self.normal.get_dist_args()
  36. prob1 = self.normal2.prob(value, *args_list1)
  37. args_list2 = self.normal1.get_dist_args()
  38. dist_type = self.normal1.get_dist_type()
  39. kl_loss = self.normal2.kl_loss(dist_type, *args_list2)
  40. args_list3 = self.normal.get_dist_args(mean_a, sd_a)
  41. dist_type = self.normal1.get_dist_type()
  42. kl_loss1 = self.normal2.kl_loss(dist_type, *args_list3)
  43. return prob, prob1, kl_loss, kl_loss1
  44. def test1():
  45. """
  46. Test Normal with two `dist_spec_args`.
  47. """
  48. net = Net1()
  49. mean = Tensor(3.0, dtype=dtype.float32)
  50. sd = Tensor(4.0, dtype=dtype.float32)
  51. mean_a = Tensor(0.0, dtype=dtype.float32)
  52. sd_a = Tensor(1.0, dtype=dtype.float32)
  53. value = Tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
  54. ans, expected, ans1, expected1 = net(value, mean, sd, mean_a, sd_a)
  55. tol = 1e-6
  56. assert (np.abs(ans.asnumpy() - expected.asnumpy()) < tol).all()
  57. assert (np.abs(ans1.asnumpy() - expected1.asnumpy()) < tol).all()
  58. class Net2(nn.Cell):
  59. """
  60. Test class: Exponential distribution. `dist_spec_args` is `rate`.
  61. """
  62. def __init__(self):
  63. super(Net2, self).__init__()
  64. self.expon = msd.Exponential(dtype=dtype.float32)
  65. self.expon1 = msd.Exponential(1.0, dtype=dtype.float32)
  66. self.expon2 = msd.Exponential(2.0, dtype=dtype.float32)
  67. def construct(self, value, rate, rate1):
  68. args_list = self.expon.get_dist_args(rate)
  69. prob = self.expon1.prob(value, *args_list)
  70. args_list1 = self.expon.get_dist_args()
  71. prob1 = self.expon2.prob(value, *args_list1)
  72. args_list2 = self.expon1.get_dist_args()
  73. dist_type = self.expon1.get_dist_type()
  74. kl_loss = self.expon2.kl_loss(dist_type, *args_list2)
  75. args_list3 = self.expon.get_dist_args(rate1)
  76. dist_type = self.expon.get_dist_type()
  77. kl_loss1 = self.expon2.kl_loss(dist_type, *args_list3)
  78. return prob, prob1, kl_loss, kl_loss1
  79. def test2():
  80. """
  81. Test Expomential with single `dist_spec_args`.
  82. """
  83. net = Net2()
  84. rate = Tensor(2.0, dtype=dtype.float32)
  85. rate1 = Tensor(1.0, dtype=dtype.float32)
  86. value = Tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
  87. ans, expected, ans1, expected1 = net(value, rate, rate1)
  88. tol = 1e-6
  89. assert (np.abs(ans.asnumpy() - expected.asnumpy()) < tol).all()
  90. assert (np.abs(ans1.asnumpy() - expected1.asnumpy()) < tol).all()