test_sequence_erase_op.py 992 字节
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6
import unittest
import numpy as np
from op_test import OpTest


def sequence_erase(in_seq, lod0, tokens):
7 8 9 10 11 12 13 14 15 16
    new_lod0 = [0]
    out_seq = []
    for i in range(0, len(lod0) - 1):
        num_out = 0
        for dat in in_seq[lod0[i]:lod0[i + 1]]:
            if dat not in tokens:
                out_seq.append(dat)
                num_out += 1
        new_lod0.append(new_lod0[-1] + num_out)
    return np.array(out_seq).astype("int32"), new_lod0
Y
Yibing Liu 已提交
17 18 19 20 21


class TestSequenceEraseOp(OpTest):
    def setUp(self):
        self.op_type = "sequence_erase"
22 23
        in_seq = np.random.randint(0, 10, (30, 1)).astype("int32")
        lod = [[0, 9, 13, 24, 30]]
Y
Yibing Liu 已提交
24
        tokens = [2, 3, 5]
Y
Yibing Liu 已提交
25 26 27 28 29 30 31 32 33 34 35
        out_seq, new_lod0 = sequence_erase(in_seq, lod[0], tokens)
        self.attrs = {'tokens': tokens}
        self.inputs = {'X': (in_seq, lod)}
        self.outputs = {'Out': (out_seq, [new_lod0])}

    def test_check_output(self):
        self.check_output()


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