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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. from mindspore import context
  24. skip_flag = context.get_context("device_target") == "CPU"
  25. def test_arguments():
  26. """
  27. Args passing during initialization.
  28. """
  29. b = msd.Bernoulli()
  30. assert isinstance(b, msd.Distribution)
  31. b = msd.Bernoulli([0.1, 0.3, 0.5, 0.9], dtype=dtype.int32)
  32. assert isinstance(b, msd.Distribution)
  33. def test_type():
  34. with pytest.raises(TypeError):
  35. msd.Bernoulli([0.1], dtype=dtype.bool_)
  36. def test_name():
  37. with pytest.raises(TypeError):
  38. msd.Bernoulli([0.1], name=1.0)
  39. def test_seed():
  40. with pytest.raises(TypeError):
  41. msd.Bernoulli([0.1], seed='seed')
  42. def test_prob():
  43. """
  44. Invalid probability.
  45. """
  46. with pytest.raises(ValueError):
  47. msd.Bernoulli([-0.1], dtype=dtype.int32)
  48. with pytest.raises(ValueError):
  49. msd.Bernoulli([1.1], dtype=dtype.int32)
  50. with pytest.raises(ValueError):
  51. msd.Bernoulli([0.0], dtype=dtype.int32)
  52. with pytest.raises(ValueError):
  53. msd.Bernoulli([1.0], dtype=dtype.int32)
  54. class BernoulliProb(nn.Cell):
  55. """
  56. Bernoulli distribution: initialize with probs.
  57. """
  58. def __init__(self):
  59. super(BernoulliProb, self).__init__()
  60. self.b = msd.Bernoulli(0.5, dtype=dtype.int32)
  61. def construct(self, value):
  62. prob = self.b.prob(value)
  63. log_prob = self.b.log_prob(value)
  64. cdf = self.b.cdf(value)
  65. log_cdf = self.b.log_cdf(value)
  66. sf = self.b.survival_function(value)
  67. log_sf = self.b.log_survival(value)
  68. return prob + log_prob + cdf + log_cdf + sf + log_sf
  69. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  70. def test_bernoulli_prob():
  71. """
  72. Test probability functions: passing value through construct.
  73. """
  74. net = BernoulliProb()
  75. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  76. ans = net(value)
  77. assert isinstance(ans, Tensor)
  78. class BernoulliProb1(nn.Cell):
  79. """
  80. Bernoulli distribution: initialize without probs.
  81. """
  82. def __init__(self):
  83. super(BernoulliProb1, self).__init__()
  84. self.b = msd.Bernoulli(dtype=dtype.int32)
  85. def construct(self, value, probs):
  86. prob = self.b.prob(value, probs)
  87. log_prob = self.b.log_prob(value, probs)
  88. cdf = self.b.cdf(value, probs)
  89. log_cdf = self.b.log_cdf(value, probs)
  90. sf = self.b.survival_function(value, probs)
  91. log_sf = self.b.log_survival(value, probs)
  92. return prob + log_prob + cdf + log_cdf + sf + log_sf
  93. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  94. def test_bernoulli_prob1():
  95. """
  96. Test probability functions: passing value/probs through construct.
  97. """
  98. net = BernoulliProb1()
  99. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  100. probs = Tensor([0.5], dtype=dtype.float32)
  101. ans = net(value, probs)
  102. assert isinstance(ans, Tensor)
  103. class BernoulliKl(nn.Cell):
  104. """
  105. Test class: kl_loss between Bernoulli distributions.
  106. """
  107. def __init__(self):
  108. super(BernoulliKl, self).__init__()
  109. self.b1 = msd.Bernoulli(0.7, dtype=dtype.int32)
  110. self.b2 = msd.Bernoulli(dtype=dtype.int32)
  111. def construct(self, probs_b, probs_a):
  112. kl1 = self.b1.kl_loss('Bernoulli', probs_b)
  113. kl2 = self.b2.kl_loss('Bernoulli', probs_b, probs_a)
  114. return kl1 + kl2
  115. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  116. def test_kl():
  117. """
  118. Test kl_loss function.
  119. """
  120. ber_net = BernoulliKl()
  121. probs_b = Tensor([0.3], dtype=dtype.float32)
  122. probs_a = Tensor([0.7], dtype=dtype.float32)
  123. ans = ber_net(probs_b, probs_a)
  124. assert isinstance(ans, Tensor)
  125. class BernoulliCrossEntropy(nn.Cell):
  126. """
  127. Test class: cross_entropy of Bernoulli distribution.
  128. """
  129. def __init__(self):
  130. super(BernoulliCrossEntropy, self).__init__()
  131. self.b1 = msd.Bernoulli(0.7, dtype=dtype.int32)
  132. self.b2 = msd.Bernoulli(dtype=dtype.int32)
  133. def construct(self, probs_b, probs_a):
  134. h1 = self.b1.cross_entropy('Bernoulli', probs_b)
  135. h2 = self.b2.cross_entropy('Bernoulli', probs_b, probs_a)
  136. return h1 + h2
  137. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  138. def test_cross_entropy():
  139. """
  140. Test cross_entropy between Bernoulli distributions.
  141. """
  142. net = BernoulliCrossEntropy()
  143. probs_b = Tensor([0.3], dtype=dtype.float32)
  144. probs_a = Tensor([0.7], dtype=dtype.float32)
  145. ans = net(probs_b, probs_a)
  146. assert isinstance(ans, Tensor)
  147. class BernoulliConstruct(nn.Cell):
  148. """
  149. Bernoulli distribution: going through construct.
  150. """
  151. def __init__(self):
  152. super(BernoulliConstruct, self).__init__()
  153. self.b = msd.Bernoulli(0.5, dtype=dtype.int32)
  154. self.b1 = msd.Bernoulli(dtype=dtype.int32)
  155. def construct(self, value, probs):
  156. prob = self.b('prob', value)
  157. prob1 = self.b('prob', value, probs)
  158. prob2 = self.b1('prob', value, probs)
  159. return prob + prob1 + prob2
  160. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  161. def test_bernoulli_construct():
  162. """
  163. Test probability function going through construct.
  164. """
  165. net = BernoulliConstruct()
  166. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  167. probs = Tensor([0.5], dtype=dtype.float32)
  168. ans = net(value, probs)
  169. assert isinstance(ans, Tensor)
  170. class BernoulliMean(nn.Cell):
  171. """
  172. Test class: basic mean/sd/var/mode/entropy function.
  173. """
  174. def __init__(self):
  175. super(BernoulliMean, self).__init__()
  176. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  177. def construct(self):
  178. mean = self.b.mean()
  179. return mean
  180. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  181. def test_mean():
  182. """
  183. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  184. """
  185. net = BernoulliMean()
  186. ans = net()
  187. assert isinstance(ans, Tensor)
  188. class BernoulliSd(nn.Cell):
  189. """
  190. Test class: basic mean/sd/var/mode/entropy function.
  191. """
  192. def __init__(self):
  193. super(BernoulliSd, self).__init__()
  194. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  195. def construct(self):
  196. sd = self.b.sd()
  197. return sd
  198. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  199. def test_sd():
  200. """
  201. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  202. """
  203. net = BernoulliSd()
  204. ans = net()
  205. assert isinstance(ans, Tensor)
  206. class BernoulliVar(nn.Cell):
  207. """
  208. Test class: basic mean/sd/var/mode/entropy function.
  209. """
  210. def __init__(self):
  211. super(BernoulliVar, self).__init__()
  212. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  213. def construct(self):
  214. var = self.b.var()
  215. return var
  216. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  217. def test_var():
  218. """
  219. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  220. """
  221. net = BernoulliVar()
  222. ans = net()
  223. assert isinstance(ans, Tensor)
  224. class BernoulliMode(nn.Cell):
  225. """
  226. Test class: basic mean/sd/var/mode/entropy function.
  227. """
  228. def __init__(self):
  229. super(BernoulliMode, self).__init__()
  230. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  231. def construct(self):
  232. mode = self.b.mode()
  233. return mode
  234. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  235. def test_mode():
  236. """
  237. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  238. """
  239. net = BernoulliMode()
  240. ans = net()
  241. assert isinstance(ans, Tensor)
  242. class BernoulliEntropy(nn.Cell):
  243. """
  244. Test class: basic mean/sd/var/mode/entropy function.
  245. """
  246. def __init__(self):
  247. super(BernoulliEntropy, self).__init__()
  248. self.b = msd.Bernoulli([0.3, 0.5], dtype=dtype.int32)
  249. def construct(self):
  250. entropy = self.b.entropy()
  251. return entropy
  252. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  253. def test_entropy():
  254. """
  255. Test mean/sd/var/mode/entropy functionality of Bernoulli distribution.
  256. """
  257. net = BernoulliEntropy()
  258. ans = net()
  259. assert isinstance(ans, Tensor)