hunkim / word-rnn-tensorflow

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

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

Open FirestarterUA opened 7 years ago

FirestarterUA commented 7 years ago

Hello. Please help my with little problem. I want to write simple Rest API with Flask:


from flask import Flask
import numpy as np
import tensorflow as tf

import argparse
import time
import os
from six.moves import cPickle

from utils import TextLoader
from model import Model

app = Flask(__name__)

def sample(n, pick, width, prime):
    save_dir = './models/tinyshakespeare'
    sample = 1
    with open(os.path.join(save_dir, 'config.pkl'), 'rb') as f:
        saved_args = cPickle.load(f)
    with open(os.path.join(save_dir, 'words_vocab.pkl'), 'rb') as f:
        words, vocab = cPickle.load(f)
    model = Model(saved_args, True)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state(save_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            return model.sample(sess, words, vocab, n, prime, sample, pick, width) 

@app.route("/<int:n>/<int:pick>/<int:width>/<prime>")
def hello(n, pick, width, prime):
    return sample(n, pick, width, prime)

if __name__ == "__main__":

    app.run(debug=True, use_reloader=True)

First time in browser(Good):

http://127.0.0.1:5000/200/2/4/shakespeare text: &C: BIANCA: Believe me, boy; and me, sir, of my troth, I am not so. LADY ANNE: I will not be a man to shrink from him, Or else the duke is great; but to be back'd with the Tower. KING EDWARD IV: Why, here's a man of fourscore three, And make Clowder with the Tower. QUEEN ELIZABETH: Why, then I have not wrong'd. KING RICHARD III: Now, sir, thou liest; and I am loath to see it. Wherein, my lord. KING RICHARD III: Now, sir, thou art not fish; if thou couldst, thou hast not wrong'd. KING RICHARD III: Now, sir, I know you not? KING RICHARD III: Why, let me go: I am in estimation; it is not so far officious; for I am undone! I am too senseless--obstinate, my lord, And give me all. KING RICHARD III: Ay, if you faint, as chaste, by that. BAPTISTA: Where is the matter? DUKE OF YORK: I will not come to me that I am able to see the writing. DUKE OF AUMERLE: My lord, I am a man that mutinies in his own blood, Or in the midst of this bright-shining day, I will not spell. But come, I...

but, the second time : http://127.0.0.1:5000/200/2/4/shakespeare I get an error: ValueError: Variable rnnlm/softmax_w already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "/home/firestarter/word-rnn-tensorflow-master/model.py", line 54, in __init__
    softmax_w = tf.get_variable("softmax_w", [args.rnn_size, args.vocab_size])
  File "api.py", line 22, in sample
    model = Model(saved_args, True)
  File "api.py", line 33, in hello
    return sample(n, pick, width, prime)
FirestarterUA commented 7 years ago

Simple answer:

app.run(threaded=True)

But how to optimise model loading(((