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_transpose.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 transpose"""
  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_transpose():
  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.transpose()
  29. net = Net()
  30. net()
  31. def test_transpose_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.transpose(1, 0)
  38. net = Net()
  39. net()
  40. def test_transpose_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.transpose([1, 0])
  47. net = Net()
  48. net()
  49. def test_transpose_3():
  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.transpose((1, 0))
  56. net = Net()
  57. net()
  58. def test_transpose_error():
  59. class Net(nn.Cell):
  60. def __init__(self):
  61. super(Net, self).__init__()
  62. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  63. def construct(self):
  64. return self.value.transpose(0, 2, 1)
  65. net = Net()
  66. with pytest.raises(ValueError):
  67. net()
  68. def test_transpose_error_1():
  69. class Net(nn.Cell):
  70. def __init__(self):
  71. super(Net, self).__init__()
  72. self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
  73. def construct(self):
  74. return self.value.transpose(1.0, 0)
  75. net = Net()
  76. with pytest.raises(TypeError):
  77. net()