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

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