test_sequence_slice_op.py 2.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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.

15 16
from __future__ import print_function

17 18 19
import unittest
import numpy as np
import sys
20
from op_test import OpTest
21

C
caoying03 已提交
22

23
class TestSequenceSliceOp(OpTest):
24
    def set_data(self):
25
        self.init_test_case()
26
        # only supprot one level LoD
27 28
        x = np.random.random(self.x_dim).astype('float32')
        lod = self.x_lod
29 30
        offset = np.array(self.offset).astype("int64")
        length = np.array(self.length).astype("int64")
31

32
        self.inputs = {'X': (x, lod), 'Offset': offset, 'Length': length}
C
caoying03 已提交
33
        outs = []  #np.zeros((100, 3, 2)).astype('float32')
34 35
        out_lod = [[]]
        lod_offset = 0
36
        for i in range(len(offset)):
37
            sub_x = x[lod_offset + offset[i, 0]:lod_offset + offset[i, 0] +
C
caoying03 已提交
38
                      length[i, 0], :]
39
            outs.append(sub_x)
40 41
            out_lod[0].append(len(sub_x))
            lod_offset += lod[0][i]
42
        outs = np.concatenate(outs, axis=0)
43
        self.outputs = {'Out': (outs, out_lod)}
44

45 46
    def init_test_case(self):
        self.x_dim = (100, 3, 2)
47
        self.x_lod = [[20, 20, 20, 20, 20]]
48 49
        self.offset = [[1], [2], [3], [4], [5]]
        self.length = [[10], [8], [6], [4], [2]]
50

51
    def setUp(self):
52
        self.op_type = "sequence_slice"
53 54 55 56 57 58 59 60
        self.set_data()

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')

C
caoying03 已提交
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
class TestSequenceSliceOpSeqlen0Case0(TestSequenceSliceOp):
    def init_test_case(self):
        self.x_dim = (100, 3, 2)
        self.x_lod = [[20, 30, 0, 30, 20]]
        self.offset = [[1], [2], [0], [4], [5]]
        self.length = [[10], [8], [0], [4], [2]]


class TestSequenceSliceOpSeqlen0Case1(TestSequenceSliceOp):
    def init_test_case(self):
        self.x_dim = (100, 3, 2)
        self.x_lod = [[0, 70, 0, 30, 0]]
        self.offset = [[0], [2], [0], [4], [0]]
        self.length = [[0], [8], [0], [4], [0]]


class TestSequenceSliceOpSeqlen0Case2(TestSequenceSliceOp):
    def init_test_case(self):
        self.x_dim = (100, 3, 2)
        self.x_lod = [[0, 100, 0, 0, 0]]
        self.offset = [[0], [2], [0], [0], [0]]
        self.length = [[0], [8], [0], [0], [0]]


86 87
if __name__ == '__main__':
    unittest.main()