test_sequence_concat.py 4.0 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15
import sys
C
chengduoZH 已提交
16
import unittest
17

C
chengduoZH 已提交
18
import numpy as np
19

20
sys.path.append("../")
C
chengduoZH 已提交
21 22
from op_test import OpTest

H
hong 已提交
23
import paddle
24

C
chengduoZH 已提交
25 26

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

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

37 38 39 40 41 42 43 44
        out = np.concatenate(
            (
                x1[0 : self.lod1[0]],
                x2[0 : self.lod2[0]],
                x1[self.lod1[0] :],
                x2[self.lod2[0] :],
            )
        )
C
chengduoZH 已提交
45 46

        self.op_type = "sequence_concat"
47 48 49 50
        self.inputs = {
            'X': [("x1", (x1, [self.lod1])), ("x2", (x2, [self.lod2]))]
        }
        self.outputs = {"Out": (out, [self.out_lod])}
C
chengduoZH 已提交
51 52

    def test_output(self):
53
        self.check_output()
C
chengduoZH 已提交
54 55 56 57 58

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


59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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]


87 88 89 90
class TestSequenceConcatOpError(unittest.TestCase):
    def test_errors(self):
        def test_input_list():
            # the input type must be list
G
GGBond8488 已提交
91 92 93
            x_data = paddle.static.data(
                name='x', shape=[-1, 4], dtype='float32'
            )
94
            paddle.static.nn.sequence_lod.sequence_concat(input=x_data)
95 96 97 98 99 100

        self.assertRaises(TypeError, test_input_list)

        def test_variable1():
            # the input element type must be Variable
            x1_data = np.array([[3, 5]]).astype('float32')
101

G
GGBond8488 已提交
102 103 104
            y1_data = paddle.static.data(
                name='y1', shape=[-1, 4], dtype='float32'
            )
105 106 107
            paddle.static.nn.sequence_lod.sequence_concat(
                input=[x1_data, y1_data]
            )
108 109 110

        def test_variable2():
            x2_data = np.array([[3, 5]]).astype('float32')
G
GGBond8488 已提交
111 112 113
            y2_data = paddle.static.data(
                name='y2', shape=[-1, 4], dtype='float32'
            )
114 115 116
            paddle.static.nn.sequence_lod.sequence_concat(
                input=[y2_data, x2_data]
            )
117 118 119 120 121 122 123 124 125

        for i in range(2):
            if i == 0:
                self.assertRaises(TypeError, test_variable1)
            else:
                self.assertRaises(TypeError, test_variable2)

        def test_dtype():
            # dtype must be 'float32', 'float64', 'int64'
G
GGBond8488 已提交
126 127 128 129 130 131
            x3_data = paddle.static.data(
                name="x3", shape=[-1, 3, 5], dtype='int32'
            )
            y3_data = paddle.static.data(
                name="y3", shape=[-1, 3, 5], dtype='int16'
            )
132
            input_list = [x3_data, y3_data]
133
            paddle.static.nn.sequence_lod.sequence_concat(input=input_list)
134 135 136 137

        self.assertRaises(TypeError, test_dtype)


C
chengduoZH 已提交
138
if __name__ == '__main__':
H
hong 已提交
139
    paddle.enable_static()
C
chengduoZH 已提交
140
    unittest.main()