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_logistic.py 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.logistic.
  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_logistic_shape_errpr():
  26. """
  27. Invalid shapes.
  28. """
  29. with pytest.raises(ValueError):
  30. msd.Logistic([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
  31. def test_type():
  32. with pytest.raises(TypeError):
  33. msd.Logistic(0., 1., dtype=dtype.int32)
  34. def test_name():
  35. with pytest.raises(TypeError):
  36. msd.Logistic(0., 1., name=1.0)
  37. def test_seed():
  38. with pytest.raises(TypeError):
  39. msd.Logistic(0., 1., seed='seed')
  40. def test_scale():
  41. with pytest.raises(ValueError):
  42. msd.Logistic(0., 0.)
  43. with pytest.raises(ValueError):
  44. msd.Logistic(0., -1.)
  45. def test_arguments():
  46. """
  47. args passing during initialization.
  48. """
  49. l = msd.Logistic()
  50. assert isinstance(l, msd.Distribution)
  51. l = msd.Logistic([3.0], [4.0], dtype=dtype.float32)
  52. assert isinstance(l, msd.Distribution)
  53. class LogisticProb(nn.Cell):
  54. """
  55. logistic distribution: initialize with loc/scale.
  56. """
  57. def __init__(self):
  58. super(LogisticProb, self).__init__()
  59. self.logistic = msd.Logistic(3.0, 4.0, dtype=dtype.float32)
  60. def construct(self, value):
  61. prob = self.logistic.prob(value)
  62. log_prob = self.logistic.log_prob(value)
  63. cdf = self.logistic.cdf(value)
  64. log_cdf = self.logistic.log_cdf(value)
  65. sf = self.logistic.survival_function(value)
  66. log_sf = self.logistic.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")
  69. def test_logistic_prob():
  70. """
  71. Test probability functions: passing value through construct.
  72. """
  73. net = LogisticProb()
  74. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  75. ans = net(value)
  76. assert isinstance(ans, Tensor)
  77. class LogisticProb1(nn.Cell):
  78. """
  79. logistic distribution: initialize without loc/scale.
  80. """
  81. def __init__(self):
  82. super(LogisticProb1, self).__init__()
  83. self.logistic = msd.Logistic()
  84. def construct(self, value, mu, s):
  85. prob = self.logistic.prob(value, mu, s)
  86. log_prob = self.logistic.log_prob(value, mu, s)
  87. cdf = self.logistic.cdf(value, mu, s)
  88. log_cdf = self.logistic.log_cdf(value, mu, s)
  89. sf = self.logistic.survival_function(value, mu, s)
  90. log_sf = self.logistic.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")
  93. def test_logistic_prob1():
  94. """
  95. Test probability functions: passing loc/scale, value through construct.
  96. """
  97. net = LogisticProb1()
  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. Should raise NotImplementedError.
  106. """
  107. def __init__(self):
  108. super(KL, self).__init__()
  109. self.logistic = msd.Logistic(3.0, 4.0)
  110. def construct(self, mu, s):
  111. kl = self.logistic.kl_loss('Logistic', mu, s)
  112. return kl
  113. class Crossentropy(nn.Cell):
  114. """
  115. Test cross entropy. Should raise NotImplementedError.
  116. """
  117. def __init__(self):
  118. super(Crossentropy, self).__init__()
  119. self.logistic = msd.Logistic(3.0, 4.0)
  120. def construct(self, mu, s):
  121. cross_entropy = self.logistic.cross_entropy('Logistic', mu, s)
  122. return cross_entropy
  123. class LogisticBasics(nn.Cell):
  124. """
  125. Test class: basic loc/scale function.
  126. """
  127. def __init__(self):
  128. super(LogisticBasics, self).__init__()
  129. self.logistic = msd.Logistic(3.0, 4.0, dtype=dtype.float32)
  130. def construct(self):
  131. mean = self.logistic.mean()
  132. sd = self.logistic.sd()
  133. mode = self.logistic.mode()
  134. entropy = self.logistic.entropy()
  135. return mean + sd + mode + entropy
  136. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  137. def test_bascis():
  138. """
  139. Test mean/sd/mode/entropy functionality of logistic.
  140. """
  141. net = LogisticBasics()
  142. ans = net()
  143. assert isinstance(ans, Tensor)
  144. mu = Tensor(1.0, dtype=dtype.float32)
  145. s = Tensor(1.0, dtype=dtype.float32)
  146. with pytest.raises(NotImplementedError):
  147. kl = KL()
  148. ans = kl(mu, s)
  149. with pytest.raises(NotImplementedError):
  150. crossentropy = Crossentropy()
  151. ans = crossentropy(mu, s)
  152. class LogisticConstruct(nn.Cell):
  153. """
  154. logistic distribution: going through construct.
  155. """
  156. def __init__(self):
  157. super(LogisticConstruct, self).__init__()
  158. self.logistic = msd.Logistic(3.0, 4.0)
  159. self.logistic1 = msd.Logistic()
  160. def construct(self, value, mu, s):
  161. prob = self.logistic('prob', value)
  162. prob1 = self.logistic('prob', value, mu, s)
  163. prob2 = self.logistic1('prob', value, mu, s)
  164. return prob + prob1 + prob2
  165. @pytest.mark.skipif(skip_flag, reason="not support running in CPU")
  166. def test_logistic_construct():
  167. """
  168. Test probability function going through construct.
  169. """
  170. net = LogisticConstruct()
  171. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  172. mu = Tensor([0.0], dtype=dtype.float32)
  173. s = Tensor([1.0], dtype=dtype.float32)
  174. ans = net(value, mu, s)
  175. assert isinstance(ans, Tensor)