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_reshape.py 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 reshape"""
  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_reshape():
  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.reshape(-1)
  29. net = Net()
  30. net()
  31. def test_reshape_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.reshape([3, 2, 1])
  38. net = Net()
  39. net()
  40. def test_reshape_2():
  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.reshape((-1, 2))
  47. net = Net()
  48. net()
  49. def test_reshape_error():
  50. class Net(nn.Cell):
  51. def __init__(self):
  52. super(Net, self).__init__()
  53. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  54. def construct(self):
  55. return self.value.reshape(1, 2, 4)
  56. net = Net()
  57. with pytest.raises(ValueError):
  58. net()
  59. def test_reshape_error_1():
  60. class Net(nn.Cell):
  61. def __init__(self):
  62. super(Net, self).__init__()
  63. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  64. def construct(self):
  65. return self.value.reshape((1, 2, 3.5))
  66. net = Net()
  67. with pytest.raises(TypeError):
  68. net()