我已经制作了一个 TensorFlow 模型并让训练部分正常工作。模型训练成功,训练时预测准确。不幸的是,当仅使用模型进行预测时,答案大多是胡言乱语,有时甚至是空白。
这是代码:
def train_model():
loss_track = []
max_batches = 10001
batches_in_epoch = 1000
try:
for batch in range(max_batches):
fd = next_feed() # This is a wrapper that prepares the correct response (decoder_targets) and returns batch_method(query_list) (batch method returns encoder_inputs, encoder_input_lenghts)
_, l_t_a = sess.run([train_op, loss], fd)
loss_track.append(l_t_a)
if batch == 0 or batch % batches_in_epoch == 0:
#fd = random.choice(list(fd))
#print(fd)
print('batch {}'.format(batch))
print(' minibatch loss: {}'.format(sess.run(loss, fd)))
predict_ = sess.run(decoder_prediction, fd)
for i, (inp, pred) in enumerate(zip(fd[encoder_inputs].T, predict_.T)):
index = random.randint(0, len(fd[encoder_inputs].T)-1)
print(' sample {}'.format(i + 1))
print(' Query > {}'.format(batch_to_words(fd[encoder_inputs].T[index])))
print(' Predicted Response > {}'.format(batch_to_words(predict_.T[index])))
if i >= 2:
break
print()
saver.save(sess, MODEL_LOCATION)
except KeyboardInterrupt:
print('Training interrupted.')
def test_model(sentence):
saver.restore(sess, MODEL_LOCATION)
batch__ = []
query = get_formatted_sentence(sentence) # This returns the array of the sentence. ex. ['hello','world']
batch__.append(
words_to_batch(query) # This is the inverse of batch_to_words(), it transforms the sentence array in an int array, based on the word vocabulary position
)
batch, batch_len = batch_method(batch__)
prediction = sess.run(decoder_prediction, feed_dict={encoder_inputs: batch, encoder_inputs_length: batch_len})
batch_response = []
for i in prediction:
batch_response.append(i[0])
print(batch_to_words(batch_response))
这里有什么问题?对于预测部分,我根据需要将查询插入到另一个列表batch_method()中。
请记住,由于某种原因,训练中的预测看起来像这样:([12,5,3,5,1]这就是我能够batch_to_words()直接调用的原因),而在仅预测模式下,它返回如下内容:([[3],[4],[64],[23],[1],[0],[1]]这很奇怪)
我也调用train_model()或test_model()单独调用,从不同时运行(这就是我保存模型的原因——与往常一样,我不确定我是否做得正确)。
在这一点上,我不确定这是可以预料的还是我在某个地方搞砸了。
任何帮助表示赞赏。