test_reduce_op.py 3.5 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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"
G
guosheng 已提交
23
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
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 36 37 38
class TestMeanOp(OpTest):
    def setUp(self):
        self.op_type = "reduce_mean"
        self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float32")}
        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"
G
guosheng 已提交
52 53
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
        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 65 66 67
    def setUp(self):
        self.op_type = "reduce_min"
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
        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
class TestKeepDimReduce(OpTest):
G
guosheng 已提交
74
    def setUp(self):
75
        self.op_type = "reduce_sum"
G
guosheng 已提交
76
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
77
        self.attrs = {'dim': -2, 'keep_dim': True}
78 79 80 81 82 83
        self.outputs = {
            'Out': self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
        }

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

85 86
    def test_check_grad(self):
        self.check_grad(['X'], 'Out')
G
guosheng 已提交
87 88


89
class Test1DReduce(OpTest):
G
guosheng 已提交
90
    def setUp(self):
91 92 93 94 95 96 97 98 99
        self.op_type = "reduce_sum"
        self.inputs = {'X': np.random.random(20).astype("float32")}
        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 已提交
100 101


102 103 104 105 106 107 108 109 110 111 112 113 114 115
class TestReduceAll(OpTest):
    def setUp(self):
        self.op_type = "reduce_sum"
        self.inputs = {'X': np.random.random((5, 6, 2, 10)).astype("float32")}
        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 已提交
116 117
if __name__ == '__main__':
    unittest.main()