【问题标题】:Encoder Decoder model for RNN in tensorflow张量流中RNN的编码器解码器模型
【发布时间】:2019-09-17 07:34:12
【问题描述】:

我正在为编码器和解码器使用双向 RNN 实现编码器解码器模型。由于我在编码器端初始化了双向 RNN,并且与双向 RNN 相关的权重和向量已经初始化,所以当我尝试在解码器端初始化另一个实例时出现以下错误:

ValueError: Variable bidirectional_rnn/fw/gru_cell/w_ru already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?

我尝试在它自己的name_scope 中定义每个,如下所示,但无济于事:

def enc(message, weights, biases):
    message = tf.unstack(message, timesteps_enc, 1)
    fw_cell = rnn.GRUBlockCell(num_hidden_enc)
    bw_cell = rnn.GRUBlockCell(num_hidden_enc)
    with tf.name_scope("encoder"):
        outputs, _, _ = rnn.static_bidirectional_rnn(fw_cell, bw_cell, message, dtype=tf.float32)
    return tf.matmul(outputs[-1], weights) + biases


def dec(codeword, weights, biases):
    codeword = tf.expand_dims(codeword, axis=2)
    codeword = tf.unstack(codeword, timesteps_dec, 1)
    fw_cell = rnn.GRUBlockCell(num_hidden_dec)
    bw_cell = rnn.GRUBlockCell(num_hidden_dec)
    with tf.name_scope("decoder"):
        outputs, _, _ = rnn.static_bidirectional_rnn(fw_cell, bw_cell, codeword, dtype=tf.float32)
    return tf.matmul(outputs[-1], weights) + biases

有人可以提示我做错了什么吗?

【问题讨论】:

  • 尝试将name_scope 换成variable_scope。我不确定它是否仍然有效,但对于旧版本的 TF,不鼓励使用 name_scope。从你的变量名bidirectional_rnn/fw/gru_cell/w_ru 可以看出没有应用范围。
  • 感谢您的回复,这很有效。您能否将其作为答案,以便我将其标记为正确?

标签: python tensorflow deep-learning recurrent-neural-network


【解决方案1】:

只是把它作为答案:

尝试将name_scope 换成variable_scope。我不确定它是否仍然有效,但对于旧版本的 TF,不鼓励使用 name_scope。从你的变量名bidirectional_rnn/fw/gru_cell/w_ru 你可以看到没有应用范围。

【讨论】:

    【解决方案2】:

    有一件事是您不能在同一范围内创建具有相同名称的变量,因此将name_scope 更改为variable_scope 将修复训练。

    另一件事是这样的模型不能用作编码器-解码器模型,因为解码器 RNN 不能是双向的。您确实在训练时拥有整个目标序列,但在推理时,您从左到右生成目标。这意味着您只有前向 RNN 的左侧上下文,但您没有反向 RNN 的正确上下文。

    【讨论】:

    • 感谢您的回复。这是我不知道的事实。非常感谢你。另外,你能帮我解决一下吗?我应该从编码器双向 RNN 中获取所有三个输出并将它们堆叠起来吗?同样从文档中我发现了这个outputs is a length `T` list of outputs (one for each input), which are depth-concatenated forward and backward outputs. output_state_fw is the final state of the forward rnn. output_state_bw is the final state of the backward rnn.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 2017-09-14
    • 2019-07-21
    • 2019-09-11
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多