Skip to main content
 首页 » 编程设计

python之TensorFlow embedding_attention_seq2seq 方法是否默认实现双向 RNN 编码器

2024年08月21日2998°冷暖

我已将 embedding_attention_seq2seq 模块用于机器翻译任务,如指定教程中所述:

https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html

在教程中指定模型的 seq2seq_model.py 中,我注意到如果 use_lstm 设置为 false,他们默认使用 GRUCell 在这些行中:

# Create the internal multi-layer cell for our RNN. 
single_cell = tf.nn.rnn_cell.GRUCell(size) 
if use_lstm: 
  single_cell = tf.nn.rnn_cell.BasicLSTMCell(size) 
cell = single_cell 
if num_layers > 1: 
  cell = tf.nn.rnn_cell.MultiRNNCell([single_cell] * num_layers) 

现在论文中描述的attention机制here如果编码器是双向的并且上下文化考虑了两个隐藏层参数,那么本教程指定为模型实现的语义更有意义。 seq2seq_model 文件没有提及双向组件。

所以我的问题是,embedding_attention_seq2seq 是否默认实现双向 RNN 编码器?

如果不是,它是否只是采用普通 LSTM 编码器每个时间步的隐藏层输出,从而将上下文限制为仅出现在句子中的单词之前?

请您参考如下方法:

So my question is, does the embedding_attention_seq2seq implement a bidirectional RNN Encoder by default?

不,它没有实现双向 RNN 编码器。编码器的输出(用于构建注意力状态)在 embedding_attention_seq2seq 的前几行中构建:

# Encoder. 
encoder_cell = rnn_cell.EmbeddingWrapper( 
    cell, embedding_classes=num_encoder_symbols, 
    embedding_size=embedding_size) 
encoder_outputs, encoder_state = rnn.rnn( 
    encoder_cell, encoder_inputs, dtype=dtype) 

第一行用嵌入包裹了单元格。第二个在 encoder_inputs 上运行 encoder_cell(tf/python/ops/rnn.py 的第 210-228 行)。

If not, does it simply take the hidden layer outputs of each time step of an ordinary LSTM Encoder, thereby limiting the context to only the words in the sentence that have occurred before it?

没错。