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_poisson.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. """
  16. Test nn.probability.distribution.Poisson.
  17. """
  18. import pytest
  19. import mindspore.nn as nn
  20. import mindspore.nn.probability.distribution as msd
  21. from mindspore import dtype
  22. from mindspore import Tensor
  23. def test_arguments():
  24. """
  25. Args passing during initialization.
  26. """
  27. p = msd.Poisson()
  28. assert isinstance(p, msd.Distribution)
  29. p = msd.Poisson([0.1, 0.3, 0.5, 1.0], dtype=dtype.float32)
  30. assert isinstance(p, msd.Distribution)
  31. def test_type():
  32. with pytest.raises(TypeError):
  33. msd.Poisson([0.1], dtype=dtype.bool_)
  34. def test_name():
  35. with pytest.raises(TypeError):
  36. msd.Poisson([0.1], name=1.0)
  37. def test_seed():
  38. with pytest.raises(TypeError):
  39. msd.Poisson([0.1], seed='seed')
  40. def test_rate():
  41. """
  42. Invalid rate.
  43. """
  44. with pytest.raises(ValueError):
  45. msd.Poisson([-0.1], dtype=dtype.float32)
  46. with pytest.raises(ValueError):
  47. msd.Poisson([0.0], dtype=dtype.float32)
  48. class PoissonProb(nn.Cell):
  49. """
  50. Poisson distribution: initialize with rate.
  51. """
  52. def __init__(self):
  53. super(PoissonProb, self).__init__()
  54. self.p = msd.Poisson([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  55. def construct(self, value):
  56. prob = self.p.prob(value)
  57. log_prob = self.p.log_prob(value)
  58. cdf = self.p.cdf(value)
  59. log_cdf = self.p.log_cdf(value)
  60. sf = self.p.survival_function(value)
  61. log_sf = self.p.log_survival(value)
  62. return prob + log_prob + cdf + log_cdf + sf + log_sf
  63. def test_poisson_prob():
  64. """
  65. Test probability functions: passing value through construct.
  66. """
  67. net = PoissonProb()
  68. value = Tensor([0.2, 0.3, 5.0, 2, 3.9], dtype=dtype.float32)
  69. ans = net(value)
  70. assert isinstance(ans, Tensor)
  71. class PoissonProb1(nn.Cell):
  72. """
  73. Poisson distribution: initialize without rate.
  74. """
  75. def __init__(self):
  76. super(PoissonProb1, self).__init__()
  77. self.p = msd.Poisson(dtype=dtype.float32)
  78. def construct(self, value, rate):
  79. prob = self.p.prob(value, rate)
  80. log_prob = self.p.log_prob(value, rate)
  81. cdf = self.p.cdf(value, rate)
  82. log_cdf = self.p.log_cdf(value, rate)
  83. sf = self.p.survival_function(value, rate)
  84. log_sf = self.p.log_survival(value, rate)
  85. return prob + log_prob + cdf + log_cdf + sf + log_sf
  86. def test_poisson_prob1():
  87. """
  88. Test probability functions: passing value/rate through construct.
  89. """
  90. net = PoissonProb1()
  91. value = Tensor([0.2, 0.9, 1, 2, 3], dtype=dtype.float32)
  92. rate = Tensor([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  93. ans = net(value, rate)
  94. assert isinstance(ans, Tensor)
  95. class PoissonBasics(nn.Cell):
  96. """
  97. Test class: basic mean/sd/var/mode function.
  98. """
  99. def __init__(self):
  100. super(PoissonBasics, self).__init__()
  101. self.p = msd.Poisson([2.3, 2.5], dtype=dtype.float32)
  102. def construct(self):
  103. mean = self.p.mean()
  104. sd = self.p.sd()
  105. var = self.p.var()
  106. return mean + sd + var
  107. def test_bascis():
  108. """
  109. Test mean/sd/var/mode functionality of Poisson distribution.
  110. """
  111. net = PoissonBasics()
  112. ans = net()
  113. assert isinstance(ans, Tensor)
  114. class PoissonConstruct(nn.Cell):
  115. """
  116. Poisson distribution: going through construct.
  117. """
  118. def __init__(self):
  119. super(PoissonConstruct, self).__init__()
  120. self.p = msd.Poisson([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  121. self.p1 = msd.Poisson(dtype=dtype.float32)
  122. def construct(self, value, rate):
  123. prob = self.p('prob', value)
  124. prob1 = self.p('prob', value, rate)
  125. prob2 = self.p1('prob', value, rate)
  126. return prob + prob1 + prob2
  127. def test_poisson_construct():
  128. """
  129. Test probability function going through construct.
  130. """
  131. net = PoissonConstruct()
  132. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  133. probs = Tensor([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  134. ans = net(value, probs)
  135. assert isinstance(ans, Tensor)