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_error.py 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. """test error"""
  16. import math
  17. import numpy as np
  18. import pytest
  19. from mindspore import Tensor
  20. from mindspore.nn.metrics import MAE, MSE
  21. def test_MAE():
  22. x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]))
  23. y = Tensor(np.array([0.1, 0.25, 0.7, 0.9]))
  24. error = MAE()
  25. error.clear()
  26. error.update(x, y)
  27. result = error.eval()
  28. assert math.isclose(result, 0.15 / 4)
  29. def test_input_MAE():
  30. x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]))
  31. y = Tensor(np.array([0.1, 0.25, 0.7, 0.9]))
  32. error = MAE()
  33. error.clear()
  34. with pytest.raises(ValueError):
  35. error.update(x, y, x)
  36. def test_zero_MAE():
  37. error = MAE()
  38. with pytest.raises(RuntimeError):
  39. error.eval()
  40. def test_MSE():
  41. x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]))
  42. y = Tensor(np.array([0.1, 0.25, 0.5, 0.9]))
  43. error = MSE()
  44. error.clear()
  45. error.update(x, y)
  46. result = error.eval()
  47. assert math.isclose(result, 0.0125 / 4)
  48. def test_input_MSE():
  49. x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]))
  50. y = Tensor(np.array([0.1, 0.25, 0.7, 0.9]))
  51. error = MSE()
  52. error.clear()
  53. with pytest.raises(ValueError):
  54. error.update(x, y, x)
  55. def test_zero_MSE():
  56. error = MSE()
  57. with pytest.raises(RuntimeError):
  58. error.eval()