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_exponential.py 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.Exponential.
  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. e = msd.Exponential()
  30. assert isinstance(e, msd.Distribution)
  31. e = msd.Exponential([0.1, 0.3, 0.5, 1.0], dtype=dtype.float32)
  32. assert isinstance(e, msd.Distribution)
  33. def test_type():
  34. with pytest.raises(TypeError):
  35. msd.Exponential([0.1], dtype=dtype.int32)
  36. def test_name():
  37. with pytest.raises(TypeError):
  38. msd.Exponential([0.1], name=1.0)
  39. def test_seed():
  40. with pytest.raises(TypeError):
  41. msd.Exponential([0.1], seed='seed')
  42. def test_rate():
  43. """
  44. Invalid rate.
  45. """
  46. with pytest.raises(ValueError):
  47. msd.Exponential([-0.1], dtype=dtype.float32)
  48. with pytest.raises(ValueError):
  49. msd.Exponential([0.0], dtype=dtype.float32)
  50. class ExponentialProb(nn.Cell):
  51. """
  52. Exponential distribution: initialize with rate.
  53. """
  54. def __init__(self):
  55. super(ExponentialProb, self).__init__()
  56. self.e = msd.Exponential(0.5, dtype=dtype.float32)
  57. def construct(self, value):
  58. prob = self.e.prob(value)
  59. log_prob = self.e.log_prob(value)
  60. cdf = self.e.cdf(value)
  61. log_cdf = self.e.log_cdf(value)
  62. sf = self.e.survival_function(value)
  63. log_sf = self.e.log_survival(value)
  64. return prob + log_prob + cdf + log_cdf + sf + log_sf
  65. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  66. def test_exponential_prob():
  67. """
  68. Test probability functions: passing value through construct.
  69. """
  70. net = ExponentialProb()
  71. value = Tensor([0.2, 0.3, 5.0, 2, 3.9], dtype=dtype.float32)
  72. ans = net(value)
  73. assert isinstance(ans, Tensor)
  74. class ExponentialProb1(nn.Cell):
  75. """
  76. Exponential distribution: initialize without rate.
  77. """
  78. def __init__(self):
  79. super(ExponentialProb1, self).__init__()
  80. self.e = msd.Exponential(dtype=dtype.float32)
  81. def construct(self, value, rate):
  82. prob = self.e.prob(value, rate)
  83. log_prob = self.e.log_prob(value, rate)
  84. cdf = self.e.cdf(value, rate)
  85. log_cdf = self.e.log_cdf(value, rate)
  86. sf = self.e.survival_function(value, rate)
  87. log_sf = self.e.log_survival(value, rate)
  88. return prob + log_prob + cdf + log_cdf + sf + log_sf
  89. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  90. def test_exponential_prob1():
  91. """
  92. Test probability functions: passing value/rate through construct.
  93. """
  94. net = ExponentialProb1()
  95. value = Tensor([0.2, 0.9, 1, 2, 3], dtype=dtype.float32)
  96. rate = Tensor([0.5], dtype=dtype.float32)
  97. ans = net(value, rate)
  98. assert isinstance(ans, Tensor)
  99. class ExponentialKl(nn.Cell):
  100. """
  101. Test class: kl_loss between Exponential distributions.
  102. """
  103. def __init__(self):
  104. super(ExponentialKl, self).__init__()
  105. self.e1 = msd.Exponential(0.7, dtype=dtype.float32)
  106. self.e2 = msd.Exponential(dtype=dtype.float32)
  107. def construct(self, rate_b, rate_a):
  108. kl1 = self.e1.kl_loss('Exponential', rate_b)
  109. kl2 = self.e2.kl_loss('Exponential', rate_b, rate_a)
  110. return kl1 + kl2
  111. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  112. def test_kl():
  113. """
  114. Test kl_loss function.
  115. """
  116. net = ExponentialKl()
  117. rate_b = Tensor([0.3], dtype=dtype.float32)
  118. rate_a = Tensor([0.7], dtype=dtype.float32)
  119. ans = net(rate_b, rate_a)
  120. assert isinstance(ans, Tensor)
  121. class ExponentialCrossEntropy(nn.Cell):
  122. """
  123. Test class: cross_entropy of Exponential distribution.
  124. """
  125. def __init__(self):
  126. super(ExponentialCrossEntropy, self).__init__()
  127. self.e1 = msd.Exponential(0.3, dtype=dtype.float32)
  128. self.e2 = msd.Exponential(dtype=dtype.float32)
  129. def construct(self, rate_b, rate_a):
  130. h1 = self.e1.cross_entropy('Exponential', rate_b)
  131. h2 = self.e2.cross_entropy('Exponential', rate_b, rate_a)
  132. return h1 + h2
  133. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  134. def test_cross_entropy():
  135. """
  136. Test cross_entropy between Exponential distributions.
  137. """
  138. net = ExponentialCrossEntropy()
  139. rate_b = Tensor([0.3], dtype=dtype.float32)
  140. rate_a = Tensor([0.7], dtype=dtype.float32)
  141. ans = net(rate_b, rate_a)
  142. assert isinstance(ans, Tensor)
  143. class ExponentialBasics(nn.Cell):
  144. """
  145. Test class: basic mean/sd/mode/entropy function.
  146. """
  147. def __init__(self):
  148. super(ExponentialBasics, self).__init__()
  149. self.e = msd.Exponential([0.3, 0.5], dtype=dtype.float32)
  150. def construct(self):
  151. mean = self.e.mean()
  152. sd = self.e.sd()
  153. var = self.e.var()
  154. mode = self.e.mode()
  155. entropy = self.e.entropy()
  156. return mean + sd + var + mode + entropy
  157. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  158. def test_bascis():
  159. """
  160. Test mean/sd/var/mode/entropy functionality of Exponential distribution.
  161. """
  162. net = ExponentialBasics()
  163. ans = net()
  164. assert isinstance(ans, Tensor)
  165. class ExpConstruct(nn.Cell):
  166. """
  167. Exponential distribution: going through construct.
  168. """
  169. def __init__(self):
  170. super(ExpConstruct, self).__init__()
  171. self.e = msd.Exponential(0.5, dtype=dtype.float32)
  172. self.e1 = msd.Exponential(dtype=dtype.float32)
  173. def construct(self, value, rate):
  174. prob = self.e('prob', value)
  175. prob1 = self.e('prob', value, rate)
  176. prob2 = self.e1('prob', value, rate)
  177. return prob + prob1 + prob2
  178. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  179. def test_exp_construct():
  180. """
  181. Test probability function going through construct.
  182. """
  183. net = ExpConstruct()
  184. value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32)
  185. probs = Tensor([0.5], dtype=dtype.float32)
  186. ans = net(value, probs)
  187. assert isinstance(ans, Tensor)