转自:https://blog.csdn.net/u010420283/article/details/80295270
在尝试深度学习LSTM序列模型实验时候,参考网上的实验代码(链接),发现训练模型和预测模型不能同时运行,会报错:ValueError: Variable train/rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: 。网上查阅发现是变量作用域的问题,解决方案有说使用tensorflow的reset_default_graph()函数(这里),有说定义命名范围的(这里),由于对TensorFlow不太熟悉,不知道怎么定义我的代码中作用域,后来使用这个简单粗暴的方法解决了,给坑友提供参考。
原来是:
-
train_lstm()
-
prediction()
-
with tf.variable_scope( 'train'):
-
train_lstm()
-
with tf.variable_scope( 'train',reuse= True):
-
prediction()
-