test_reduce_op.py 3.4 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#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
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#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 已提交
14 15
import unittest
import numpy as np
16
from op_test import OpTest
G
guosheng 已提交
17 18


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

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

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


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

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

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


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

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

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


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

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

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


72
class TestKeepDimReduce(OpTest):
G
guosheng 已提交
73
    def setUp(self):
74
        self.op_type = "reduce_sum"
75
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
76
        self.attrs = {'dim': -2, 'keep_dim': True}
77 78 79 80 81 82
        self.outputs = {
            'Out': self.inputs['X'].sum(axis=self.attrs['dim'], keepdims=True)
        }

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

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


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


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