gpt4 book ai didi

python - 如何使用注意力机制在多层双向中操纵编码器状态

转载 作者:太空宇宙 更新时间:2023-11-03 21:21:13 25 4
gpt4 key购买 nike

我正在实现一个具有多层双向 rnn 和注意力机制的 Seq2Seq 模型,同时遵循本教程 https://github.com/tensorflow/nmt我对如何在双向层之后正确操作encoder_state感到困惑。

引用教程“对于多个双向层,我们需要对encoder_state进行一些操作,请参阅model.py的方法_build_bidirection_rnn()以了解更多详细信息。”这是代码的相关部分(https://github.com/tensorflow/nmt/blob/master/nmt/model.py第770行):

encoder_outputs, bi_encoder_state = (
self._build_bidirectional_rnn(
inputs=self.encoder_emb_inp,
sequence_length=sequence_length,
dtype=dtype,
hparams=hparams,
num_bi_layers=num_bi_layers,
num_bi_residual_layers=num_bi_residual_layers))

if num_bi_layers == 1:
encoder_state = bi_encoder_state
else:
# alternatively concat forward and backward states
encoder_state = []
for layer_id in range(num_bi_layers):
encoder_state.append(bi_encoder_state[0][layer_id]) # forward
encoder_state.append(bi_encoder_state[1][layer_id]) # backward
encoder_state = tuple(encoder_state)

这就是我现在所拥有的:

def get_a_cell(lstm_size):
lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
#drop = tf.nn.rnn_cell.DropoutWrapper(lstm,
output_keep_prob=keep_prob)
return lstm


encoder_FW = tf.nn.rnn_cell.MultiRNNCell(
[get_a_cell(num_units) for _ in range(num_layers)])
encoder_BW = tf.nn.rnn_cell.MultiRNNCell(
[get_a_cell(num_units) for _ in range(num_layers)])


bi_outputs, bi_encoder_state = tf.nn.bidirectional_dynamic_rnn(
encoder_FW, encoder_BW, encoderInput,
sequence_length=x_lengths, dtype=tf.float32)
encoder_output = tf.concat(bi_outputs, -1)

encoder_state = []

for layer_id in range(num_layers):
encoder_state.append(bi_encoder_state[0][layer_id]) # forward
encoder_state.append(bi_encoder_state[1][layer_id]) # backward
encoder_state = tuple(encoder_state)

#DECODER -------------------

decoder_cell = tf.nn.rnn_cell.MultiRNNCell([get_a_cell(num_units) for _ in range(num_layers)])

# Create an attention mechanism
attention_mechanism = tf.contrib.seq2seq.LuongAttention(num_units_attention, encoder_output ,memory_sequence_length=x_lengths)

decoder_cell = tf.contrib.seq2seq.AttentionWrapper(
decoder_cell,attention_mechanism,
attention_layer_size=num_units_attention)

decoder_initial_state = decoder_cell.zero_state(batch_size,tf.float32)
.clone(cell_state=encoder_state)

问题是我收到错误

The two structures don't have the same nested structure.

First structure: type=AttentionWrapperState
str=AttentionWrapperState(cell_state=(LSTMStateTuple(c=, h=),
LSTMStateTuple(c=, h=)), attention=, time=, alignments=, alignment_history=
(), attention_state=)

Second structure: type=AttentionWrapperState
str=AttentionWrapperState(cell_state=(LSTMStateTuple(c=, h=),
LSTMStateTuple(c=, h=), LSTMStateTuple(c=, h=), LSTMStateTuple(c=, h=)),
attention=, time=, alignments=, alignment_history=(), attention_state=)

这对我来说有点意义,因为我们不包括所有层的输出,但(我猜)只包括最后一层。而对于状态,我们实际上是连接所有层。

正如我所期望的,当仅连接最后一层状态时,如下所示:

encoder_state = []
encoder_state.append(bi_encoder_state[0][num_layers-1]) # forward
encoder_state.append(bi_encoder_state[1][num_layers-1]) # backward
encoder_state = tuple(encoder_state)

运行没有错误。

据我所知,在将编码器状态传递到注意层之前,没有任何代码部分会再次对其进行转换。那么他们的代码如何工作呢?更重要的是,我的修复是否破坏了注意力机制的正确行为?

最佳答案

问题是这样的:

只有编码器是双向的,但您向解码器提供双状态(始终是单向的)。

解决方案:

您要做的就是简单地连接状态,因此,您再次操作“单向数据”!

encoder_state = []

for layer_id in range(num_layers):
state_fw = bi_encoder_state[0][layer_id]
state_bw = bi_encoder_state[1][layer_id]

# Merging the fw state and the bw state
cell_state = tf.concat([state_fw.c, state_bw.c], 1)
hidden_state= tf.concat([state_fw.h, state_bw.h], 1)

# This state as the same structure than an uni-directional encoder state
state = tf.nn.rnn_cell.LSTMStateTuple(c=cell_state, h=hidden_state)

encoder_state.append(state)

encoder_state = tuple(encoder_state)

关于python - 如何使用注意力机制在多层双向中操纵编码器状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54232350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com