• 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 reshape"""
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_reshape():
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.reshape(-1)
34
35    net = Net()
36    net()
37
38
39def test_reshape_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.reshape([3, 2, 1])
47
48    net = Net()
49    net()
50
51
52def test_reshape_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.reshape((-1, 2))
60
61    net = Net()
62    net()
63
64
65def test_reshape_error():
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.reshape(1, 2, 4)
73
74    net = Net()
75    with pytest.raises(ValueError):
76        net()
77
78
79def test_reshape_error_1():
80    class Net(nn.Cell):
81        def __init__(self):
82            super(Net, self).__init__()
83            self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
84
85        def construct(self):
86            return self.value.reshape((1, 2, 3.5))
87
88    net = Net()
89    with pytest.raises(TypeError):
90        net()
91