test_beam_search_op.py 3.3 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

Y
Yan Chunwei 已提交
17
import logging
18 19
from paddle.fluid.op import Operator, DynamicRecurrentOp
import paddle.fluid.core as core
Y
Yan Chunwei 已提交
20 21 22 23 24 25 26 27 28 29 30
import unittest
import numpy as np


def create_tensor(scope, name, np_data):
    tensor = scope.var(name).get_tensor()
    tensor.set(np_data, core.CPUPlace())
    return tensor


class BeamSearchOpTester(unittest.TestCase):
31 32
    """unittest of beam_search_op"""

Y
Yan Chunwei 已提交
33 34 35
    def setUp(self):
        self.scope = core.Scope()
        self._create_ids()
36
        self._create_pre_scores()
Y
Yan Chunwei 已提交
37 38 39 40
        self._create_scores()
        self._create_pre_ids()
        self.scope.var('selected_ids')
        self.scope.var('selected_scores')
41
        self.scope.var('parent_idx')
Y
Yan Chunwei 已提交
42 43 44 45

    def test_run(self):
        op = Operator(
            'beam_search',
46 47
            pre_ids='pre_ids',
            pre_scores='pre_scores',
Y
Yan Chunwei 已提交
48 49 50 51
            ids='ids',
            scores='scores',
            selected_ids='selected_ids',
            selected_scores='selected_scores',
52
            parent_idx='parent_idx',
Y
Yan Chunwei 已提交
53 54 55
            level=0,
            beam_size=2,
            end_id=0, )
D
dzhwinter 已提交
56
        op.run(self.scope, core.CPUPlace())
Y
Yan Chunwei 已提交
57
        selected_ids = self.scope.find_var("selected_ids").get_tensor()
58
        selected_scores = self.scope.find_var("selected_scores").get_tensor()
59
        parent_idx = self.scope.find_var("parent_idx").get_tensor()
60 61 62 63 64 65 66
        self.assertTrue(
            np.allclose(
                np.array(selected_ids), np.array([4, 2, 3, 8])[:, np.newaxis]))
        self.assertTrue(
            np.allclose(
                np.array(selected_scores),
                np.array([0.5, 0.6, 0.9, 0.7])[:, np.newaxis]))
67
        self.assertEqual(selected_ids.lod(), [[0, 2, 4], [0, 1, 2, 3, 4]])
68 69
        self.assertTrue(
            np.allclose(np.array(parent_idx), np.array([0, 1, 2, 3])))
Y
Yan Chunwei 已提交
70 71

    def _create_pre_ids(self):
72
        np_data = np.array([[1, 2, 3, 4]], dtype='int64')
73 74 75 76 77
        tensor = create_tensor(self.scope, 'pre_ids', np_data)

    def _create_pre_scores(self):
        np_data = np.array([[0.1, 0.2, 0.3, 0.4]], dtype='float32')
        tensor = create_tensor(self.scope, 'pre_scores', np_data)
Y
Yan Chunwei 已提交
78 79

    def _create_ids(self):
80
        self.lod = [[0, 2, 4], [0, 1, 2, 3, 4]]
Y
Yan Chunwei 已提交
81
        np_data = np.array(
82
            [[4, 2, 5], [2, 1, 3], [3, 5, 2], [8, 2, 1]], dtype='int64')
Y
Yan Chunwei 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        tensor = create_tensor(self.scope, "ids", np_data)
        tensor.set_lod(self.lod)

    def _create_scores(self):
        np_data = np.array(
            [
                [0.5, 0.3, 0.2],
                [0.6, 0.3, 0.1],
                [0.9, 0.5, 0.1],
                [0.7, 0.5, 0.1],
            ],
            dtype='float32')
        tensor = create_tensor(self.scope, "scores", np_data)
        tensor.set_lod(self.lod)


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