LSTM的注意机制是一个直接的softmax前馈网络,它接收编码器各个时间步长的隐藏状态和解码器的当前状态。
这两个步骤似乎自相矛盾,我无法理解: 1)前馈网络的输入数量需要预先定义,2)编码器的隐藏状态数是可变的(取决于编码过程中的时间步骤数)。
我是不是误会了什么?另外,培训是否就像我要训练一个常规的编解码网络,还是我必须单独训练注意力机制?
提前谢谢
发布于 2018-03-08 03:18:36
def attention(inputs, size, scope):
with tf.variable_scope(scope or 'attention') as scope:
attention_context_vector = tf.get_variable(name='attention_context_vector',
shape=[size],
regularizer=layers.l2_regularizer(scale=L2_REG),
dtype=tf.float32)
input_projection = layers.fully_connected(inputs, size,
activation_fn=tf.tanh,
weights_regularizer=layers.l2_regularizer(scale=L2_REG))
vector_attn = tf.reduce_sum(tf.multiply(input_projection, attention_context_vector), axis=2, keep_dims=True)
attention_weights = tf.nn.softmax(vector_attn, dim=1)
weighted_projection = tf.multiply(inputs, attention_weights)
outputs = tf.reduce_sum(weighted_projection, axis=1)
return outputs
希望这段代码能帮助你理解注意力是如何运作的。我在我的文档分类工作中使用了这个函数,这是一个lstm-注意模型,与您的编解码模型不同。
https://stackoverflow.com/questions/44443433
复制相似问题