首页 > 解决方案 > 张量流中RNN的编码器解码器模型

问题描述

我正在为编码器和解码器使用双向 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

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

标签: pythontensorflowdeep-learningrecurrent-neural-network

解决方案


只是把它作为一个答案:

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


推荐阅读