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_gumbel.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.gumbel.
  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_gumbel_shape_errpr():
  25. """
  26. Invalid shapes.
  27. """
  28. with pytest.raises(ValueError):
  29. msd.Gumbel([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
  30. def test_type():
  31. with pytest.raises(TypeError):
  32. msd.Gumbel(0., 1., dtype=dtype.int32)
  33. def test_name():
  34. with pytest.raises(TypeError):
  35. msd.Gumbel(0., 1., name=1.0)
  36. def test_seed():
  37. with pytest.raises(TypeError):
  38. msd.Gumbel(0., 1., seed='seed')
  39. def test_scale():
  40. with pytest.raises(ValueError):
  41. msd.Gumbel(0., 0.)
  42. with pytest.raises(ValueError):
  43. msd.Gumbel(0., -1.)
  44. def test_arguments():
  45. """
  46. args passing during initialization.
  47. """
  48. l = msd.Gumbel([3.0], [4.0], dtype=dtype.float32)
  49. assert isinstance(l, msd.Distribution)
  50. class GumbelProb(nn.Cell):
  51. """
  52. Gumbel distribution: initialize with loc/scale.
  53. """
  54. def __init__(self):
  55. super(GumbelProb, self).__init__()
  56. self.gumbel = msd.Gumbel(3.0, 4.0, dtype=dtype.float32)
  57. def construct(self, value):
  58. prob = self.gumbel.prob(value)
  59. log_prob = self.gumbel.log_prob(value)
  60. cdf = self.gumbel.cdf(value)
  61. log_cdf = self.gumbel.log_cdf(value)
  62. sf = self.gumbel.survival_function(value)
  63. log_sf = self.gumbel.log_survival(value)
  64. return prob + log_prob + cdf + log_cdf + sf + log_sf
  65. def test_gumbel_prob():
  66. """
  67. Test probability functions: passing value through construct.
  68. """
  69. net = GumbelProb()
  70. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  71. ans = net(value)
  72. assert isinstance(ans, Tensor)
  73. class KL(nn.Cell):
  74. """
  75. Test kl_loss.
  76. """
  77. def __init__(self):
  78. super(KL, self).__init__()
  79. self.gumbel = msd.Gumbel(3.0, 4.0)
  80. def construct(self, mu, s):
  81. kl = self.gumbel.kl_loss('Gumbel', mu, s)
  82. cross_entropy = self.gumbel.cross_entropy('Gumbel', mu, s)
  83. return kl + cross_entropy
  84. def test_kl_cross_entropy():
  85. """
  86. Test kl_loss and cross_entropy.
  87. """
  88. from mindspore import context
  89. context.set_context(device_target="Ascend")
  90. net = KL()
  91. loc_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  92. scale_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
  93. ans = net(loc_b, scale_b)
  94. assert isinstance(ans, Tensor)
  95. class GumbelBasics(nn.Cell):
  96. """
  97. Test class: basic loc/scale function.
  98. """
  99. def __init__(self):
  100. super(GumbelBasics, self).__init__()
  101. self.gumbel = msd.Gumbel(3.0, 4.0, dtype=dtype.float32)
  102. def construct(self):
  103. mean = self.gumbel.mean()
  104. sd = self.gumbel.sd()
  105. mode = self.gumbel.mode()
  106. entropy = self.gumbel.entropy()
  107. return mean + sd + mode + entropy
  108. def test_bascis():
  109. """
  110. Test mean/sd/mode/entropy functionality of Gumbel.
  111. """
  112. net = GumbelBasics()
  113. ans = net()
  114. assert isinstance(ans, Tensor)
  115. class GumbelConstruct(nn.Cell):
  116. """
  117. Gumbel distribution: going through construct.
  118. """
  119. def __init__(self):
  120. super(GumbelConstruct, self).__init__()
  121. self.gumbel = msd.Gumbel(3.0, 4.0)
  122. def construct(self, value):
  123. prob = self.gumbel('prob', value)
  124. prob1 = self.gumbel.prob(value)
  125. return prob + prob1
  126. def test_gumbel_construct():
  127. """
  128. Test probability function going through construct.
  129. """
  130. net = GumbelConstruct()
  131. value = Tensor([0.5, 1.0], dtype=dtype.float32)
  132. ans = net(value)
  133. assert isinstance(ans, Tensor)