seqToseq_net_v2.py 3.6 KB
Newer Older
1
import paddle.v2 as paddle
2 3 4 5 6 7 8 9 10


def seqToseq_net_v2(source_dict_dim, target_dict_dim):
    ### Network Architecture
    word_vector_dim = 512  # dimension of word vector
    decoder_size = 512  # dimension of hidden unit in GRU Decoder network
    encoder_size = 512  # dimension of hidden unit in GRU Encoder network

    #### Encoder
11
    src_word_id = paddle.layer.data(
12
        name='source_language_word',
13 14
        type=paddle.data_type.integer_value_sequence(source_dict_dim))
    src_embedding = paddle.layer.embedding(
15 16
        input=src_word_id,
        size=word_vector_dim,
17 18 19 20
        param_attr=paddle.attr.ParamAttr(name='_source_language_embedding'))
    src_forward = paddle.networks.simple_gru(
        input=src_embedding, size=encoder_size)
    src_backward = paddle.networks.simple_gru(
21
        input=src_embedding, size=encoder_size, reverse=True)
22
    encoded_vector = paddle.layer.concat(input=[src_forward, src_backward])
23 24

    #### Decoder
25 26 27
    with paddle.layer.mixed(size=decoder_size) as encoded_proj:
        encoded_proj += paddle.layer.full_matrix_projection(
            input=encoded_vector)
28

29
    backward_first = paddle.layer.first_seq(input=src_backward)
30

31 32 33 34
    with paddle.layer.mixed(
            size=decoder_size, act=paddle.activation.Tanh()) as decoder_boot:
        decoder_boot += paddle.layer.full_matrix_projection(
            input=backward_first)
35 36 37

    def gru_decoder_with_attention(enc_vec, enc_proj, current_word):

38
        decoder_mem = paddle.layer.memory(
39 40
            name='gru_decoder', size=decoder_size, boot_layer=decoder_boot)

41
        context = paddle.networks.simple_attention(
42 43 44 45
            encoded_sequence=enc_vec,
            encoded_proj=enc_proj,
            decoder_state=decoder_mem)

46 47 48 49
        with paddle.layer.mixed(size=decoder_size * 3) as decoder_inputs:
            decoder_inputs += paddle.layer.full_matrix_projection(input=context)
            decoder_inputs += paddle.layer.full_matrix_projection(
                input=current_word)
50

51
        gru_step = paddle.layer.gru_step(
52 53 54 55 56
            name='gru_decoder',
            input=decoder_inputs,
            output_mem=decoder_mem,
            size=decoder_size)

57 58 59 60 61
        with paddle.layer.mixed(
                size=target_dict_dim,
                bias_attr=True,
                act=paddle.activation.Softmax()) as out:
            out += paddle.layer.full_matrix_projection(input=gru_step)
62 63 64
        return out

    decoder_group_name = "decoder_group"
65 66
    group_input1 = paddle.layer.StaticInputV2(input=encoded_vector, is_seq=True)
    group_input2 = paddle.layer.StaticInputV2(input=encoded_proj, is_seq=True)
67 68
    group_inputs = [group_input1, group_input2]

69 70
    trg_embedding = paddle.layer.embedding(
        input=paddle.layer.data(
71
            name='target_language_word',
72
            type=paddle.data_type.integer_value_sequence(target_dict_dim)),
73
        size=word_vector_dim,
74
        param_attr=paddle.attr.ParamAttr(name='_target_language_embedding'))
75 76 77 78 79 80 81
    group_inputs.append(trg_embedding)

    # For decoder equipped with attention mechanism, in training,
    # target embeding (the groudtruth) is the data input,
    # while encoded source sequence is accessed to as an unbounded memory.
    # Here, the StaticInput defines a read-only memory
    # for the recurrent_group.
82
    decoder = paddle.layer.recurrent_group(
83 84 85 86
        name=decoder_group_name,
        step=gru_decoder_with_attention,
        input=group_inputs)

87
    lbl = paddle.layer.data(
88
        name='target_language_next_word',
89 90
        type=paddle.data_type.integer_value_sequence(target_dict_dim))
    cost = paddle.layer.classification_cost(input=decoder, label=lbl)
91 92

    return cost