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_uniform.py 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.Uniform.
  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_uniform_shape_errpr():
  25. """
  26. Invalid shapes.
  27. """
  28. with pytest.raises(ValueError):
  29. msd.Uniform([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
  30. def test_type():
  31. with pytest.raises(TypeError):
  32. msd.Uniform(0., 1., dtype=dtype.int32)
  33. def test_name():
  34. with pytest.raises(TypeError):
  35. msd.Uniform(0., 1., name=1.0)
  36. def test_seed():
  37. with pytest.raises(TypeError):
  38. msd.Uniform(0., 1., seed='seed')
  39. def test_arguments():
  40. """
  41. Args passing during initialization.
  42. """
  43. u = msd.Uniform()
  44. assert isinstance(u, msd.Distribution)
  45. u = msd.Uniform([3.0], [4.0], dtype=dtype.float32)
  46. assert isinstance(u, msd.Distribution)
  47. def test_invalid_range():
  48. """
  49. Test range of uniform distribution.
  50. """
  51. with pytest.raises(ValueError):
  52. msd.Uniform(0.0, 0.0, dtype=dtype.float32)
  53. with pytest.raises(ValueError):
  54. msd.Uniform(1.0, 0.0, dtype=dtype.float32)
  55. class UniformProb(nn.Cell):
  56. """
  57. Uniform distribution: initialize with low/high.
  58. """
  59. def __init__(self):
  60. super(UniformProb, self).__init__()
  61. self.u = msd.Uniform(3.0, 4.0, dtype=dtype.float32)
  62. def construct(self, value):
  63. prob = self.u.prob(value)
  64. log_prob = self.u.log_prob(value)
  65. cdf = self.u.cdf(value)
  66. log_cdf = self.u.log_cdf(value)
  67. sf = self.u.survival_function(value)
  68. log_sf = self.u.log_survival(value)
  69. return prob + log_prob + cdf + log_cdf + sf + log_sf
  70. def test_uniform_prob():
  71. """
  72. Test probability functions: passing value through construct.
  73. """
  74. net = UniformProb()
  75. value = Tensor([3.1, 3.2, 3.3, 3.4], dtype=dtype.float32)
  76. ans = net(value)
  77. assert isinstance(ans, Tensor)
  78. class UniformProb1(nn.Cell):
  79. """
  80. Uniform distribution: initialize without low/high.
  81. """
  82. def __init__(self):
  83. super(UniformProb1, self).__init__()
  84. self.u = msd.Uniform(dtype=dtype.float32)
  85. def construct(self, value, low, high):
  86. prob = self.u.prob(value, low, high)
  87. log_prob = self.u.log_prob(value, low, high)
  88. cdf = self.u.cdf(value, low, high)
  89. log_cdf = self.u.log_cdf(value, low, high)
  90. sf = self.u.survival_function(value, low, high)
  91. log_sf = self.u.log_survival(value, low, high)
  92. return prob + log_prob + cdf + log_cdf + sf + log_sf
  93. def test_uniform_prob1():
  94. """
  95. Test probability functions: passing low/high, value through construct.
  96. """
  97. net = UniformProb1()
  98. value = Tensor([0.1, 0.2, 0.3, 0.9], dtype=dtype.float32)
  99. low = Tensor([0.0], dtype=dtype.float32)
  100. high = Tensor([1.0], dtype=dtype.float32)
  101. ans = net(value, low, high)
  102. assert isinstance(ans, Tensor)
  103. class UniformKl(nn.Cell):
  104. """
  105. Test class: kl_loss of Uniform distribution.
  106. """
  107. def __init__(self):
  108. super(UniformKl, self).__init__()
  109. self.u1 = msd.Uniform(np.array([3.0]), np.array([4.0]), dtype=dtype.float32)
  110. self.u2 = msd.Uniform(dtype=dtype.float32)
  111. def construct(self, low_b, high_b, low_a, high_a):
  112. kl1 = self.u1.kl_loss('Uniform', low_b, high_b)
  113. kl2 = self.u2.kl_loss('Uniform', low_b, high_b, low_a, high_a)
  114. return kl1 + kl2
  115. def test_kl():
  116. """
  117. Test kl_loss.
  118. """
  119. net = UniformKl()
  120. low_b = Tensor(np.array([0.0]).astype(np.float32), dtype=dtype.float32)
  121. high_b = Tensor(np.array([5.0]).astype(np.float32), dtype=dtype.float32)
  122. low_a = Tensor(np.array([2.0]).astype(np.float32), dtype=dtype.float32)
  123. high_a = Tensor(np.array([3.0]).astype(np.float32), dtype=dtype.float32)
  124. ans = net(low_b, high_b, low_a, high_a)
  125. assert isinstance(ans, Tensor)
  126. class UniformCrossEntropy(nn.Cell):
  127. """
  128. Test class: cross_entropy of Uniform distribution.
  129. """
  130. def __init__(self):
  131. super(UniformCrossEntropy, self).__init__()
  132. self.u1 = msd.Uniform(np.array([3.0]), np.array([4.0]), dtype=dtype.float32)
  133. self.u2 = msd.Uniform(dtype=dtype.float32)
  134. def construct(self, low_b, high_b, low_a, high_a):
  135. h1 = self.u1.cross_entropy('Uniform', low_b, high_b)
  136. h2 = self.u2.cross_entropy('Uniform', low_b, high_b, low_a, high_a)
  137. return h1 + h2
  138. def test_cross_entropy():
  139. """
  140. Test cross_entropy between Unifrom distributions.
  141. """
  142. net = UniformCrossEntropy()
  143. low_b = Tensor(np.array([0.0]).astype(np.float32), dtype=dtype.float32)
  144. high_b = Tensor(np.array([5.0]).astype(np.float32), dtype=dtype.float32)
  145. low_a = Tensor(np.array([2.0]).astype(np.float32), dtype=dtype.float32)
  146. high_a = Tensor(np.array([3.0]).astype(np.float32), dtype=dtype.float32)
  147. ans = net(low_b, high_b, low_a, high_a)
  148. assert isinstance(ans, Tensor)
  149. class UniformBasics(nn.Cell):
  150. """
  151. Test class: basic mean/sd/var/mode/entropy function.
  152. """
  153. def __init__(self):
  154. super(UniformBasics, self).__init__()
  155. self.u = msd.Uniform(3.0, 4.0, dtype=dtype.float32)
  156. def construct(self):
  157. mean = self.u.mean()
  158. sd = self.u.sd()
  159. var = self.u.var()
  160. entropy = self.u.entropy()
  161. return mean + sd + var + entropy
  162. def test_bascis():
  163. """
  164. Test mean/sd/var/mode/entropy functionality of Uniform.
  165. """
  166. net = UniformBasics()
  167. ans = net()
  168. assert isinstance(ans, Tensor)
  169. class UniConstruct(nn.Cell):
  170. """
  171. Unifrom distribution: going through construct.
  172. """
  173. def __init__(self):
  174. super(UniConstruct, self).__init__()
  175. self.u = msd.Uniform(-4.0, 4.0)
  176. self.u1 = msd.Uniform()
  177. def construct(self, value, low, high):
  178. prob = self.u('prob', value)
  179. prob1 = self.u('prob', value, low, high)
  180. prob2 = self.u1('prob', value, low, high)
  181. return prob + prob1 + prob2
  182. def test_uniform_construct():
  183. """
  184. Test probability function going through construct.
  185. """
  186. net = UniConstruct()
  187. value = Tensor([-5.0, 0.0, 1.0, 5.0], dtype=dtype.float32)
  188. low = Tensor([-1.0], dtype=dtype.float32)
  189. high = Tensor([1.0], dtype=dtype.float32)
  190. ans = net(value, low, high)
  191. assert isinstance(ans, Tensor)