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_cauchy.py 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.cauchy.
  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") != "Ascend"
  25. def test_cauchy_shape_errpr():
  26. """
  27. Invalid shapes.
  28. """
  29. with pytest.raises(ValueError):
  30. msd.Cauchy([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
  31. def test_type():
  32. with pytest.raises(TypeError):
  33. msd.Cauchy(0., 1., dtype=dtype.int32)
  34. def test_name():
  35. with pytest.raises(TypeError):
  36. msd.Cauchy(0., 1., name=1.0)
  37. def test_seed():
  38. with pytest.raises(TypeError):
  39. msd.Cauchy(0., 1., seed='seed')
  40. def test_scale():
  41. with pytest.raises(ValueError):
  42. msd.Cauchy(0., 0.)
  43. with pytest.raises(ValueError):
  44. msd.Cauchy(0., -1.)
  45. def test_arguments():
  46. """
  47. args passing during initialization.
  48. """
  49. l1 = msd.Cauchy()
  50. assert isinstance(l1, msd.Distribution)
  51. l2 = msd.Cauchy([3.0], [4.0], dtype=dtype.float32)
  52. assert isinstance(l2, msd.Distribution)
  53. class CauchyProb(nn.Cell):
  54. """
  55. Cauchy distribution: initialize with loc/scale.
  56. """
  57. def __init__(self):
  58. super(CauchyProb, self).__init__()
  59. self.cauchy = msd.Cauchy(3.0, 4.0, dtype=dtype.float32)
  60. def construct(self, value):
  61. prob = self.cauchy.prob(value)
  62. log_prob = self.cauchy.log_prob(value)
  63. cdf = self.cauchy.cdf(value)
  64. log_cdf = self.cauchy.log_cdf(value)
  65. sf = self.cauchy.survival_function(value)
  66. log_sf = self.cauchy.log_survival(value)
  67. return prob + log_prob + cdf + log_cdf + sf + log_sf
  68. @pytest.mark.skipif(skip_flag, reason="not support running in CPU and GPU")
  69. def test_cauchy_prob():
  70. """
  71. Test probability functions: passing value through construct.
  72. """
  73. net = CauchyProb()
  74. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  75. ans = net(value)
  76. assert isinstance(ans, Tensor)
  77. class CauchyProb1(nn.Cell):
  78. """
  79. Cauchy distribution: initialize without loc/scale.
  80. """
  81. def __init__(self):
  82. super(CauchyProb1, self).__init__()
  83. self.cauchy = msd.Cauchy()
  84. def construct(self, value, mu, s):
  85. prob = self.cauchy.prob(value, mu, s)
  86. log_prob = self.cauchy.log_prob(value, mu, s)
  87. cdf = self.cauchy.cdf(value, mu, s)
  88. log_cdf = self.cauchy.log_cdf(value, mu, s)
  89. sf = self.cauchy.survival_function(value, mu, s)
  90. log_sf = self.cauchy.log_survival(value, mu, s)
  91. return prob + log_prob + cdf + log_cdf + sf + log_sf
  92. @pytest.mark.skipif(skip_flag, reason="not support running in CPU and GPU")
  93. def test_cauchy_prob1():
  94. """
  95. Test probability functions: passing loc/scale, value through construct.
  96. """
  97. net = CauchyProb1()
  98. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  99. mu = Tensor([0.0], dtype=dtype.float32)
  100. s = Tensor([1.0], dtype=dtype.float32)
  101. ans = net(value, mu, s)
  102. assert isinstance(ans, Tensor)
  103. class KL(nn.Cell):
  104. """
  105. Test kl_loss and cross entropy.
  106. """
  107. def __init__(self):
  108. super(KL, self).__init__()
  109. self.cauchy = msd.Cauchy(3.0, 4.0)
  110. self.cauchy1 = msd.Cauchy()
  111. def construct(self, mu, s, mu_a, s_a):
  112. kl = self.cauchy.kl_loss('Cauchy', mu, s)
  113. kl1 = self.cauchy1.kl_loss('Cauchy', mu, s, mu_a, s_a)
  114. cross_entropy = self.cauchy.cross_entropy('Cauchy', mu, s)
  115. cross_entropy1 = self.cauchy.cross_entropy('Cauchy', mu, s, mu_a, s_a)
  116. return kl + kl1 + cross_entropy + cross_entropy1
  117. @pytest.mark.skipif(skip_flag, reason="not support running in CPU and GPU")
  118. def test_kl_cross_entropy():
  119. """
  120. Test kl_loss and cross_entropy.
  121. """
  122. net = KL()
  123. mu = Tensor([0.0], dtype=dtype.float32)
  124. s = Tensor([1.0], dtype=dtype.float32)
  125. mu_a = Tensor([0.0], dtype=dtype.float32)
  126. s_a = Tensor([1.0], dtype=dtype.float32)
  127. ans = net(mu, s, mu_a, s_a)
  128. assert isinstance(ans, Tensor)
  129. class CauchyBasics(nn.Cell):
  130. """
  131. Test class: basic loc/scale function.
  132. """
  133. def __init__(self):
  134. super(CauchyBasics, self).__init__()
  135. self.cauchy = msd.Cauchy(3.0, 4.0, dtype=dtype.float32)
  136. def construct(self):
  137. mode = self.cauchy.mode()
  138. entropy = self.cauchy.entropy()
  139. return mode + entropy
  140. class CauchyMean(nn.Cell):
  141. """
  142. Test class: basic loc/scale function.
  143. """
  144. def __init__(self):
  145. super(CauchyMean, self).__init__()
  146. self.cauchy = msd.Cauchy(3.0, 4.0, dtype=dtype.float32)
  147. def construct(self):
  148. return self.cauchy.mean()
  149. class CauchyVar(nn.Cell):
  150. """
  151. Test class: basic loc/scale function.
  152. """
  153. def __init__(self):
  154. super(CauchyVar, self).__init__()
  155. self.cauchy = msd.Cauchy(3.0, 4.0, dtype=dtype.float32)
  156. def construct(self):
  157. return self.cauchy.var()
  158. class CauchySd(nn.Cell):
  159. """
  160. Test class: basic loc/scale function.
  161. """
  162. def __init__(self):
  163. super(CauchySd, self).__init__()
  164. self.cauchy = msd.Cauchy(3.0, 4.0, dtype=dtype.float32)
  165. def construct(self):
  166. return self.cauchy.sd()
  167. @pytest.mark.skipif(skip_flag, reason="not support running in CPU and GPU")
  168. def test_bascis():
  169. """
  170. Test mean/sd/var/mode/entropy functionality of Cauchy.
  171. """
  172. net = CauchyBasics()
  173. ans = net()
  174. assert isinstance(ans, Tensor)
  175. with pytest.raises(ValueError):
  176. net = CauchyMean()
  177. ans = net()
  178. with pytest.raises(ValueError):
  179. net = CauchyVar()
  180. ans = net()
  181. with pytest.raises(ValueError):
  182. net = CauchySd()
  183. ans = net()
  184. class CauchyConstruct(nn.Cell):
  185. """
  186. Cauchy distribution: going through construct.
  187. """
  188. def __init__(self):
  189. super(CauchyConstruct, self).__init__()
  190. self.cauchy = msd.Cauchy(3.0, 4.0)
  191. self.cauchy1 = msd.Cauchy()
  192. def construct(self, value, mu, s):
  193. prob = self.cauchy('prob', value)
  194. prob1 = self.cauchy('prob', value, mu, s)
  195. prob2 = self.cauchy1('prob', value, mu, s)
  196. return prob + prob1 + prob2
  197. @pytest.mark.skipif(skip_flag, reason="not support running in CPU and GPU")
  198. def test_cauchy_construct():
  199. """
  200. Test probability function going through construct.
  201. """
  202. net = CauchyConstruct()
  203. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  204. mu = Tensor([0.0], dtype=dtype.float32)
  205. s = Tensor([1.0], dtype=dtype.float32)
  206. ans = net(value, mu, s)
  207. assert isinstance(ans, Tensor)