问题出现背景:博主在同一个tf.name_scope(name)下同时处理音频和文本的序列特征时,使用两次tf.nn.dynamic_rnn()函数时出现该错误:
ValueError: Variable rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
问题出现分析:同时调用两次RNN时,第一次调用RNN时没有任何问题,关键是出在第二次调用RNN时,由于第一次tf.nn.dynamic_rnn()处理音频序列时会产生一个权重,而再次调用RNN,再次使用tf.nn.dynamic_rnn()处理文本序列时,这时候系统里应该存在两个不同的lstm_cell模型,但是系统无法辨别出来,因此会出现kernel already exists。(此处参考https://blog.csdn.net/u013041398/article/details/74941991)
问题解决:需要用tf.variable_scope(name)来定义不同的作用范围。
贴上报错代码:
def dropout():
if self.config.rnn == 'lstm':
cell = tf.contrib.rnn.BasicLSTMCell(self.config.hidden_dim)
else:
cell = tf.contrib.rnn.GRUCell(self.config.hidden_dim)
return tf.contrib.rnn.DropoutWra