hunkim / word-rnn-tensorflow

Multi-layer Recurrent Neural Networks (LSTM, RNN) for word-level language models in Python using TensorFlow.
MIT License
1.31k stars 495 forks source link

Generating words from trained RNN model: “Variable already exists, disallowed. Did you mean to set reuse=True in VarScope? ” #63

Open sunsidazzz opened 6 years ago

sunsidazzz commented 6 years ago

So I am trying to implemented this RNN word generator model in jupytor notebook. When I was trying to use the trained model to generate some words:

with open(os.path.join(cfgs['save_dir'], 'config.pkl'), 'rb') as f:
   saved_args = cPickle.load(f)

with open(os.path.join(cfgs['save_dir'], 'words_vocab.pkl'), 'rb') as f:
   words, vocab = cPickle.load(f)

with tf.Session() as sess:
   model = Model(saved_args, True)
   tf.global_variables_initializer().run()
   saver = tf.train.Saver(tf.global_variables())
   ckpt = tf.train.get_checkpoint_state(cfgs['save_dir'])
   if ckpt and ckpt.model_checkpoint_path:
       saver.restore(sess, ckpt.model_checkpoint_path)
       print(model.sample(sess, words, vocab, cfgs['n'], cfgs['prime'], cfgs['sample'], cfgs['pick'], cfgs['width']))

It works for the first time, but if I run the code again there is an error:

ValueError: Variable rnnlm/softmax_w already exists, disallowed. Did you mean to set reuse=True in VarScope? 

Right now I have to shut down the ipynb file then run the code to get a new sample. Any idea to change the code to avoid this situation?

suzil commented 6 years ago

You could also try adding this line in the code tf.reset_default_graph() before the new session is created.