infer.py 7.6 KB
Newer Older
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
from __future__ import print_function
16 17 18 19 20 21 22 23 24 25 26
import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.framework as framework
import paddle.fluid.layers as pd
from paddle.fluid.executor import Executor
import os

dict_size = 30000
source_dict_dim = target_dict_dim = dict_size
word_dim = 32
G
guosheng 已提交
27 28
hidden_dim = 32
decoder_size = hidden_dim
29 30
max_length = 8
beam_size = 2
G
guosheng 已提交
31
batch_size = 2
32 33 34 35 36 37 38 39 40 41

is_sparse = True
model_save_dir = "machine_translation.inference.model"


def encoder():
    src_word_id = pd.data(
        name="src_word_id", shape=[1], dtype='int64', lod_level=1)
    src_embedding = pd.embedding(
        input=src_word_id,
G
guosheng 已提交
42
        size=[source_dict_dim, word_dim],
43
        dtype='float32',
G
guosheng 已提交
44
        is_sparse=is_sparse)
45

G
guosheng 已提交
46 47 48 49 50 51 52 53 54
    fc_forward = pd.fc(
        input=src_embedding, size=hidden_dim * 3, bias_attr=False)
    src_forward = pd.dynamic_gru(input=fc_forward, size=hidden_dim)
    fc_backward = pd.fc(
        input=src_embedding, size=hidden_dim * 3, bias_attr=False)
    src_backward = pd.dynamic_gru(
        input=fc_backward, size=hidden_dim, is_reverse=True)
    encoded_vector = pd.concat(input=[src_forward, src_backward], axis=1)
    return encoded_vector
55 56


G
guosheng 已提交
57 58 59 60
def decode(encoder_out):
    encoder_last = pd.sequence_last_step(input=encoder_out)
    encoder_last_projected = pd.fc(
        input=encoder_last, size=decoder_size, act='tanh')
61

G
guosheng 已提交
62 63
    max_len = pd.fill_constant(shape=[1], dtype='int64', value=max_length)
    counter = pd.zeros(shape=[1], dtype='int64', force_cpu=True)
64 65 66 67 68

    init_ids = pd.data(name="init_ids", shape=[1], dtype="int64", lod_level=2)
    init_scores = pd.data(
        name="init_scores", shape=[1], dtype="float32", lod_level=2)

G
guosheng 已提交
69 70
    # arrays to save selected ids and corresponding scores for each step
    ids_array = pd.create_array('int64')
71
    pd.array_write(init_ids, array=ids_array, i=counter)
G
guosheng 已提交
72
    scores_array = pd.create_array('float32')
73 74
    pd.array_write(init_scores, array=scores_array, i=counter)

G
guosheng 已提交
75 76 77 78 79
    # arrays to save states and context for each step
    state_array = pd.create_array('float32')
    pd.array_write(encoder_last_projected, array=state_array, i=counter)
    context_array = pd.create_array('float32')
    pd.array_write(encoder_last, array=state_array, i=counter)
80

G
guosheng 已提交
81
    cond = pd.less_than(x=counter, y=max_len)
82 83 84 85
    while_op = pd.While(cond=cond)
    with while_op.block():
        pre_ids = pd.array_read(array=ids_array, i=counter)
        pre_score = pd.array_read(array=scores_array, i=counter)
G
guosheng 已提交
86 87
        pre_state = pd.array_read(array=state_array, i=counter)
        pre_context = pd.array_read(array=context_array, i=counter)
88

G
guosheng 已提交
89
        # cell calculations
90 91
        pre_ids_emb = pd.embedding(
            input=pre_ids,
G
guosheng 已提交
92
            size=[target_dict_dim, word_dim],
93
            dtype='float32',
G
guosheng 已提交
94 95 96 97 98 99 100
            is_sparse=is_sparse)
        decoder_inputs = pd.fc(
            input=[pre_ids_emb, pre_context],
            size=decoder_size * 3,
            bias_attr=False)
        current_state = pd.gru_unit(
            input=decoder_inputs, hidden=pre_state, size=decoder_size)
101 102
        current_state_with_lod = pd.lod_reset(x=current_state, y=pre_score)
        current_score = pd.fc(
G
guosheng 已提交
103 104 105
            input=current_state, size=target_dict_dim, act='softmax')

        # beam search
106 107 108
        topk_scores, topk_indices = pd.topk(current_score, k=beam_size)
        accu_scores = pd.elementwise_add(
            x=pd.log(topk_scores), y=pd.reshape(pre_score, shape=[-1]), axis=0)
109
        selected_ids, selected_scores = pd.beam_search(
110 111 112 113 114 115 116
            pre_ids,
            pre_score,
            topk_indices,
            accu_scores,
            beam_size,
            end_id=10,
            level=0)
117

G
guosheng 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
        pd.increment(x=counter, value=1, in_place=True)
        # update states
        pd.array_write(selected_ids, array=ids_array, i=counter)
        pd.array_write(selected_scores, array=scores_array, i=counter)
        # update rnn state by sequence_expand acting as gather
        current_state = pd.sequence_expand(current_state, selected_ids)
        current_context = pd.sequence_expand(pre_context, selected_ids)
        pd.array_write(current_state, array=state_array, i=counter)
        pd.array_write(current_context, array=context_array, i=counter)

        # update conditional variable 
        length_cond = pd.less_than(x=counter, y=array_len)
        finish_cond = pd.logical_not(pd.is_empty(x=selected_ids))
        pd.logical_and(x=length_cond, y=finish_cond, out=cond)
132 133

    translation_ids, translation_scores = pd.beam_search_decode(
134
        ids=ids_array, scores=scores_array, beam_size=beam_size, end_id=10)
135 136 137 138 139 140 141 142 143 144 145 146

    return translation_ids, translation_scores


def decode_main(use_cuda):
    if use_cuda and not fluid.core.is_compiled_with_cuda():
        return
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()

    exe = Executor(place)
    exe.run(framework.default_startup_program())

G
guosheng 已提交
147 148
    encoder_out = encoder()
    translation_ids, translation_scores = decode(encoder_out)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    fluid.io.load_persistables(executor=exe, dirname=model_save_dir)

    init_ids_data = np.array([1 for _ in range(batch_size)], dtype='int64')
    init_scores_data = np.array(
        [1. for _ in range(batch_size)], dtype='float32')
    init_ids_data = init_ids_data.reshape((batch_size, 1))
    init_scores_data = init_scores_data.reshape((batch_size, 1))
    init_lod = [1] * batch_size
    init_lod = [init_lod, init_lod]

    init_ids = fluid.create_lod_tensor(init_ids_data, init_lod, place)
    init_scores = fluid.create_lod_tensor(init_scores_data, init_lod, place)

    test_data = paddle.batch(
        paddle.reader.shuffle(
            paddle.dataset.wmt14.test(dict_size), buf_size=1000),
        batch_size=batch_size)

    feed_order = ['src_word_id']
    feed_list = [
        framework.default_main_program().global_block().var(var_name)
        for var_name in feed_order
    ]
    feeder = fluid.DataFeeder(feed_list, place)

    src_dict, trg_dict = paddle.dataset.wmt14.get_dict(dict_size)

    for data in test_data():
        feed_data = map(lambda x: [x[0]], data)
        feed_dict = feeder.feed(feed_data)
        feed_dict['init_ids'] = init_ids
        feed_dict['init_scores'] = init_scores

        results = exe.run(
            framework.default_main_program(),
            feed=feed_dict,
            fetch_list=[translation_ids, translation_scores],
            return_numpy=False)

        result_ids = np.array(results[0])
R
root 已提交
189
        result_ids_lod = results[0].lod()
190 191 192
        result_scores = np.array(results[1])

        print("Original sentence:")
193 194 195 196 197 198 199 200
        print(" ".join([src_dict[w] for w in feed_data[0][0][1:-1]]))
        print("Translated score and sentence:")
        for i in xrange(beam_size):
            start_pos = result_ids_lod[1][i] + 1
            end_pos = result_ids_lod[1][i + 1]
            print("%d\t%.4f\t%s\n" % (
                i + 1, result_scores[end_pos - 1],
                " ".join([trg_dict[w] for w in result_ids[start_pos:end_pos]])))
201 202 203 204 205 206 207 208 209 210 211

        break


def main(use_cuda):
    decode_main(False)  # Beam Search does not support CUDA


if __name__ == '__main__':
    use_cuda = os.getenv('WITH_GPU', '0') != '0'
    main(use_cuda)