• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""
16import pytest
17
18import mindspore.nn as nn
19import mindspore.common.dtype as mstype
20from mindspore import Tensor
21from mindspore import context
22
23context.set_context(mode=context.GRAPH_MODE)
24
25
26def test_transpose():
27    class Net(nn.Cell):
28        def __init__(self):
29            super(Net, self).__init__()
30            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
31
32        def construct(self):
33            return self.value.transpose()
34
35    net = Net()
36    net()
37
38
39def test_transpose_1():
40    class Net(nn.Cell):
41        def __init__(self):
42            super(Net, self).__init__()
43            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
44
45        def construct(self):
46            return self.value.transpose(1, 0)
47
48    net = Net()
49    net()
50
51
52def test_transpose_2():
53    class Net(nn.Cell):
54        def __init__(self):
55            super(Net, self).__init__()
56            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
57
58        def construct(self):
59            return self.value.transpose([1, 0])
60
61    net = Net()
62    net()
63
64
65def test_transpose_3():
66    class Net(nn.Cell):
67        def __init__(self):
68            super(Net, self).__init__()
69            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
70
71        def construct(self):
72            return self.value.transpose((1, 0))
73
74    net = Net()
75    net()
76
77
78def test_transpose_error():
79    class Net(nn.Cell):
80        def __init__(self):
81            super(Net, self).__init__()
82            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
83
84        def construct(self):
85            return self.value.transpose(0, 2, 1)
86
87    net = Net()
88    with pytest.raises(ValueError):
89        net()
90
91
92def test_transpose_error_1():
93    class Net(nn.Cell):
94        def __init__(self):
95            super(Net, self).__init__()
96            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
97
98        def construct(self):
99            return self.value.transpose(1.0, 0)
100
101    net = Net()
102    with pytest.raises(TypeError):
103        net()
104