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_normal.py 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.Normal.
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.nn as nn
  21. import mindspore.nn.probability.distribution as msd
  22. from mindspore import dtype
  23. from mindspore import Tensor
  24. def test_normal_shape_errpr():
  25. """
  26. Invalid shapes.
  27. """
  28. with pytest.raises(ValueError):
  29. msd.Normal([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
  30. def test_type():
  31. with pytest.raises(TypeError):
  32. msd.Normal(0., 1., dtype=dtype.int32)
  33. def test_name():
  34. with pytest.raises(TypeError):
  35. msd.Normal(0., 1., name=1.0)
  36. def test_seed():
  37. with pytest.raises(TypeError):
  38. msd.Normal(0., 1., seed='seed')
  39. def test_sd():
  40. with pytest.raises(ValueError):
  41. msd.Normal(0., 0.)
  42. with pytest.raises(ValueError):
  43. msd.Normal(0., -1.)
  44. def test_arguments():
  45. """
  46. args passing during initialization.
  47. """
  48. n = msd.Normal()
  49. assert isinstance(n, msd.Distribution)
  50. n = msd.Normal([3.0], [4.0], dtype=dtype.float32)
  51. assert isinstance(n, msd.Distribution)
  52. class NormalProb(nn.Cell):
  53. """
  54. Normal distribution: initialize with mean/sd.
  55. """
  56. def __init__(self):
  57. super(NormalProb, self).__init__()
  58. self.normal = msd.Normal(3.0, 4.0, dtype=dtype.float32)
  59. def construct(self, value):
  60. prob = self.normal.prob(value)
  61. log_prob = self.normal.log_prob(value)
  62. cdf = self.normal.cdf(value)
  63. log_cdf = self.normal.log_cdf(value)
  64. sf = self.normal.survival_function(value)
  65. log_sf = self.normal.log_survival(value)
  66. return prob + log_prob + cdf + log_cdf + sf + log_sf
  67. def test_normal_prob():
  68. """
  69. Test probability functions: passing value through construct.
  70. """
  71. net = NormalProb()
  72. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  73. ans = net(value)
  74. assert isinstance(ans, Tensor)
  75. class NormalProb1(nn.Cell):
  76. """
  77. Normal distribution: initialize without mean/sd.
  78. """
  79. def __init__(self):
  80. super(NormalProb1, self).__init__()
  81. self.normal = msd.Normal()
  82. def construct(self, value, mean, sd):
  83. prob = self.normal.prob(value, mean, sd)
  84. log_prob = self.normal.log_prob(value, mean, sd)
  85. cdf = self.normal.cdf(value, mean, sd)
  86. log_cdf = self.normal.log_cdf(value, mean, sd)
  87. sf = self.normal.survival_function(value, mean, sd)
  88. log_sf = self.normal.log_survival(value, mean, sd)
  89. return prob + log_prob + cdf + log_cdf + sf + log_sf
  90. def test_normal_prob1():
  91. """
  92. Test probability functions: passing mean/sd, value through construct.
  93. """
  94. net = NormalProb1()
  95. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  96. mean = Tensor([0.0], dtype=dtype.float32)
  97. sd = Tensor([1.0], dtype=dtype.float32)
  98. ans = net(value, mean, sd)
  99. assert isinstance(ans, Tensor)
  100. class NormalKl(nn.Cell):
  101. """
  102. Test class: kl_loss of Normal distribution.
  103. """
  104. def __init__(self):
  105. super(NormalKl, self).__init__()
  106. self.n1 = msd.Normal(np.array([3.0]), np.array([4.0]), dtype=dtype.float32)
  107. self.n2 = msd.Normal(dtype=dtype.float32)
  108. def construct(self, mean_b, sd_b, mean_a, sd_a):
  109. kl1 = self.n1.kl_loss('Normal', mean_b, sd_b)
  110. kl2 = self.n2.kl_loss('Normal', mean_b, sd_b, mean_a, sd_a)
  111. return kl1 + kl2
  112. def test_kl():
  113. """
  114. Test kl_loss.
  115. """
  116. net = NormalKl()
  117. mean_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  118. sd_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  119. mean_a = Tensor(np.array([2.0]).astype(np.float32), dtype=dtype.float32)
  120. sd_a = Tensor(np.array([3.0]).astype(np.float32), dtype=dtype.float32)
  121. ans = net(mean_b, sd_b, mean_a, sd_a)
  122. assert isinstance(ans, Tensor)
  123. class NormalCrossEntropy(nn.Cell):
  124. """
  125. Test class: cross_entropy of Normal distribution.
  126. """
  127. def __init__(self):
  128. super(NormalCrossEntropy, self).__init__()
  129. self.n1 = msd.Normal(np.array([3.0]), np.array([4.0]), dtype=dtype.float32)
  130. self.n2 = msd.Normal(dtype=dtype.float32)
  131. def construct(self, mean_b, sd_b, mean_a, sd_a):
  132. h1 = self.n1.cross_entropy('Normal', mean_b, sd_b)
  133. h2 = self.n2.cross_entropy('Normal', mean_b, sd_b, mean_a, sd_a)
  134. return h1 + h2
  135. def test_cross_entropy():
  136. """
  137. Test cross entropy between Normal distributions.
  138. """
  139. net = NormalCrossEntropy()
  140. mean_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  141. sd_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  142. mean_a = Tensor(np.array([2.0]).astype(np.float32), dtype=dtype.float32)
  143. sd_a = Tensor(np.array([3.0]).astype(np.float32), dtype=dtype.float32)
  144. ans = net(mean_b, sd_b, mean_a, sd_a)
  145. assert isinstance(ans, Tensor)
  146. class NormalBasics(nn.Cell):
  147. """
  148. Test class: basic mean/sd function.
  149. """
  150. def __init__(self):
  151. super(NormalBasics, self).__init__()
  152. self.n = msd.Normal(3.0, 4.0, dtype=dtype.float32)
  153. def construct(self):
  154. mean = self.n.mean()
  155. sd = self.n.sd()
  156. mode = self.n.mode()
  157. entropy = self.n.entropy()
  158. return mean + sd + mode + entropy
  159. def test_bascis():
  160. """
  161. Test mean/sd/mode/entropy functionality of Normal.
  162. """
  163. net = NormalBasics()
  164. ans = net()
  165. assert isinstance(ans, Tensor)
  166. class NormalConstruct(nn.Cell):
  167. """
  168. Normal distribution: going through construct.
  169. """
  170. def __init__(self):
  171. super(NormalConstruct, self).__init__()
  172. self.normal = msd.Normal(3.0, 4.0)
  173. self.normal1 = msd.Normal()
  174. def construct(self, value, mean, sd):
  175. prob = self.normal('prob', value)
  176. prob1 = self.normal('prob', value, mean, sd)
  177. prob2 = self.normal1('prob', value, mean, sd)
  178. return prob + prob1 + prob2
  179. def test_normal_construct():
  180. """
  181. Test probability function going through construct.
  182. """
  183. net = NormalConstruct()
  184. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  185. mean = Tensor([0.0], dtype=dtype.float32)
  186. sd = Tensor([1.0], dtype=dtype.float32)
  187. ans = net(value, mean, sd)
  188. assert isinstance(ans, Tensor)