test_sequence_reverse.py 3.2 KB
Newer Older
S
sneaxiy 已提交
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
S
sneaxiy 已提交
16
import unittest
17

S
sneaxiy 已提交
18
import numpy as np
19

G
GGBond8488 已提交
20
import paddle
21

22 23
sys.path.append("../")
from op_test import OpTest
S
sneaxiy 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38


class TestSequenceReverseBase(OpTest):
    def initParameters(self):
        pass

    def setUp(self):
        self.size = (10, 3, 4)
        self.lod = [2, 3, 5]
        self.dtype = 'float32'
        self.initParameters()
        self.op_type = 'sequence_reverse'
        self.x = np.random.random(self.size).astype(self.dtype)
        self.y = self.get_output()

39
        self.inputs = {
40 41 42 43 44 45
            'X': (
                self.x,
                [
                    self.lod,
                ],
            ),
46 47
        }
        self.outputs = {
48 49 50 51 52 53
            'Y': (
                self.y,
                [
                    self.lod,
                ],
            ),
54
        }
S
sneaxiy 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67

    def get_output(self):
        tmp_x = np.reshape(self.x, newshape=[self.x.shape[0], -1])
        tmp_y = np.ndarray(tmp_x.shape).astype(self.dtype)
        prev_idx = 0
        for cur_len in self.lod:
            idx_range = range(prev_idx, prev_idx + cur_len)
            tmp_y[idx_range, :] = np.flip(tmp_x[idx_range, :], 0)
            prev_idx += cur_len

        return np.reshape(tmp_y, newshape=self.x.shape).astype(self.dtype)

    def test_output(self):
H
hong 已提交
68
        self.check_output(0, check_dygraph=False)
S
sneaxiy 已提交
69 70

    def test_grad(self):
H
hong 已提交
71
        self.check_grad(['X'], 'Y', check_dygraph=False)
S
sneaxiy 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85


class TestSequenceReserve1(TestSequenceReverseBase):
    def initParameters(self):
        self.size = (12, 10)
        self.lod = [4, 5, 3]


class TestSequenceReverse2(TestSequenceReverseBase):
    def initParameters(self):
        self.size = (12, 10)
        self.lod = [12]


86 87 88 89 90 91
class TestSequenceReverse3(TestSequenceReverseBase):
    def initParameters(self):
        self.size = (12, 10)
        self.lod = [3, 0, 6, 3]


Z
zhangchunle 已提交
92
class TestSequenceReverse4(TestSequenceReverseBase):
93 94 95 96 97
    def initParameters(self):
        self.size = (12, 10)
        self.lod = [0, 2, 10, 0]


98 99 100 101 102
class TestSequenceReverseOpError(unittest.TestCase):
    def test_error(self):
        def test_variable():
            # the input type must be Variable
            x_data = np.random.random((2, 4)).astype("float32")
103
            paddle.static.nn.sequence_lod.sequence_reverse(x=x_data)
104 105 106 107 108

        self.assertRaises(TypeError, test_variable)

        def test_dtype():
            # dtype must be 'float32', 'float64', 'int8', 'int32', 'int64'
109

G
GGBond8488 已提交
110 111 112
            x2_data = paddle.static.data(
                name='x2', shape=[-1, 4], dtype='float16'
            )
113
            paddle.static.nn.sequence_lod.sequence_reverse(x=x2_data)
114 115 116 117

        self.assertRaises(TypeError, test_dtype)


S
sneaxiy 已提交
118 119
if __name__ == '__main__':
    unittest.main()