test_reduce_op.py 3.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.

G
guosheng 已提交
15 16
import unittest
import numpy as np
17
from op_test import OpTest
G
guosheng 已提交
18 19


20
class TestSumOp(OpTest):
G
guosheng 已提交
21
    def setUp(self):
22
        self.op_type = "reduce_sum"
23
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
24
        self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
G
guosheng 已提交
25

26 27
    def test_check_output(self):
        self.check_output()
G
guosheng 已提交
28

29 30
    def test_check_grad(self):
        self.check_grad(['X'], 'Out')
G
guosheng 已提交
31 32


33 34 35
class TestMeanOp(OpTest):
    def setUp(self):
        self.op_type = "reduce_mean"
36
        self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float64")}
37 38
        self.attrs = {'dim': 1}
        self.outputs = {'Out': self.inputs['X'].mean(axis=self.attrs['dim'])}
G
guosheng 已提交
39

40 41
    def test_check_output(self):
        self.check_output()
G
guosheng 已提交
42

43 44
    def test_check_grad(self):
        self.check_grad(['X'], 'Out')
G
guosheng 已提交
45 46


47 48
class TestMaxOp(OpTest):
    """Remove Max with subgradient from gradient check to confirm the success of CI."""
G
guosheng 已提交
49 50

    def setUp(self):
51
        self.op_type = "reduce_max"
52
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
G
guosheng 已提交
53
        self.attrs = {'dim': -1}
54 55 56 57
        self.outputs = {'Out': self.inputs['X'].max(axis=self.attrs['dim'])}

    def test_check_output(self):
        self.check_output()
G
guosheng 已提交
58 59


60 61
class TestMinOp(OpTest):
    """Remove Min with subgradient from gradient check to confirm the success of CI."""
G
guosheng 已提交
62

63 64
    def setUp(self):
        self.op_type = "reduce_min"
65
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
66 67
        self.attrs = {'dim': 2}
        self.outputs = {'Out': self.inputs['X'].min(axis=self.attrs['dim'])}
G
guosheng 已提交
68

69 70
    def test_check_output(self):
        self.check_output()
G
guosheng 已提交
71 72


73 74 75 76 77 78 79 80 81 82 83 84 85
class TestProdOp(OpTest):
    def setUp(self):
        self.op_type = "reduce_prod"
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].prod(axis=0)}

    def test_check_output(self):
        self.check_output()

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


86
class TestKeepDimReduce(OpTest):
G
guosheng 已提交
87
    def setUp(self):
88
        self.op_type = "reduce_sum"
89
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
90
        self.attrs = {'dim': -2, 'keep_dim': True}
91 92 93 94 95 96
        self.outputs = {
            'Out': self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
        }

    def test_check_output(self):
        self.check_output()
G
guosheng 已提交
97

98 99
    def test_check_grad(self):
        self.check_grad(['X'], 'Out')
G
guosheng 已提交
100 101


102
class Test1DReduce(OpTest):
G
guosheng 已提交
103
    def setUp(self):
104
        self.op_type = "reduce_sum"
105
        self.inputs = {'X': np.random.random(20).astype("float64")}
106 107 108 109 110 111 112
        self.outputs = {'Out': self.inputs['X'].sum(axis=0)}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')
G
guosheng 已提交
113 114


115 116 117
class TestReduceAll(OpTest):
    def setUp(self):
        self.op_type = "reduce_sum"
118
        self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float64")}
119 120 121 122 123 124 125 126 127 128
        self.attrs = {'reduce_all': True}
        self.outputs = {'Out': self.inputs['X'].sum()}

    def test_check_output(self):
        self.check_output()

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


G
guosheng 已提交
129 130
if __name__ == '__main__':
    unittest.main()