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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Distribution.Geometric.
  17. """
  18. import pytest
  19. import mindspore.nn as nn
  20. from mindspore import dtype
  21. from mindspore import Tensor
  22. def test_arguments():
  23. """
  24. Args passing during initialization.
  25. """
  26. g = nn.Geometric()
  27. assert isinstance(g, nn.Distribution)
  28. g = nn.Geometric([0.0, 0.3, 0.5, 1.0], dtype=dtype.int32)
  29. assert isinstance(g, nn.Distribution)
  30. def test_prob():
  31. """
  32. Invalid probability.
  33. """
  34. with pytest.raises(ValueError):
  35. nn.Geometric([-0.1], dtype=dtype.int32)
  36. with pytest.raises(ValueError):
  37. nn.Geometric([1.1], dtype=dtype.int32)
  38. class GeometricProb(nn.Cell):
  39. """
  40. Geometric distribution: initialize with probs.
  41. """
  42. def __init__(self):
  43. super(GeometricProb, self).__init__()
  44. self.g = nn.Geometric(0.5, dtype=dtype.int32)
  45. def construct(self, value):
  46. prob = self.g('prob', value)
  47. log_prob = self.g('log_prob', value)
  48. cdf = self.g('cdf', value)
  49. log_cdf = self.g('log_cdf', value)
  50. sf = self.g('survival_function', value)
  51. log_sf = self.g('log_survival', value)
  52. return prob + log_prob + cdf + log_cdf + sf + log_sf
  53. def test_geometric_prob():
  54. """
  55. Test probability functions: passing value through construct.
  56. """
  57. net = GeometricProb()
  58. value = Tensor([3, 4, 5, 6, 7], dtype=dtype.float32)
  59. ans = net(value)
  60. assert isinstance(ans, Tensor)
  61. class GeometricProb1(nn.Cell):
  62. """
  63. Geometric distribution: initialize without probs.
  64. """
  65. def __init__(self):
  66. super(GeometricProb1, self).__init__()
  67. self.g = nn.Geometric(dtype=dtype.int32)
  68. def construct(self, value, probs):
  69. prob = self.g('prob', value, probs)
  70. log_prob = self.g('log_prob', value, probs)
  71. cdf = self.g('cdf', value, probs)
  72. log_cdf = self.g('log_cdf', value, probs)
  73. sf = self.g('survival_function', value, probs)
  74. log_sf = self.g('log_survival', value, probs)
  75. return prob + log_prob + cdf + log_cdf + sf + log_sf
  76. def test_geometric_prob1():
  77. """
  78. Test probability functions: passing value/probs through construct.
  79. """
  80. net = GeometricProb1()
  81. value = Tensor([3, 4, 5, 6, 7], dtype=dtype.float32)
  82. probs = Tensor([0.5], dtype=dtype.float32)
  83. ans = net(value, probs)
  84. assert isinstance(ans, Tensor)
  85. class GeometricKl(nn.Cell):
  86. """
  87. Test class: kl_loss between Geometric distributions.
  88. """
  89. def __init__(self):
  90. super(GeometricKl, self).__init__()
  91. self.g1 = nn.Geometric(0.7, dtype=dtype.int32)
  92. self.g2 = nn.Geometric(dtype=dtype.int32)
  93. def construct(self, probs_b, probs_a):
  94. kl1 = self.g1('kl_loss', 'Geometric', probs_b)
  95. kl2 = self.g2('kl_loss', 'Geometric', probs_b, probs_a)
  96. return kl1 + kl2
  97. def test_kl():
  98. """
  99. Test kl_loss function.
  100. """
  101. ber_net = GeometricKl()
  102. probs_b = Tensor([0.3], dtype=dtype.float32)
  103. probs_a = Tensor([0.7], dtype=dtype.float32)
  104. ans = ber_net(probs_b, probs_a)
  105. assert isinstance(ans, Tensor)
  106. class GeometricCrossEntropy(nn.Cell):
  107. """
  108. Test class: cross_entropy of Geometric distribution.
  109. """
  110. def __init__(self):
  111. super(GeometricCrossEntropy, self).__init__()
  112. self.g1 = nn.Geometric(0.3, dtype=dtype.int32)
  113. self.g2 = nn.Geometric(dtype=dtype.int32)
  114. def construct(self, probs_b, probs_a):
  115. h1 = self.g1('cross_entropy', 'Geometric', probs_b)
  116. h2 = self.g2('cross_entropy', 'Geometric', probs_b, probs_a)
  117. return h1 + h2
  118. def test_cross_entropy():
  119. """
  120. Test cross_entropy between Geometric distributions.
  121. """
  122. net = GeometricCrossEntropy()
  123. probs_b = Tensor([0.3], dtype=dtype.float32)
  124. probs_a = Tensor([0.7], dtype=dtype.float32)
  125. ans = net(probs_b, probs_a)
  126. assert isinstance(ans, Tensor)
  127. class GeometricBasics(nn.Cell):
  128. """
  129. Test class: basic mean/sd/mode/entropy function.
  130. """
  131. def __init__(self):
  132. super(GeometricBasics, self).__init__()
  133. self.g = nn.Geometric([0.3, 0.5], dtype=dtype.int32)
  134. def construct(self):
  135. mean = self.g('mean')
  136. sd = self.g('sd')
  137. var = self.g('var')
  138. mode = self.g('mode')
  139. entropy = self.g('entropy')
  140. return mean + sd + var + mode + entropy
  141. def test_bascis():
  142. """
  143. Test mean/sd/mode/entropy functionality of Geometric distribution.
  144. """
  145. net = GeometricBasics()
  146. ans = net()
  147. assert isinstance(ans, Tensor)