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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # You may obtain a copy of the License at
  2. #
  3. # http://www.apache.org/licenses/LICENSE-2.0
  4. #
  5. # Unless required by applicable law or agreed to in writing, software
  6. # distributed under the License is distributed on an "AS IS" BASIS,
  7. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. # See the License for the specific language governing permissions and
  9. # limitations under the License.
  10. # ============================================================================
  11. """
  12. Test nn.probability.distribution.
  13. """
  14. import pytest
  15. import mindspore.nn as nn
  16. import mindspore.nn.probability.distribution as msd
  17. from mindspore import dtype as mstype
  18. from mindspore import Tensor
  19. from mindspore import context
  20. func_name_list = ['prob', 'log_prob', 'cdf', 'log_cdf',
  21. 'survival_function', 'log_survival',
  22. 'sd', 'var', 'mode', 'mean',
  23. 'entropy', 'kl_loss', 'cross_entropy',
  24. 'sample']
  25. class MyExponential(msd.Distribution):
  26. """
  27. Test distirbution class: no function is implemented.
  28. """
  29. def __init__(self, rate=None, seed=None, dtype=mstype.float32, name="MyExponential"):
  30. param = dict(locals())
  31. param['param_dict'] = {'rate': rate}
  32. super(MyExponential, self).__init__(seed, dtype, name, param)
  33. class Net(nn.Cell):
  34. """
  35. Test Net: function called through construct.
  36. """
  37. def __init__(self, func_name):
  38. super(Net, self).__init__()
  39. self.dist = MyExponential()
  40. self.name = func_name
  41. def construct(self, *args, **kwargs):
  42. return self.dist(self.name, *args, **kwargs)
  43. def test_raise_not_implemented_error_construct():
  44. """
  45. test raise not implemented error in pynative mode.
  46. """
  47. value = Tensor([0.2], dtype=mstype.float32)
  48. for func_name in func_name_list:
  49. with pytest.raises(NotImplementedError):
  50. net = Net(func_name)
  51. net(value)
  52. def test_raise_not_implemented_error_construct_graph_mode():
  53. """
  54. test raise not implemented error in graph mode.
  55. """
  56. context.set_context(mode=context.GRAPH_MODE)
  57. value = Tensor([0.2], dtype=mstype.float32)
  58. for func_name in func_name_list:
  59. with pytest.raises(NotImplementedError):
  60. net = Net(func_name)
  61. net(value)
  62. class Net1(nn.Cell):
  63. """
  64. Test Net: function called directly.
  65. """
  66. def __init__(self, func_name):
  67. super(Net1, self).__init__()
  68. self.dist = MyExponential()
  69. self.func = getattr(self.dist, func_name)
  70. def construct(self, *args, **kwargs):
  71. return self.func(*args, **kwargs)
  72. def test_raise_not_implemented_error():
  73. """
  74. test raise not implemented error in pynative mode.
  75. """
  76. value = Tensor([0.2], dtype=mstype.float32)
  77. for func_name in func_name_list:
  78. with pytest.raises(NotImplementedError):
  79. net = Net1(func_name)
  80. net(value)
  81. def test_raise_not_implemented_error_graph_mode():
  82. """
  83. test raise not implemented error in graph mode.
  84. """
  85. context.set_context(mode=context.GRAPH_MODE)
  86. value = Tensor([0.2], dtype=mstype.float32)
  87. for func_name in func_name_list:
  88. with pytest.raises(NotImplementedError):
  89. net = Net1(func_name)
  90. net(value)