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_bernoulli.py 5.3 kB

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