deeppavlov/core/layers/tf_attention_mechanisms.py
def general_attention(key, context, hidden_size, projected_align=False):
    """ It is a implementation of the Luong et al. attention mechanism with general score. Based on the paper:
        https://arxiv.org/abs/1508.04025 "Effective Approaches to Attention-based Neural Machine Translation"
    Args:
        key: A tensorflow tensor with dimensionality [None, None, key_size]
        context: A tensorflow tensor with dimensionality [None, None, max_num_tokens, token_size]
        hidden_size: Number of units in hidden representation
        projected_align: Using bidirectional lstm for hidden representation of context.
        If true, beetween input and attention mechanism insert layer of bidirectional lstm with dimensionality [hidden_size].
        If false, bidirectional lstm is not used.
    Returns:
        output: Tensor at the output with dimensionality [None, None, hidden_size]
    """

    if hidden_size % 2 != 0:
        raise ValueError("hidden size must be dividable by two")
    batch_size = tf.shape(context)[0]
    max_num_tokens, token_size = context.get_shape().as_list()[-2:]
    r_context = tf.reshape(context, shape=[-1, max_num_tokens, token_size])

    # projected_key: [None, None, hidden_size]
    projected_key = \
        tf.layers.dense(key, hidden_size, kernel_initializer=xav())
    r_projected_key = tf.reshape(projected_key, shape=[-1, hidden_size, 1])

    lstm_fw_cell = tf.nn.rnn_cell.LSTMCell(hidden_size//2)
    lstm_bw_cell = tf.nn.rnn_cell.LSTMCell(hidden_size//2)
    (output_fw, output_bw), states = \
        tf.nn.bidirectional_dynamic_rnn(cell_fw=lstm_fw_cell,
                                        cell_bw=lstm_bw_cell,
                                        inputs=r_context,
                                        dtype=tf.float32)
    # bilstm_output: [-1, max_num_tokens, hidden_size]
    bilstm_output = tf.concat([output_fw, output_bw], -1)

    attn = tf.nn.softmax(tf.matmul(bilstm_output, r_projected_key), dim=1)

    if projected_align:
        log.info("Using projected attention alignment")
        t_context = tf.transpose(bilstm_output, [0, 2, 1])
        output = tf.reshape(tf.matmul(t_context, attn),
                            shape=[batch_size, -1, hidden_size])
    else:
        log.info("Using without projected attention alignment")
        t_context = tf.transpose(r_context, [0, 2, 1])
        output = tf.reshape(tf.matmul(t_context, attn),
                            shape=[batch_size, -1, token_size])
    return output
def general_attention(key, context, hidden_size, projected_align=False):
    """ It is a implementation of the Luong et al. attention mechanism with general score. Based on the paper:
        https://arxiv.org/abs/1508.04025 "Effective Approaches to Attention-based Neural Machine Translation"
    Args:
        key: A tensorflow tensor with dimensionality [None, None, key_size]
        context: A tensorflow tensor with dimensionality [None, None, max_num_tokens, token_size]
        hidden_size: Number of units in hidden representation
        projected_align: Using bidirectional lstm for hidden representation of context.
        If true, beetween input and attention mechanism insert layer of bidirectional lstm with dimensionality [hidden_size].
        If false, bidirectional lstm is not used.
    Returns:
        output: Tensor at the output with dimensionality [None, None, hidden_size]
    """

    if hidden_size % 2 != 0:
        raise ValueError("hidden size must be dividable by two")
    batch_size = tf.shape(context)[0]
    max_num_tokens, token_size = context.get_shape().as_list()[-2:]
    r_context = tf.reshape(context, shape=[-1, max_num_tokens, token_size])

    # projected_key: [None, None, hidden_size]
    projected_key = \
        tf.layers.dense(key, hidden_size, kernel_initializer=xav())
    r_projected_key = tf.reshape(projected_key, shape=[-1, hidden_size, 1])

    lstm_fw_cell = tf.nn.rnn_cell.LSTMCell(hidden_size//2)
    lstm_bw_cell = tf.nn.rnn_cell.LSTMCell(hidden_size//2)
    (output_fw, output_bw), states = \
        tf.nn.bidirectional_dynamic_rnn(cell_fw=lstm_fw_cell,
                                        cell_bw=lstm_bw_cell,
                                        inputs=r_context,
                                        dtype=tf.float32)
    # bilstm_output: [-1, max_num_tokens, hidden_size]
    bilstm_output = tf.concat([output_fw, output_bw], -1)

    attn = tf.nn.softmax(tf.matmul(bilstm_output, r_projected_key), dim=1)

    if projected_align:
        log.info("Using projected attention alignment")
        t_context = tf.transpose(bilstm_output, [0, 2, 1])
        output = tf.reshape(tf.matmul(t_context, attn),
                            shape=[batch_size, -1, hidden_size])
    else:
        log.info("Using without projected attention alignment")
        t_context = tf.transpose(r_context, [0, 2, 1])
        output = tf.reshape(tf.matmul(t_context, attn),
                            shape=[batch_size, -1, token_size])
    return output
def general_attention ( key , context , hidden_size , projected_align = False ) : if hidden_size % 2 != 0 : raise ValueError ( "hidden size must be dividable by two" ) batch_size = tf . shape ( context ) [ 0 ] max_num_tokens , token_size = context . get_shape ( ) . as_list ( ) [ - 2 : ] r_context = tf . reshape ( context , shape = [ - 1 , max_num_tokens , token_size ] ) # projected_key: [None, None, hidden_size] projected_key = tf . layers . dense ( key , hidden_size , kernel_initializer = xav ( ) ) r_projected_key = tf . reshape ( projected_key , shape = [ - 1 , hidden_size , 1 ] ) lstm_fw_cell = tf . nn . rnn_cell . LSTMCell ( hidden_size // 2 ) lstm_bw_cell = tf . nn . rnn_cell . LSTMCell ( hidden_size // 2 ) ( output_fw , output_bw ) , states = tf . nn . bidirectional_dynamic_rnn ( cell_fw = lstm_fw_cell , cell_bw = lstm_bw_cell , inputs = r_context , dtype = tf . float32 ) # bilstm_output: [-1, max_num_tokens, hidden_size] bilstm_output = tf . concat ( [ output_fw , output_bw ] , - 1 ) attn = tf . nn . softmax ( tf . matmul ( bilstm_output , r_projected_key ) , dim = 1 ) if projected_align : log . info ( "Using projected attention alignment" ) t_context = tf . transpose ( bilstm_output , [ 0 , 2 , 1 ] ) output = tf . reshape ( tf . matmul ( t_context , attn ) , shape = [ batch_size , - 1 , hidden_size ] ) else : log . info ( "Using without projected attention alignment" ) t_context = tf . transpose ( r_context , [ 0 , 2 , 1 ] ) output = tf . reshape ( tf . matmul ( t_context , attn ) , shape = [ batch_size , - 1 , token_size ] ) return output
It is a implementation of the Luong et al. attention mechanism with general score. Based on the paper:
        https://arxiv.org/abs/1508.04025 "Effective Approaches to Attention-based Neural Machine Translation"
    Args:
        key: A tensorflow tensor with dimensionality [None, None, key_size]
        context: A tensorflow tensor with dimensionality [None, None, max_num_tokens, token_size]
        hidden_size: Number of units in hidden representation
        projected_align: Using bidirectional lstm for hidden representation of context.
        If true, beetween input and attention mechanism insert layer of bidirectional lstm with dimensionality [hidden_size].
        If false, bidirectional lstm is not used.
    Returns:
        output: Tensor at the output with dimensionality [None, None, hidden_size]
It is a implementation of the Luong et al . attention mechanism with general score . Based on the paper : https : // arxiv . org / abs / 1508 . 04025 Effective Approaches to Attention - based Neural Machine Translation Args : key : A tensorflow tensor with dimensionality [ None None key_size ] context : A tensorflow tensor with dimensionality [ None None max_num_tokens token_size ] hidden_size : Number of units in hidden representation projected_align : Using bidirectional lstm for hidden representation of context . If true beetween input and attention mechanism insert layer of bidirectional lstm with dimensionality [ hidden_size ] . If false bidirectional lstm is not used . Returns : output : Tensor at the output with dimensionality [ None None hidden_size ]