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""" 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_flatten(): 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.flatten() 34 35 net = Net() 36 net() 37 38 39def test_flatten_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.flatten(order='F') 47 48 net = Net() 49 net() 50 51 52def test_flatten_error(): 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.flatten(order='X') 60 61 net = Net() 62 with pytest.raises(ValueError): 63 net() 64 65 66def test_flatten_error_1(): 67 class Net(nn.Cell): 68 def __init__(self): 69 super(Net, self).__init__() 70 self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32) 71 72 def construct(self): 73 return self.value.flatten(order=123) 74 75 net = Net() 76 with pytest.raises(TypeError): 77 net() 78