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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_rate():
  32. """
  33. Invalid rate.
  34. """
  35. with pytest.raises(ValueError):
  36. msd.Exponential([-0.1], dtype=dtype.float32)
  37. with pytest.raises(ValueError):
  38. msd.Exponential([0.0], dtype=dtype.float32)
  39. class ExponentialProb(nn.Cell):
  40. """
  41. Exponential distribution: initialize with rate.
  42. """
  43. def __init__(self):
  44. super(ExponentialProb, self).__init__()
  45. self.e = msd.Exponential(0.5, dtype=dtype.float32)
  46. def construct(self, value):
  47. prob = self.e('prob', value)
  48. log_prob = self.e('log_prob', value)
  49. cdf = self.e('cdf', value)
  50. log_cdf = self.e('log_cdf', value)
  51. sf = self.e('survival_function', value)
  52. log_sf = self.e('log_survival', value)
  53. return prob + log_prob + cdf + log_cdf + sf + log_sf
  54. def test_exponential_prob():
  55. """
  56. Test probability functions: passing value through construct.
  57. """
  58. net = ExponentialProb()
  59. value = Tensor([0.2, 0.3, 5.0, 2, 3.9], dtype=dtype.float32)
  60. ans = net(value)
  61. assert isinstance(ans, Tensor)
  62. class ExponentialProb1(nn.Cell):
  63. """
  64. Exponential distribution: initialize without rate.
  65. """
  66. def __init__(self):
  67. super(ExponentialProb1, self).__init__()
  68. self.e = msd.Exponential(dtype=dtype.float32)
  69. def construct(self, value, rate):
  70. prob = self.e('prob', value, rate)
  71. log_prob = self.e('log_prob', value, rate)
  72. cdf = self.e('cdf', value, rate)
  73. log_cdf = self.e('log_cdf', value, rate)
  74. sf = self.e('survival_function', value, rate)
  75. log_sf = self.e('log_survival', value, rate)
  76. return prob + log_prob + cdf + log_cdf + sf + log_sf
  77. def test_exponential_prob1():
  78. """
  79. Test probability functions: passing value/rate through construct.
  80. """
  81. net = ExponentialProb1()
  82. value = Tensor([0.2, 0.9, 1, 2, 3], dtype=dtype.float32)
  83. rate = Tensor([0.5], dtype=dtype.float32)
  84. ans = net(value, rate)
  85. assert isinstance(ans, Tensor)
  86. class ExponentialKl(nn.Cell):
  87. """
  88. Test class: kl_loss between Exponential distributions.
  89. """
  90. def __init__(self):
  91. super(ExponentialKl, self).__init__()
  92. self.e1 = msd.Exponential(0.7, dtype=dtype.float32)
  93. self.e2 = msd.Exponential(dtype=dtype.float32)
  94. def construct(self, rate_b, rate_a):
  95. kl1 = self.e1('kl_loss', 'Exponential', rate_b)
  96. kl2 = self.e2('kl_loss', 'Exponential', rate_b, rate_a)
  97. return kl1 + kl2
  98. def test_kl():
  99. """
  100. Test kl_loss function.
  101. """
  102. net = ExponentialKl()
  103. rate_b = Tensor([0.3], dtype=dtype.float32)
  104. rate_a = Tensor([0.7], dtype=dtype.float32)
  105. ans = net(rate_b, rate_a)
  106. assert isinstance(ans, Tensor)
  107. class ExponentialCrossEntropy(nn.Cell):
  108. """
  109. Test class: cross_entropy of Exponential distribution.
  110. """
  111. def __init__(self):
  112. super(ExponentialCrossEntropy, self).__init__()
  113. self.e1 = msd.Exponential(0.3, dtype=dtype.float32)
  114. self.e2 = msd.Exponential(dtype=dtype.float32)
  115. def construct(self, rate_b, rate_a):
  116. h1 = self.e1('cross_entropy', 'Exponential', rate_b)
  117. h2 = self.e2('cross_entropy', 'Exponential', rate_b, rate_a)
  118. return h1 + h2
  119. def test_cross_entropy():
  120. """
  121. Test cross_entropy between Exponential distributions.
  122. """
  123. net = ExponentialCrossEntropy()
  124. rate_b = Tensor([0.3], dtype=dtype.float32)
  125. rate_a = Tensor([0.7], dtype=dtype.float32)
  126. ans = net(rate_b, rate_a)
  127. assert isinstance(ans, Tensor)
  128. class ExponentialBasics(nn.Cell):
  129. """
  130. Test class: basic mean/sd/mode/entropy function.
  131. """
  132. def __init__(self):
  133. super(ExponentialBasics, self).__init__()
  134. self.e = msd.Exponential([0.3, 0.5], dtype=dtype.float32)
  135. def construct(self):
  136. mean = self.e('mean')
  137. sd = self.e('sd')
  138. var = self.e('var')
  139. mode = self.e('mode')
  140. entropy = self.e('entropy')
  141. return mean + sd + var + mode + entropy
  142. def test_bascis():
  143. """
  144. Test mean/sd/var/mode/entropy functionality of Exponential distribution.
  145. """
  146. net = ExponentialBasics()
  147. ans = net()
  148. assert isinstance(ans, Tensor)