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_parse_numpy.py 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2021 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_parse_numpy """
  16. import pytest
  17. import numpy as np
  18. from mindspore import nn
  19. from mindspore import context
  20. context.set_context(mode=context.GRAPH_MODE)
  21. def test_use_numpy_constant():
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. def construct(self):
  26. ret = np.pi
  27. return ret
  28. net = Net()
  29. output = net()
  30. assert np.allclose(output, np.pi)
  31. def test_use_numpy_method():
  32. class Net(nn.Cell):
  33. def __init__(self):
  34. super(Net, self).__init__()
  35. def construct(self):
  36. ret = np.linspace(1, 10, 4)
  37. return ret
  38. net = Net()
  39. with pytest.raises(NotImplementedError) as err:
  40. net()
  41. assert "MindSpore does not support to use the numpy methods in the function construct with the graph mode." \
  42. in str(err.value)
  43. def test_use_numpy_module():
  44. class Net(nn.Cell):
  45. def __init__(self):
  46. super(Net, self).__init__()
  47. def construct(self):
  48. ret = np.random.randint(0, 10, [1, 10])
  49. return ret
  50. net = Net()
  51. with pytest.raises(NotImplementedError) as err:
  52. net()
  53. assert "MindSpore does not support to use the numpy methods in the function construct with the graph mode." \
  54. in str(err.value)