이전에 훈련 된 LSTM을 사용하여 텍스트를 생성하려고합니다. existing solution을 발견했지만 문제는 예외가 발생한다는 것입니다. 이전 라이브러리 사용으로 인해 발생하는 것으로 알고 있습니다. 일부 수정 후 여기에 텍스트 생성을위한 내 마지막 함수의 :Tensorflow - 훈련 된 RNN을 사용하여 텍스트 생성
def generate_text(train_path, num_sentences, rnn_data):
gen_config = get_config()
gen_config.num_steps = 1
gen_config.batch_size = 1
with tf.Graph().as_default(), tf.Session() as session:
initializer = tf.random_uniform_initializer(-gen_config.init_scale,
gen_config.init_scale)
with tf.name_scope("Generate"):
rnn_input = PTBInput(config=gen_config, data=rnn_data, name="GenOut")
with tf.variable_scope("OutModel", reuse=None, initializer=initializer):
mout = PTBModel(is_training=False, config=gen_config, input_=rnn_input)
# Restore variables from disk. TODO: save/load trained models
# saver = tf.train.Saver()
# saver.restore(session, model_path)
# print("Model restored from file " + model_path)
print('Getting Vocabulary')
words = reader.get_vocab(train_path)
mout.initial_state = tf.convert_to_tensor(mout.initial_state)
state = mout.initial_state.eval()
# state = session.run(mout.initial_state)
x = 0 # the id for '<eos>' from the training set //TODO: fix this
word_input = np.matrix([[x]]) # a 2D numpy matrix
text = ""
count = 0
while count < num_sentences:
output_probs, state = session.run([mout.output_probs, mout.final_state],
{mout.input.input_data: word_input,
mout.initial_state: state})
print('Output Probs = ' + str(output_probs[0]))
x = sample(output_probs[0], 0.9)
if words[x] == "<eos>":
text += ".\n\n"
count += 1
else:
text += " " + words[x]
# now feed this new word as input into the next iteration
word_input = np.matrix([[x]])
print(text)
return
는하지만 예외가 얻을 :
FailedPreconditionError (역 추적에 대한 위 참조) : 초기화되지 않은 값을 사용하려고 OutModel/softmax_b [[노드 : OutModel/softmax_b/읽기 = IdentityT = DT_FLOAT, _class = [ "위치 : @ OutModel/softmax_b"], _device = "/ 작업 : 로컬 호스트/복제본 : 0/작업 : 0/cpu : 0"]]
어떻게 해결할 수 있습니까? 그리고 내 코드에 다른 문제가 있습니까?
우리에게 도움이 될 수있는 다른 출력 정보가를 사용하여이 문제를 해결할 수있다? – pypypy
도움이 될만한 게 있는지 잘 모르겠다. 알다시피 -이 예외는 프로그램 도달시에 발생한다 : 'output_probs, state = session.run ([mout.output_probs, mout.final_state] , {mout.input.input_data : word_input, mout.initial_state : 상태} 그 아무것도 –
시도'tf.global_variables_initializer()'그것은 내 문제가 해결이 코드 같은 느낌보세요! 감사합니다! :) 당신은 그런 텍스트 생성 방법에 대해 무엇을 말할 수 있습니까? 뭔가 고쳐야할까요? 맞습니까? – pypypy