【问题标题】:How to use previous output and hidden states from LSTM for the attention mechanism?如何将 LSTM 的先前输出和隐藏状态用于注意力机制?
【发布时间】:2018-07-17 03:14:54
【问题描述】:

我目前正在尝试编写本文中的注意力机制:"Effective Approaches to Attention-based Neural Machine Translation", Luong, Pham, Manning (2015)。 (我将全局注意力与点分数结合使用)。

但是,我不确定如何从 lstm 解码中输入隐藏和输出状态。问题在于,lstm 解码器在时间 t 的输入取决于我需要使用来自 t-1 的输出和隐藏状态来计算的数量。

以下是代码的相关部分:

with tf.variable_scope('data'):
    prob = tf.placeholder_with_default(1.0, shape=())
    X_or = tf.placeholder(shape = [batch_size, timesteps_1, num_input], dtype = tf.float32, name = "input")
    X = tf.unstack(X_or, timesteps_1, 1)
    y = tf.placeholder(shape = [window_size,1], dtype = tf.float32, name = "label_annotation")
    logits = tf.zeros((1,1), tf.float32)

with tf.variable_scope('lstm_cell_encoder'):
    rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [hidden_size, hidden_size]]
    multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
    lstm_outputs, lstm_state =  tf.contrib.rnn.static_rnn(cell=multi_rnn_cell,inputs=X,dtype=tf.float32)
    concat_lstm_outputs = tf.stack(tf.squeeze(lstm_outputs))
    last_encoder_state = lstm_state[-1]

with tf.variable_scope('lstm_cell_decoder'):

    initial_input = tf.unstack(tf.zeros(shape=(1,1,hidden_size2)))
    rnn_decoder_cell = tf.nn.rnn_cell.LSTMCell(hidden_size, state_is_tuple = True)
    # Compute the hidden and output of h_1

    for index in range(window_size):

        output_decoder, state_decoder = tf.nn.static_rnn(rnn_decoder_cell, initial_input, initial_state=last_encoder_state, dtype=tf.float32)

        # Compute the score for source output vector
        scores = tf.matmul(concat_lstm_outputs, tf.reshape(output_decoder[-1],(hidden_size,1)))
        attention_coef = tf.nn.softmax(scores)
        context_vector = tf.reduce_sum(tf.multiply(concat_lstm_outputs, tf.reshape(attention_coef, (window_size, 1))),0)
        context_vector = tf.reshape(context_vector, (1,hidden_size))

        # compute the tilda hidden state \tilde{h}_t=tanh(W[c_t, h_t]+b_t)
        concat_context = tf.concat([context_vector, output_decoder[-1]], axis = 1)
        W_tilde = tf.Variable(tf.random_normal(shape = [hidden_size*2, hidden_size2], stddev = 0.1), name = "weights_tilde", trainable = True)
        b_tilde = tf.Variable(tf.zeros([1, hidden_size2]), name="bias_tilde", trainable = True)
        hidden_tilde = tf.nn.tanh(tf.matmul(concat_context, W_tilde)+b_tilde) # hidden_tilde is [1*64]

        # update for next time step
        initial_input = tf.unstack(tf.reshape(hidden_tilde, (1,1,hidden_size2)))
        last_encoder_state = state_decoder

        # predict the target

        W_target = tf.Variable(tf.random_normal(shape = [hidden_size2, 1], stddev = 0.1), name = "weights_target", trainable = True)
        logit = tf.matmul(hidden_tilde, W_target)
        logits = tf.concat([logits, logit], axis = 0)

    logits = logits[1:]

循环内的部分是我不确定的。当我覆盖变量“initial_input”和“last_encoder_state”时,tensorflow会记住计算图吗?

【问题讨论】:

    标签: tensorflow machine-learning lstm recurrent-neural-network attention-model


    【解决方案1】:

    如果您将tf.contrib.seq2seq.AttentionWrapper 与以下实现之一一起使用,我认为您的模型将大大简化:BahdanauAttentionLuongAttention

    这样就可以在单元级别上连接注意力向量,以便在应用注意力后已经单元输出。来自seq2seq tutorial 的示例:

    cell = LSTMCell(512)
    attention_mechanism = tf.contrib.seq2seq.LuongAttention(512, encoder_outputs)
    attn_cell = tf.contrib.seq2seq.AttentionWrapper(cell, attention_mechanism, attention_size=256)
    

    请注意,这种方式不需要window_size 循环,因为tf.nn.static_rnntf.nn.dynamic_rnn 将实例化被注意力包裹的单元格。


    关于您的问题:您应该区分 python 变量和 tensorflow 图节点:您可以将 last_encoder_state 分配给不同的张量,原始图节点不会因此而改变。这是灵活的,但也可能在结果网络中产生误导——你可能认为你将 LSTM 连接到一个张量,但它实际上是另一个。一般来说,您不应该这样做。

    【讨论】:

    • 再次感谢您的回答,这救了我!所以本质上,您称为attn_cell 的 AttentionWrapper 的输出是解码器 RNN 在每个时间步的输出?
    • 关于循环,如果我理解正确,你是说我发布的代码会表现出正确的行为,因为在 tensorflow 中节点/计算图被记住并且不会被覆盖(如 python 变量)。
    猜你喜欢
    • 2018-07-01
    • 2020-08-11
    • 1970-01-01
    • 2018-09-21
    • 2018-01-20
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多