test_sequence_concat.py 2.2 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

from __future__ import print_function

import unittest
import numpy as np
19 20
import sys
sys.path.append("../")
C
chengduoZH 已提交
21 22 23 24
from op_test import OpTest


class TestSequenceConcat(OpTest):
25 26 27 28 29
    def setLoD(self):
        self.lod1 = [7, 3]
        self.lod2 = [12, 8]
        self.out_lod = [19, 11]

C
chengduoZH 已提交
30
    def setUp(self):
31 32
        x1 = np.random.random(size=(10, 80)).astype('float64')
        x2 = np.random.random(size=(20, 80)).astype('float64')
33
        self.setLoD()
C
chengduoZH 已提交
34

35 36
        out = np.concatenate((x1[0:self.lod1[0]], x2[0:self.lod2[0]],
                              x1[self.lod1[0]:], x2[self.lod2[0]:]))
C
chengduoZH 已提交
37 38

        self.op_type = "sequence_concat"
39 40 41 42
        self.inputs = {
            'X': [("x1", (x1, [self.lod1])), ("x2", (x2, [self.lod2]))]
        }
        self.outputs = {"Out": (out, [self.out_lod])}
C
chengduoZH 已提交
43 44

    def test_output(self):
45
        self.check_output()
C
chengduoZH 已提交
46 47 48 49 50

    def test_dx(self):
        self.check_grad(inputs_to_check=['x1', 'x2'], output_names="Out")


51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
class TestSequenceConcatCase2(TestSequenceConcat):
    def setLoD(self):
        self.lod1 = [10, 0]
        self.lod2 = [12, 8]
        self.out_lod = [22, 8]


class TestSequenceConcatCase3(TestSequenceConcat):
    def setLoD(self):
        self.lod1 = [10, 0]
        self.lod2 = [20, 0]
        self.out_lod = [30, 0]


class TestSequenceConcatCase4(TestSequenceConcat):
    def setLoD(self):
        self.lod1 = [0, 10]
        self.lod2 = [0, 20]
        self.out_lod = [0, 30]


class TestSequenceConcatCase5(TestSequenceConcat):
    def setLoD(self):
        self.lod1 = [0, 10]
        self.lod2 = [20, 0]
        self.out_lod = [20, 10]


C
chengduoZH 已提交
79 80
if __name__ == '__main__':
    unittest.main()