【发布时间】: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