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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Copyright 2019 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. """test cases for Logistic distribution"""
  16. import numpy as np
  17. from scipy import stats
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. import mindspore.nn.probability.distribution as msd
  21. from mindspore import Tensor
  22. from mindspore import dtype
  23. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  24. class Prob(nn.Cell):
  25. """
  26. Test class: probability of Logistic distribution.
  27. """
  28. def __init__(self):
  29. super(Prob, self).__init__()
  30. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  31. def construct(self, x_):
  32. return self.l.prob(x_)
  33. def test_pdf():
  34. """
  35. Test pdf.
  36. """
  37. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  38. expect_pdf = logistic_benchmark.pdf([1.0, 2.0]).astype(np.float32)
  39. pdf = Prob()
  40. output = pdf(Tensor([1.0, 2.0], dtype=dtype.float32))
  41. tol = 1e-6
  42. assert (np.abs(output.asnumpy() - expect_pdf) < tol).all()
  43. class LogProb(nn.Cell):
  44. """
  45. Test class: log probability of Logistic distribution.
  46. """
  47. def __init__(self):
  48. super(LogProb, self).__init__()
  49. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  50. def construct(self, x_):
  51. return self.l.log_prob(x_)
  52. def test_log_likelihood():
  53. """
  54. Test log_pdf.
  55. """
  56. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  57. expect_logpdf = logistic_benchmark.logpdf([1.0, 2.0]).astype(np.float32)
  58. logprob = LogProb()
  59. output = logprob(Tensor([1.0, 2.0], dtype=dtype.float32))
  60. tol = 1e-6
  61. assert (np.abs(output.asnumpy() - expect_logpdf) < tol).all()
  62. class Basics(nn.Cell):
  63. """
  64. Test class: mean/sd/mode of Logistic distribution.
  65. """
  66. def __init__(self):
  67. super(Basics, self).__init__()
  68. self.l = msd.Logistic(np.array([3.0]), np.array([2.0, 4.0]), dtype=dtype.float32)
  69. def construct(self):
  70. return self.l.mean(), self.l.sd(), self.l.mode()
  71. def test_basics():
  72. """
  73. Test mean/standard deviation/mode.
  74. """
  75. basics = Basics()
  76. mean, sd, mode = basics()
  77. expect_mean = [3.0, 3.0]
  78. expect_sd = np.pi * np.array([2.0, 4.0]) / np.sqrt(np.array([3.0]))
  79. tol = 1e-6
  80. assert (np.abs(mean.asnumpy() - expect_mean) < tol).all()
  81. assert (np.abs(mode.asnumpy() - expect_mean) < tol).all()
  82. assert (np.abs(sd.asnumpy() - expect_sd) < tol).all()
  83. class Sampling(nn.Cell):
  84. """
  85. Test class: sample of Logistic distribution.
  86. """
  87. def __init__(self, shape, seed=0):
  88. super(Sampling, self).__init__()
  89. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), seed=seed, dtype=dtype.float32)
  90. self.shape = shape
  91. def construct(self, mean=None, sd=None):
  92. return self.l.sample(self.shape, mean, sd)
  93. def test_sample():
  94. """
  95. Test sample.
  96. """
  97. shape = (2, 3)
  98. seed = 10
  99. mean = Tensor([2.0], dtype=dtype.float32)
  100. sd = Tensor([2.0, 2.0, 2.0], dtype=dtype.float32)
  101. sample = Sampling(shape, seed=seed)
  102. output = sample(mean, sd)
  103. assert output.shape == (2, 3, 3)
  104. class CDF(nn.Cell):
  105. """
  106. Test class: cdf of Logistic distribution.
  107. """
  108. def __init__(self):
  109. super(CDF, self).__init__()
  110. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  111. def construct(self, x_):
  112. return self.l.cdf(x_)
  113. def test_cdf():
  114. """
  115. Test cdf.
  116. """
  117. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  118. expect_cdf = logistic_benchmark.cdf([1.0, 2.0]).astype(np.float32)
  119. cdf = CDF()
  120. output = cdf(Tensor([1.0, 2.0], dtype=dtype.float32))
  121. tol = 2e-5
  122. assert (np.abs(output.asnumpy() - expect_cdf) < tol).all()
  123. class LogCDF(nn.Cell):
  124. """
  125. Test class: log_cdf of Logistic distribution.
  126. """
  127. def __init__(self):
  128. super(LogCDF, self).__init__()
  129. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  130. def construct(self, x_):
  131. return self.l.log_cdf(x_)
  132. def test_log_cdf():
  133. """
  134. Test log cdf.
  135. """
  136. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  137. expect_logcdf = logistic_benchmark.logcdf([1.0, 2.0]).astype(np.float32)
  138. logcdf = LogCDF()
  139. output = logcdf(Tensor([1.0, 2.0], dtype=dtype.float32))
  140. tol = 5e-5
  141. assert (np.abs(output.asnumpy() - expect_logcdf) < tol).all()
  142. class SF(nn.Cell):
  143. """
  144. Test class: survival function of Logistic distribution.
  145. """
  146. def __init__(self):
  147. super(SF, self).__init__()
  148. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  149. def construct(self, x_):
  150. return self.l.survival_function(x_)
  151. def test_survival():
  152. """
  153. Test log_survival.
  154. """
  155. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  156. expect_survival = logistic_benchmark.sf([1.0, 2.0]).astype(np.float32)
  157. survival_function = SF()
  158. output = survival_function(Tensor([1.0, 2.0], dtype=dtype.float32))
  159. tol = 2e-5
  160. assert (np.abs(output.asnumpy() - expect_survival) < tol).all()
  161. class LogSF(nn.Cell):
  162. """
  163. Test class: log survival function of Logistic distribution.
  164. """
  165. def __init__(self):
  166. super(LogSF, self).__init__()
  167. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  168. def construct(self, x_):
  169. return self.l.log_survival(x_)
  170. def test_log_survival():
  171. """
  172. Test log_survival.
  173. """
  174. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  175. expect_log_survival = logistic_benchmark.logsf([1.0, 2.0]).astype(np.float32)
  176. log_survival = LogSF()
  177. output = log_survival(Tensor([1.0, 2.0], dtype=dtype.float32))
  178. tol = 2e-5
  179. assert (np.abs(output.asnumpy() - expect_log_survival) < tol).all()
  180. class EntropyH(nn.Cell):
  181. """
  182. Test class: entropy of Logistic distribution.
  183. """
  184. def __init__(self):
  185. super(EntropyH, self).__init__()
  186. self.l = msd.Logistic(np.array([3.0]), np.array([[2.0], [4.0]]), dtype=dtype.float32)
  187. def construct(self):
  188. return self.l.entropy()
  189. def test_entropy():
  190. """
  191. Test entropy.
  192. """
  193. logistic_benchmark = stats.logistic(np.array([3.0]), np.array([[2.0], [4.0]]))
  194. expect_entropy = logistic_benchmark.entropy().astype(np.float32)
  195. entropy = EntropyH()
  196. output = entropy()
  197. tol = 1e-6
  198. assert (np.abs(output.asnumpy() - expect_entropy) < tol).all()