test_cumsum_op.py 4.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
E
emailweixu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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.

15 16
from __future__ import print_function

E
emailweixu 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
20 21 22
import paddle.fluid.core as core
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
E
emailweixu 已提交
23 24 25 26 27 28 29 30 31 32


class TestSumOp1(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=2)}

    def test_check_output(self):
33
        self.check_output()
E
emailweixu 已提交
34 35

    def test_check_grad(self):
36
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50


class TestSumOp2(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': -1, 'reverse': True}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {
            'Out': np.flip(
                np.flip(
                    self.inputs['X'], axis=2).cumsum(axis=2), axis=2)
        }

    def test_check_output(self):
51
        self.check_output()
E
emailweixu 已提交
52 53

    def test_check_grad(self):
54
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
55 56 57 58 59 60 61 62 63 64


class TestSumOp3(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 1}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}

    def test_check_output(self):
65
        self.check_output()
E
emailweixu 已提交
66 67

    def test_check_grad(self):
68
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
69 70 71 72 73 74 75 76 77 78


class TestSumOp4(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 0}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}

    def test_check_output(self):
79
        self.check_output()
E
emailweixu 已提交
80 81

    def test_check_grad(self):
82
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
83 84 85 86 87


class TestSumOp5(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
Z
zhupengyang 已提交
88
        self.inputs = {'X': np.random.random((5, 20)).astype("float64")}
E
emailweixu 已提交
89 90 91
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}

    def test_check_output(self):
92
        self.check_output()
E
emailweixu 已提交
93 94

    def test_check_grad(self):
95
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
96 97 98 99 100


class TestSumOp7(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
Z
zhupengyang 已提交
101
        self.inputs = {'X': np.random.random((100)).astype("float64")}
E
emailweixu 已提交
102 103 104
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}

    def test_check_output(self):
105
        self.check_output()
E
emailweixu 已提交
106 107

    def test_check_grad(self):
108
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
109 110 111 112 113 114


class TestSumOp8(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
Z
zhupengyang 已提交
115
        a = np.random.random((5, 6, 4)).astype("float64")
E
emailweixu 已提交
116 117 118 119 120 121 122 123 124
        self.inputs = {'X': a}
        self.outputs = {
            'Out': np.concatenate(
                (np.zeros(
                    (5, 6, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                axis=2)
        }

    def test_check_output(self):
125
        self.check_output()
E
emailweixu 已提交
126 127

    def test_check_grad(self):
128
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
129 130


131 132 133 134 135 136 137 138 139 140 141
class BadInputTest(unittest.TestCase):
    def test_error(self):
        with fluid.program_guard(fluid.Program()):

            def test_bad_x():
                data = [1, 2, 3]
                result = fluid.layers.cumsum(data, axis=0)

            self.assertRaises(TypeError, test_bad_x)


E
emailweixu 已提交
142 143
if __name__ == '__main__':
    unittest.main()