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_flatten.py 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 flatten"""
  16. import pytest
  17. import mindspore.nn as nn
  18. import mindspore.common.dtype as mstype
  19. from mindspore import Tensor
  20. from mindspore import context
  21. context.set_context(mode=context.GRAPH_MODE)
  22. def test_flatten():
  23. class Net(nn.Cell):
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  27. def construct(self):
  28. return self.value.flatten()
  29. net = Net()
  30. net()
  31. def test_flatten_1():
  32. class Net(nn.Cell):
  33. def __init__(self):
  34. super(Net, self).__init__()
  35. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  36. def construct(self):
  37. return self.value.flatten(order='F')
  38. net = Net()
  39. net()
  40. def test_flatten_error():
  41. class Net(nn.Cell):
  42. def __init__(self):
  43. super(Net, self).__init__()
  44. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  45. def construct(self):
  46. return self.value.flatten(order='X')
  47. net = Net()
  48. with pytest.raises(ValueError):
  49. net()
  50. def test_flatten_error_1():
  51. class Net(nn.Cell):
  52. def __init__(self):
  53. super(Net, self).__init__()
  54. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  55. def construct(self):
  56. return self.value.flatten(order=123)
  57. net = Net()
  58. with pytest.raises(TypeError):
  59. net()