python - How to resolve the ValueError proj_w in Tensorflow? -
i have created rest webservice execute machine translation modifications in translate.py. if run decode function in translate.py alone, on multiple runs right output. when try run decode function through webservice have created, first time, translation result. on second iteration, error mentioned in title.
this error message : valueerror : variable proj_w exists, disallowed. did mean set reuse=true in varscope?
rest webservice part :
input = request.json['inputtext'] print "'%s'" % input print 'please wait' #import pdb; pdb.set_trace() #out = demo1.decode(input);
decode python script:
def decode(sentence) tf.session() sess: # create model , load parameters. model = create_model(sess, true) model.batch_size = 1 # decode 1 sentence @ time. # load vocabularies. en_vocab_path = os.path.join(flags.data_dir, "vocab%d.en" % flags.en_vocab_size) fr_vocab_path = os.path.join(flags.data_dir, "vocab%d.fr" % flags.fr_vocab_size) en_vocab, _ = data_utils.initialize_vocabulary(en_vocab_path) _, rev_fr_vocab = data_utils.initialize_vocabulary(fr_vocab_path) # decode standard input. #sys.stdout.write("> ") #sys.stdout.flush() #sentence = sys.stdin.readline() print ("reading line %s" % sentence) token_ids = data_utils.sentence_to_token_ids(tf.compat.as_bytes(sentence), en_vocab) bucket_id = min([b b in xrange(len(_buckets)) if _buckets[b][0] > len(token_ids)]) encoder_inputs, decoder_inputs, target_weights = model.get_batch( {bucket_id: [(token_ids, [])]}, bucket_id) _, _, output_logits = model.step(sess, encoder_inputs, decoder_inputs, target_weights, bucket_id, true) outputs = [int(np.argmax(logit, axis=1)) logit in output_logits] if data_utils.eos_id in outputs: outputs = outputs[:outputs.index(data_utils.eos_id)] #print(" ".join([tf.compat.as_str(rev_fr_vocab[output]) output in outputs])) str1 = ([tf.compat.as_str(rev_fr_vocab[output]) output in outputs]) output = ' '.join(str1) print ("output line %s\n" % output) sys.stdout.flush() sess.close() return output
it works first time. next hit on webservice, error "valueerror : variable proj_w exists, disallowed. did mean set reuse=true in varscope?"
with following modifications, able run webservice smoothly. creating model , tf.session() once. earlier created every hit on webservice.
model = none en_vocab_path =none fr_vocab_path =none sess = none def decode(sentence) global sess if sess==none: sess = tf.session() global model global en_vocab_path global fr_vocab_path if model==none: model = create_model(sess, true) model.batch_size = 1 # decode 1 sentence @ time. # load vocabularies. if en_vocab_path==none: en_vocab_path = os.path.join(flags.data_dir, "vocab%d.en" % flags.en_vocab_size) if fr_vocab_path==none: fr_vocab_path = os.path.join(flags.data_dir, "vocab%d.fr" % flags.fr_vocab_size)
Comments
Post a Comment