test_expand_op.py 5.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

Y
yangyaming 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
Y
yangyaming 已提交
20 21 22 23 24 25


class TestExpandOpRank1(OpTest):
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {'X': np.random.random(12).astype("float32")}
26
        self.attrs = {'expand_times': [2]}
Y
yangyaming 已提交
27 28 29 30 31 32 33 34 35 36
        output = np.tile(self.inputs['X'], 2)
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class TestExpandOpRank1_tensor_attr(OpTest):
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {
            'X': np.random.random(12).astype("float32"),
            'expand_times_tensor': [('x1', np.ones((1)).astype('int32') * 2)]
        }
        self.attrs = {}
        output = np.tile(self.inputs['X'], 2)
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', no_grad_set=set('x1'))


55
class TestExpandOpRank2_Corner(OpTest):
Y
yangyaming 已提交
56 57 58
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {'X': np.random.random((12, 14)).astype("float32")}
59
        self.attrs = {'expand_times': [1, 1]}
Y
yangyaming 已提交
60
        output = np.tile(self.inputs['X'], (1, 1))
Y
yangyaming 已提交
61 62 63 64 65 66 67 68 69
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
class TestExpandOpRank2_Corner_tensor_attr(OpTest):
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {
            'X': np.random.random((12, 14)).astype("float32"),
            'expand_times_tensor': [('x1', np.ones((1)).astype('int32')),
                                    ('x2', np.ones((1)).astype('int32'))]
        }
        self.attrs = {}
        output = np.tile(self.inputs['X'], (1, 1))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


89
class TestExpandOpRank2(OpTest):
Y
yangyaming 已提交
90 91 92
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {'X': np.random.random((12, 14)).astype("float32")}
93
        self.attrs = {'expand_times': [2, 3]}
Y
yangyaming 已提交
94 95 96 97 98 99 100 101 102 103
        output = np.tile(self.inputs['X'], (2, 3))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
class TestExpandOpRank2_attr_tensor(OpTest):
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {
            'X': np.random.random((12, 14)).astype("float32"),
            'expand_times_tensor': [('x1', np.ones((1)).astype('int32') * 2),
                                    ('x2', np.ones((1)).astype('int32') * 3)]
        }
        self.attrs = {}
        output = np.tile(self.inputs['X'], (2, 3))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


123
class TestExpandOpRank3_Corner(OpTest):
Y
yangyaming 已提交
124 125 126
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {'X': np.random.random((2, 4, 5)).astype("float32")}
127
        self.attrs = {'expand_times': [1, 1, 1]}
Y
yangyaming 已提交
128
        output = np.tile(self.inputs['X'], (1, 1, 1))
Y
yangyaming 已提交
129 130 131 132 133 134 135 136 137
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


138
class TestExpandOpRank3(OpTest):
Y
yangyaming 已提交
139 140 141
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {'X': np.random.random((2, 4, 5)).astype("float32")}
142
        self.attrs = {'expand_times': [2, 1, 4]}
Y
yangyaming 已提交
143 144 145 146 147 148 149 150 151 152
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


Y
yangyaming 已提交
153 154 155 156
class TestExpandOpRank4(OpTest):
    def setUp(self):
        self.op_type = "expand"
        self.inputs = {'X': np.random.random((2, 4, 5, 7)).astype("float32")}
157
        self.attrs = {'expand_times': [3, 2, 1, 2]}
Y
yangyaming 已提交
158 159 160 161 162 163 164 165 166 167
        output = np.tile(self.inputs['X'], (3, 2, 1, 2))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


168 169 170
class TestExpandOpInteger(OpTest):
    def setUp(self):
        self.op_type = "expand"
J
jerrywgz 已提交
171 172 173 174
        self.inputs = {
            'X': np.random.randint(
                10, size=(2, 4, 5)).astype("int32")
        }
175 176 177 178 179 180 181 182 183 184 185
        self.attrs = {'expand_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


class TestExpandOpBoolean(OpTest):
    def setUp(self):
        self.op_type = "expand"
J
jerrywgz 已提交
186
        self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")}
187 188 189 190 191 192 193 194
        self.attrs = {'expand_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output()


Y
yangyaming 已提交
195 196
if __name__ == "__main__":
    unittest.main()