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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. def test_scalar():
  49. with pytest.raises(TypeError):
  50. msd.Poisson(0.1, seed='seed')
  51. class PoissonProb(nn.Cell):
  52. """
  53. Poisson distribution: initialize with rate.
  54. """
  55. def __init__(self):
  56. super(PoissonProb, self).__init__()
  57. self.p = msd.Poisson([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  58. def construct(self, value):
  59. prob = self.p.prob(value)
  60. log_prob = self.p.log_prob(value)
  61. cdf = self.p.cdf(value)
  62. log_cdf = self.p.log_cdf(value)
  63. sf = self.p.survival_function(value)
  64. log_sf = self.p.log_survival(value)
  65. return prob + log_prob + cdf + log_cdf + sf + log_sf
  66. def test_poisson_prob():
  67. """
  68. Test probability functions: passing value through construct.
  69. """
  70. net = PoissonProb()
  71. value = Tensor([0.2, 0.3, 5.0, 2, 3.9], dtype=dtype.float32)
  72. ans = net(value)
  73. assert isinstance(ans, Tensor)
  74. class PoissonProb1(nn.Cell):
  75. """
  76. Poisson distribution: initialize without rate.
  77. """
  78. def __init__(self):
  79. super(PoissonProb1, self).__init__()
  80. self.p = msd.Poisson(dtype=dtype.float32)
  81. def construct(self, value, rate):
  82. prob = self.p.prob(value, rate)
  83. log_prob = self.p.log_prob(value, rate)
  84. cdf = self.p.cdf(value, rate)
  85. log_cdf = self.p.log_cdf(value, rate)
  86. sf = self.p.survival_function(value, rate)
  87. log_sf = self.p.log_survival(value, rate)
  88. return prob + log_prob + cdf + log_cdf + sf + log_sf
  89. def test_poisson_prob1():
  90. """
  91. Test probability functions: passing value/rate through construct.
  92. """
  93. net = PoissonProb1()
  94. value = Tensor([0.2, 0.9, 1, 2, 3], dtype=dtype.float32)
  95. rate = Tensor([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  96. ans = net(value, rate)
  97. assert isinstance(ans, Tensor)
  98. class PoissonBasics(nn.Cell):
  99. """
  100. Test class: basic mean/sd/var/mode function.
  101. """
  102. def __init__(self):
  103. super(PoissonBasics, self).__init__()
  104. self.p = msd.Poisson([2.3, 2.5], dtype=dtype.float32)
  105. def construct(self):
  106. mean = self.p.mean()
  107. sd = self.p.sd()
  108. var = self.p.var()
  109. return mean + sd + var
  110. def test_bascis():
  111. """
  112. Test mean/sd/var/mode functionality of Poisson distribution.
  113. """
  114. net = PoissonBasics()
  115. ans = net()
  116. assert isinstance(ans, Tensor)
  117. class PoissonConstruct(nn.Cell):
  118. """
  119. Poisson distribution: going through construct.
  120. """
  121. def __init__(self):
  122. super(PoissonConstruct, self).__init__()
  123. self.p = msd.Poisson([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  124. self.p1 = msd.Poisson(dtype=dtype.float32)
  125. def construct(self, value, rate):
  126. prob = self.p('prob', value)
  127. prob1 = self.p('prob', value, rate)
  128. prob2 = self.p1('prob', value, rate)
  129. return prob + prob1 + prob2
  130. def test_poisson_construct():
  131. """
  132. Test probability function going through construct.
  133. """
  134. net = PoissonConstruct()
  135. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  136. probs = Tensor([0.5, 0.5, 0.5, 0.5, 0.5], dtype=dtype.float32)
  137. ans = net(value, probs)
  138. assert isinstance(ans, Tensor)