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

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