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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.1, 0.3, 0.5, 0.9], 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.bool_)
  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. with pytest.raises(ValueError):
  49. msd.Bernoulli([0.0], dtype=dtype.int32)
  50. with pytest.raises(ValueError):
  51. msd.Bernoulli([1.0], dtype=dtype.int32)
  52. class BernoulliProb(nn.Cell):
  53. """
  54. Bernoulli distribution: initialize with probs.
  55. """
  56. def __init__(self):
  57. super(BernoulliProb, self).__init__()
  58. self.b = msd.Bernoulli(0.5, dtype=dtype.int32)
  59. def construct(self, value):
  60. prob = self.b.prob(value)
  61. log_prob = self.b.log_prob(value)
  62. cdf = self.b.cdf(value)
  63. log_cdf = self.b.log_cdf(value)
  64. sf = self.b.survival_function(value)
  65. log_sf = self.b.log_survival(value)
  66. return prob + log_prob + cdf + log_cdf + sf + log_sf
  67. def test_bernoulli_prob():
  68. """
  69. Test probability functions: passing value through construct.
  70. """
  71. net = BernoulliProb()
  72. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  73. ans = net(value)
  74. assert isinstance(ans, Tensor)
  75. class BernoulliProb1(nn.Cell):
  76. """
  77. Bernoulli distribution: initialize without probs.
  78. """
  79. def __init__(self):
  80. super(BernoulliProb1, self).__init__()
  81. self.b = msd.Bernoulli(dtype=dtype.int32)
  82. def construct(self, value, probs):
  83. prob = self.b.prob(value, probs)
  84. log_prob = self.b.log_prob(value, probs)
  85. cdf = self.b.cdf(value, probs)
  86. log_cdf = self.b.log_cdf(value, probs)
  87. sf = self.b.survival_function(value, probs)
  88. log_sf = self.b.log_survival(value, probs)
  89. return prob + log_prob + cdf + log_cdf + sf + log_sf
  90. def test_bernoulli_prob1():
  91. """
  92. Test probability functions: passing value/probs through construct.
  93. """
  94. net = BernoulliProb1()
  95. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  96. probs = Tensor([0.5], dtype=dtype.float32)
  97. ans = net(value, probs)
  98. assert isinstance(ans, Tensor)
  99. class BernoulliKl(nn.Cell):
  100. """
  101. Test class: kl_loss between Bernoulli distributions.
  102. """
  103. def __init__(self):
  104. super(BernoulliKl, self).__init__()
  105. self.b1 = msd.Bernoulli(0.7, dtype=dtype.int32)
  106. self.b2 = msd.Bernoulli(dtype=dtype.int32)
  107. def construct(self, probs_b, probs_a):
  108. kl1 = self.b1.kl_loss('Bernoulli', probs_b)
  109. kl2 = self.b2.kl_loss('Bernoulli', probs_b, probs_a)
  110. return kl1 + kl2
  111. def test_kl():
  112. """
  113. Test kl_loss function.
  114. """
  115. ber_net = BernoulliKl()
  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 BernoulliCrossEntropy(nn.Cell):
  121. """
  122. Test class: cross_entropy of Bernoulli distribution.
  123. """
  124. def __init__(self):
  125. super(BernoulliCrossEntropy, self).__init__()
  126. self.b1 = msd.Bernoulli(0.7, dtype=dtype.int32)
  127. self.b2 = msd.Bernoulli(dtype=dtype.int32)
  128. def construct(self, probs_b, probs_a):
  129. h1 = self.b1.cross_entropy('Bernoulli', probs_b)
  130. h2 = self.b2.cross_entropy('Bernoulli', probs_b, probs_a)
  131. return h1 + h2
  132. def test_cross_entropy():
  133. """
  134. Test cross_entropy between Bernoulli distributions.
  135. """
  136. net = BernoulliCrossEntropy()
  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 BernoulliConstruct(nn.Cell):
  142. """
  143. Bernoulli distribution: going through construct.
  144. """
  145. def __init__(self):
  146. super(BernoulliConstruct, self).__init__()
  147. self.b = msd.Bernoulli(0.5, dtype=dtype.int32)
  148. self.b1 = msd.Bernoulli(dtype=dtype.int32)
  149. def construct(self, value, probs):
  150. prob = self.b('prob', value)
  151. prob1 = self.b('prob', value, probs)
  152. prob2 = self.b1('prob', value, probs)
  153. return prob + prob1 + prob2
  154. def test_bernoulli_construct():
  155. """
  156. Test probability function going through construct.
  157. """
  158. net = BernoulliConstruct()
  159. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  160. probs = Tensor([0.5], dtype=dtype.float32)
  161. ans = net(value, probs)
  162. assert isinstance(ans, Tensor)
  163. class BernoulliMean(nn.Cell):
  164. """
  165. Test class: basic mean/sd/var/mode/entropy function.
  166. """
  167. def __init__(self):
  168. super(BernoulliMean, self).__init__()
  169. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  170. def construct(self):
  171. mean = self.b.mean()
  172. return mean
  173. def test_mean():
  174. """
  175. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  176. """
  177. net = BernoulliMean()
  178. ans = net()
  179. assert isinstance(ans, Tensor)
  180. class BernoulliSd(nn.Cell):
  181. """
  182. Test class: basic mean/sd/var/mode/entropy function.
  183. """
  184. def __init__(self):
  185. super(BernoulliSd, self).__init__()
  186. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  187. def construct(self):
  188. sd = self.b.sd()
  189. return sd
  190. def test_sd():
  191. """
  192. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  193. """
  194. net = BernoulliSd()
  195. ans = net()
  196. assert isinstance(ans, Tensor)
  197. class BernoulliVar(nn.Cell):
  198. """
  199. Test class: basic mean/sd/var/mode/entropy function.
  200. """
  201. def __init__(self):
  202. super(BernoulliVar, self).__init__()
  203. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  204. def construct(self):
  205. var = self.b.var()
  206. return var
  207. def test_var():
  208. """
  209. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  210. """
  211. net = BernoulliVar()
  212. ans = net()
  213. assert isinstance(ans, Tensor)
  214. class BernoulliMode(nn.Cell):
  215. """
  216. Test class: basic mean/sd/var/mode/entropy function.
  217. """
  218. def __init__(self):
  219. super(BernoulliMode, self).__init__()
  220. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  221. def construct(self):
  222. mode = self.b.mode()
  223. return mode
  224. def test_mode():
  225. """
  226. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  227. """
  228. net = BernoulliMode()
  229. ans = net()
  230. assert isinstance(ans, Tensor)
  231. class BernoulliEntropy(nn.Cell):
  232. """
  233. Test class: basic mean/sd/var/mode/entropy function.
  234. """
  235. def __init__(self):
  236. super(BernoulliEntropy, self).__init__()
  237. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  238. def construct(self):
  239. entropy = self.b.entropy()
  240. return entropy
  241. def test_entropy():
  242. """
  243. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  244. """
  245. net = BernoulliEntropy()
  246. ans = net()
  247. assert isinstance(ans, Tensor)