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

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