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_beta.py 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.Gamma.
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.nn as nn
  21. import mindspore.nn.probability.distribution as msd
  22. from mindspore import dtype
  23. from mindspore import Tensor
  24. def test_gamma_shape_errpr():
  25. """
  26. Invalid shapes.
  27. """
  28. with pytest.raises(ValueError):
  29. msd.Gamma([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
  30. def test_type():
  31. with pytest.raises(TypeError):
  32. msd.Gamma([0.], [1.], dtype=dtype.int32)
  33. def test_name():
  34. with pytest.raises(TypeError):
  35. msd.Gamma([0.], [1.], name=1.0)
  36. def test_seed():
  37. with pytest.raises(TypeError):
  38. msd.Gamma([0.], [1.], seed='seed')
  39. def test_concentration1():
  40. with pytest.raises(ValueError):
  41. msd.Gamma([0.], [1.])
  42. with pytest.raises(ValueError):
  43. msd.Gamma([-1.], [1.])
  44. def test_concentration0():
  45. with pytest.raises(ValueError):
  46. msd.Gamma([1.], [0.])
  47. with pytest.raises(ValueError):
  48. msd.Gamma([1.], [-1.])
  49. def test_scalar():
  50. with pytest.raises(TypeError):
  51. msd.Gamma(3., [4.])
  52. with pytest.raises(TypeError):
  53. msd.Gamma([3.], -4.)
  54. def test_arguments():
  55. """
  56. args passing during initialization.
  57. """
  58. g = msd.Gamma()
  59. assert isinstance(g, msd.Distribution)
  60. g = msd.Gamma([3.0], [4.0], dtype=dtype.float32)
  61. assert isinstance(g, msd.Distribution)
  62. class GammaProb(nn.Cell):
  63. """
  64. Gamma distribution: initialize with concentration1/concentration0.
  65. """
  66. def __init__(self):
  67. super(GammaProb, self).__init__()
  68. self.gamma = msd.Gamma([3.0, 4.0], [1.0, 1.0], dtype=dtype.float32)
  69. def construct(self, value):
  70. prob = self.gamma.prob(value)
  71. log_prob = self.gamma.log_prob(value)
  72. return prob + log_prob
  73. def test_gamma_prob():
  74. """
  75. Test probability functions: passing value through construct.
  76. """
  77. net = GammaProb()
  78. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  79. ans = net(value)
  80. assert isinstance(ans, Tensor)
  81. class GammaProb1(nn.Cell):
  82. """
  83. Gamma distribution: initialize without concentration1/concentration0.
  84. """
  85. def __init__(self):
  86. super(GammaProb1, self).__init__()
  87. self.gamma = msd.Gamma()
  88. def construct(self, value, concentration1, concentration0):
  89. prob = self.gamma.prob(value, concentration1, concentration0)
  90. log_prob = self.gamma.log_prob(value, concentration1, concentration0)
  91. return prob + log_prob
  92. def test_gamma_prob1():
  93. """
  94. Test probability functions: passing concentration1/concentration0, value through construct.
  95. """
  96. net = GammaProb1()
  97. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  98. concentration1 = Tensor([2.0, 3.0], dtype=dtype.float32)
  99. concentration0 = Tensor([1.0], dtype=dtype.float32)
  100. ans = net(value, concentration1, concentration0)
  101. assert isinstance(ans, Tensor)
  102. class GammaKl(nn.Cell):
  103. """
  104. Test class: kl_loss of Gamma distribution.
  105. """
  106. def __init__(self):
  107. super(GammaKl, self).__init__()
  108. self.g1 = msd.Gamma(np.array([3.0]), np.array([4.0]), dtype=dtype.float32)
  109. self.g2 = msd.Gamma(dtype=dtype.float32)
  110. def construct(self, concentration1_b, concentration0_b, concentration1_a, concentration0_a):
  111. kl1 = self.g1.kl_loss('Gamma', concentration1_b, concentration0_b)
  112. kl2 = self.g2.kl_loss('Gamma', concentration1_b, concentration0_b, concentration1_a, concentration0_a)
  113. return kl1 + kl2
  114. def test_kl():
  115. """
  116. Test kl_loss.
  117. """
  118. net = GammaKl()
  119. concentration1_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  120. concentration0_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  121. concentration1_a = Tensor(np.array([2.0]).astype(np.float32), dtype=dtype.float32)
  122. concentration0_a = Tensor(np.array([3.0]).astype(np.float32), dtype=dtype.float32)
  123. ans = net(concentration1_b, concentration0_b, concentration1_a, concentration0_a)
  124. assert isinstance(ans, Tensor)
  125. class GammaCrossEntropy(nn.Cell):
  126. """
  127. Test class: cross_entropy of Gamma distribution.
  128. """
  129. def __init__(self):
  130. super(GammaCrossEntropy, self).__init__()
  131. self.g1 = msd.Gamma(np.array([3.0]), np.array([4.0]), dtype=dtype.float32)
  132. self.g2 = msd.Gamma(dtype=dtype.float32)
  133. def construct(self, concentration1_b, concentration0_b, concentration1_a, concentration0_a):
  134. h1 = self.g1.cross_entropy('Gamma', concentration1_b, concentration0_b)
  135. h2 = self.g2.cross_entropy('Gamma', concentration1_b, concentration0_b, concentration1_a, concentration0_a)
  136. return h1 + h2
  137. def test_cross_entropy():
  138. """
  139. Test cross entropy between Gamma distributions.
  140. """
  141. net = GammaCrossEntropy()
  142. concentration1_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  143. concentration0_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  144. concentration1_a = Tensor(np.array([2.0]).astype(np.float32), dtype=dtype.float32)
  145. concentration0_a = Tensor(np.array([3.0]).astype(np.float32), dtype=dtype.float32)
  146. ans = net(concentration1_b, concentration0_b, concentration1_a, concentration0_a)
  147. assert isinstance(ans, Tensor)
  148. class GammaBasics(nn.Cell):
  149. """
  150. Test class: basic mean/sd function.
  151. """
  152. def __init__(self):
  153. super(GammaBasics, self).__init__()
  154. self.g = msd.Gamma(np.array([3.0, 4.0]), np.array([4.0, 6.0]), dtype=dtype.float32)
  155. def construct(self):
  156. mean = self.g.mean()
  157. sd = self.g.sd()
  158. mode = self.g.mode()
  159. return mean + sd + mode
  160. def test_bascis():
  161. """
  162. Test mean/sd/mode/entropy functionality of Gamma.
  163. """
  164. net = GammaBasics()
  165. ans = net()
  166. assert isinstance(ans, Tensor)
  167. class GammaConstruct(nn.Cell):
  168. """
  169. Gamma distribution: going through construct.
  170. """
  171. def __init__(self):
  172. super(GammaConstruct, self).__init__()
  173. self.gamma = msd.Gamma([3.0], [4.0])
  174. self.gamma1 = msd.Gamma()
  175. def construct(self, value, concentration1, concentration0):
  176. prob = self.gamma('prob', value)
  177. prob1 = self.gamma('prob', value, concentration1, concentration0)
  178. prob2 = self.gamma1('prob', value, concentration1, concentration0)
  179. return prob + prob1 + prob2
  180. def test_gamma_construct():
  181. """
  182. Test probability function going through construct.
  183. """
  184. net = GammaConstruct()
  185. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  186. concentration1 = Tensor([0.0], dtype=dtype.float32)
  187. concentration0 = Tensor([1.0], dtype=dtype.float32)
  188. ans = net(value, concentration1, concentration0)
  189. assert isinstance(ans, Tensor)