How to load a matrix to change the attention layer in seqToseq demo? - Paddle
Asked Answered
F

0

6

While attempting to replicate the section 3.1 in Incorporating Discrete Translation Lexicons into Neural MT in paddle-paddle

I tried to have a static matrix that I'll need to load into the seqToseq training pipeline, e.g.:

>>> import numpy as np
>>> x = np.random.rand(3,2)  
>>> x
array([[ 0.64077103,  0.03278357],
       [ 0.47133411,  0.16309775],
       [ 0.63986919,  0.07130613]])
# where there is 3 target words and 2 source words, 
# and each cell in the matrix represents some co-occurrence probabilities.

With the seqToseq_net demo, this matrix would need to be multiplied to the attention layer output in the gru_decoder_with_attention. The original demo:

def gru_decoder_with_attention(enc_vec, enc_proj, current_word):
    decoder_mem = memory(name='gru_decoder',
                         size=decoder_size,
                         boot_layer=decoder_boot)

    # This attention context layer would have been 
    # a vector of size |src_vocab| x 1
    context = simple_attention(encoded_sequence=enc_vec,
                               encoded_proj=enc_proj,
                               decoder_state=decoder_mem, )

    with mixed_layer(size=decoder_size * 3) as decoder_inputs:
        decoder_inputs += full_matrix_projection(input=context)
        decoder_inputs += full_matrix_projection(input=current_word)

    gru_step = gru_step_layer(name='gru_decoder',
                              input=decoder_inputs,
                              output_mem=decoder_mem,
                              size=decoder_size)

    with mixed_layer(size=target_dict_dim,
                     bias_attr=True,
                     act=SoftmaxActivation()) as out:
        out += full_matrix_projection(input=gru_step)
    return out

The goal is to affect the attention layer by multiplying it with the static matrix:

def gru_decoder_with_attention(enc_vec, enc_proj, current_word):
    decoder_mem = memory(name='gru_decoder',
                         size=decoder_size,
                         boot_layer=decoder_boot)

    # This attention context layer would have been 
    # of size |src_vocab| x 1
    context = simple_attention(encoded_sequence=enc_vec,
                               encoded_proj=enc_proj,
                               decoder_state=decoder_mem, )

    # This static matrix layer, x, would have been
    # of size |trg_vocab| x |src_vocab|
    static_matrix = some_sort_of_layer(x)

    # This should yield a vector of size
    # |trg_vocab| x 1
    static_matrix_multiply_context = some_sort_of_operation_layer( static_matrix, context)

    with mixed_layer(size=decoder_size * 3) as decoder_inputs:
        # 
        decoder_inputs += full_matrix_projection(input= static_matrix_multiply_context)
        decoder_inputs += full_matrix_projection(input=current_word)

I've tried looking through the code in Paddle/python/trainer_config_helps and walked-through all the demo code and I've also asked on PaddlePaddle's gitter. But I can't find how can I load a customized static matrix that doesn't need to be updated in the training process and interact with one of Paddle's layer.

How to load a matrix to change the attention layer in seqToseq demo?

What should some_sort_of_layer and some_sort_of_operation_layer be in the above example?

Folberth answered 17/10, 2016 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.