我正在为编码器和解码器使用双向 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 您可以看到未应用范围。
