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_geometric.py 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Geometric.
  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") == "CPU"
  25. def test_arguments():
  26. """
  27. Args passing during initialization.
  28. """
  29. g = msd.Geometric()
  30. assert isinstance(g, msd.Distribution)
  31. g = msd.Geometric([0.1, 0.3, 0.5, 0.9], dtype=dtype.int32)
  32. assert isinstance(g, msd.Distribution)
  33. def test_type():
  34. with pytest.raises(TypeError):
  35. msd.Geometric([0.1], dtype=dtype.bool_)
  36. def test_name():
  37. with pytest.raises(TypeError):
  38. msd.Geometric([0.1], name=1.0)
  39. def test_seed():
  40. with pytest.raises(TypeError):
  41. msd.Geometric([0.1], seed='seed')
  42. def test_prob():
  43. """
  44. Invalid probability.
  45. """
  46. with pytest.raises(ValueError):
  47. msd.Geometric([-0.1], dtype=dtype.int32)
  48. with pytest.raises(ValueError):
  49. msd.Geometric([1.1], dtype=dtype.int32)
  50. with pytest.raises(ValueError):
  51. msd.Geometric([0.0], dtype=dtype.int32)
  52. with pytest.raises(ValueError):
  53. msd.Geometric([1.0], dtype=dtype.int32)
  54. class GeometricProb(nn.Cell):
  55. """
  56. Geometric distribution: initialize with probs.
  57. """
  58. def __init__(self):
  59. super(GeometricProb, self).__init__()
  60. self.g = msd.Geometric(0.5, dtype=dtype.int32)
  61. def construct(self, value):
  62. prob = self.g.prob(value)
  63. log_prob = self.g.log_prob(value)
  64. cdf = self.g.cdf(value)
  65. log_cdf = self.g.log_cdf(value)
  66. sf = self.g.survival_function(value)
  67. log_sf = self.g.log_survival(value)
  68. return prob + log_prob + cdf + log_cdf + sf + log_sf
  69. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  70. def test_geometric_prob():
  71. """
  72. Test probability functions: passing value through construct.
  73. """
  74. net = GeometricProb()
  75. value = Tensor([3, 4, 5, 6, 7], dtype=dtype.float32)
  76. ans = net(value)
  77. assert isinstance(ans, Tensor)
  78. class GeometricProb1(nn.Cell):
  79. """
  80. Geometric distribution: initialize without probs.
  81. """
  82. def __init__(self):
  83. super(GeometricProb1, self).__init__()
  84. self.g = msd.Geometric(dtype=dtype.int32)
  85. def construct(self, value, probs):
  86. prob = self.g.prob(value, probs)
  87. log_prob = self.g.log_prob(value, probs)
  88. cdf = self.g.cdf(value, probs)
  89. log_cdf = self.g.log_cdf(value, probs)
  90. sf = self.g.survival_function(value, probs)
  91. log_sf = self.g.log_survival(value, probs)
  92. return prob + log_prob + cdf + log_cdf + sf + log_sf
  93. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  94. def test_geometric_prob1():
  95. """
  96. Test probability functions: passing value/probs through construct.
  97. """
  98. net = GeometricProb1()
  99. value = Tensor([3, 4, 5, 6, 7], dtype=dtype.float32)
  100. probs = Tensor([0.5], dtype=dtype.float32)
  101. ans = net(value, probs)
  102. assert isinstance(ans, Tensor)
  103. class GeometricKl(nn.Cell):
  104. """
  105. Test class: kl_loss between Geometric distributions.
  106. """
  107. def __init__(self):
  108. super(GeometricKl, self).__init__()
  109. self.g1 = msd.Geometric(0.7, dtype=dtype.int32)
  110. self.g2 = msd.Geometric(dtype=dtype.int32)
  111. def construct(self, probs_b, probs_a):
  112. kl1 = self.g1.kl_loss('Geometric', probs_b)
  113. kl2 = self.g2.kl_loss('Geometric', probs_b, probs_a)
  114. return kl1 + kl2
  115. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  116. def test_kl():
  117. """
  118. Test kl_loss function.
  119. """
  120. ber_net = GeometricKl()
  121. probs_b = Tensor([0.3], dtype=dtype.float32)
  122. probs_a = Tensor([0.7], dtype=dtype.float32)
  123. ans = ber_net(probs_b, probs_a)
  124. assert isinstance(ans, Tensor)
  125. class GeometricCrossEntropy(nn.Cell):
  126. """
  127. Test class: cross_entropy of Geometric distribution.
  128. """
  129. def __init__(self):
  130. super(GeometricCrossEntropy, self).__init__()
  131. self.g1 = msd.Geometric(0.3, dtype=dtype.int32)
  132. self.g2 = msd.Geometric(dtype=dtype.int32)
  133. def construct(self, probs_b, probs_a):
  134. h1 = self.g1.cross_entropy('Geometric', probs_b)
  135. h2 = self.g2.cross_entropy('Geometric', probs_b, probs_a)
  136. return h1 + h2
  137. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  138. def test_cross_entropy():
  139. """
  140. Test cross_entropy between Geometric distributions.
  141. """
  142. net = GeometricCrossEntropy()
  143. probs_b = Tensor([0.3], dtype=dtype.float32)
  144. probs_a = Tensor([0.7], dtype=dtype.float32)
  145. ans = net(probs_b, probs_a)
  146. assert isinstance(ans, Tensor)
  147. class GeometricBasics(nn.Cell):
  148. """
  149. Test class: basic mean/sd/mode/entropy function.
  150. """
  151. def __init__(self):
  152. super(GeometricBasics, self).__init__()
  153. self.g = msd.Geometric([0.3, 0.5], dtype=dtype.int32)
  154. def construct(self):
  155. mean = self.g.mean()
  156. sd = self.g.sd()
  157. var = self.g.var()
  158. mode = self.g.mode()
  159. entropy = self.g.entropy()
  160. return mean + sd + var + mode + entropy
  161. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  162. def test_bascis():
  163. """
  164. Test mean/sd/mode/entropy functionality of Geometric distribution.
  165. """
  166. net = GeometricBasics()
  167. ans = net()
  168. assert isinstance(ans, Tensor)
  169. class GeoConstruct(nn.Cell):
  170. """
  171. Bernoulli distribution: going through construct.
  172. """
  173. def __init__(self):
  174. super(GeoConstruct, self).__init__()
  175. self.g = msd.Geometric(0.5, dtype=dtype.int32)
  176. self.g1 = msd.Geometric(dtype=dtype.int32)
  177. def construct(self, value, probs):
  178. prob = self.g('prob', value)
  179. prob1 = self.g('prob', value, probs)
  180. prob2 = self.g1('prob', value, probs)
  181. return prob + prob1 + prob2
  182. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  183. def test_geo_construct():
  184. """
  185. Test probability function going through construct.
  186. """
  187. net = GeoConstruct()
  188. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  189. probs = Tensor([0.5], dtype=dtype.float32)
  190. ans = net(value, probs)
  191. assert isinstance(ans, Tensor)