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 5.2 kB

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