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_distribution.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.
  17. """
  18. import pytest
  19. import mindspore.nn as nn
  20. import mindspore.nn.probability.distribution as msd
  21. from mindspore import dtype as mstype
  22. from mindspore import Tensor
  23. from mindspore import context
  24. func_name_list = ['prob', 'log_prob', 'cdf', 'log_cdf',
  25. 'survival_function', 'log_survival',
  26. 'sd', 'var', 'mode', 'mean',
  27. 'entropy', 'kl_loss', 'cross_entropy',
  28. 'sample']
  29. class MyExponential(msd.Distribution):
  30. """
  31. Test distribution class: no function is implemented.
  32. """
  33. def __init__(self, rate=None, seed=None, dtype=mstype.float32, name="MyExponential"):
  34. param = dict(locals())
  35. param['param_dict'] = {'rate': rate}
  36. super(MyExponential, self).__init__(seed, dtype, name, param)
  37. class Net(nn.Cell):
  38. """
  39. Test Net: function called through construct.
  40. """
  41. def __init__(self, func_name):
  42. super(Net, self).__init__()
  43. self.dist = MyExponential()
  44. self.name = func_name
  45. def construct(self, *args, **kwargs):
  46. return self.dist(self.name, *args, **kwargs)
  47. def test_raise_not_implemented_error_construct():
  48. """
  49. test raise not implemented error in pynative mode.
  50. """
  51. value = Tensor([0.2], dtype=mstype.float32)
  52. for func_name in func_name_list:
  53. with pytest.raises(NotImplementedError):
  54. net = Net(func_name)
  55. net(value)
  56. def test_raise_not_implemented_error_construct_graph_mode():
  57. """
  58. test raise not implemented error in graph mode.
  59. """
  60. context.set_context(mode=context.GRAPH_MODE)
  61. value = Tensor([0.2], dtype=mstype.float32)
  62. for func_name in func_name_list:
  63. with pytest.raises(NotImplementedError):
  64. net = Net(func_name)
  65. net(value)
  66. class Net1(nn.Cell):
  67. """
  68. Test Net: function called directly.
  69. """
  70. def __init__(self, func_name):
  71. super(Net1, self).__init__()
  72. self.dist = MyExponential()
  73. self.func = getattr(self.dist, func_name)
  74. def construct(self, *args, **kwargs):
  75. return self.func(*args, **kwargs)
  76. def test_raise_not_implemented_error():
  77. """
  78. test raise not implemented error in pynative mode.
  79. """
  80. value = Tensor([0.2], dtype=mstype.float32)
  81. for func_name in func_name_list:
  82. with pytest.raises(NotImplementedError):
  83. net = Net1(func_name)
  84. net(value)
  85. def test_raise_not_implemented_error_graph_mode():
  86. """
  87. test raise not implemented error in graph mode.
  88. """
  89. context.set_context(mode=context.GRAPH_MODE)
  90. value = Tensor([0.2], dtype=mstype.float32)
  91. for func_name in func_name_list:
  92. with pytest.raises(NotImplementedError):
  93. net = Net1(func_name)
  94. net(value)