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.3 kB

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