一般来说,当你训练一个模型时,你首先做一个前向传递,然后是一个反向传递。前向传播根据您的输入数据进行预测,后向传播根据您的预测的正确程度调整您的模型。
因此,当您想应用模型时,只需将新数据作为输入进行前向传递。
在您的特定示例中,使用 this code,您可以通过查看他们如何运行测试集(从第 286 行开始)来了解它是如何完成的。
# They instantiate the model with is_training=False
mtest = PTBModel(is_training=False, config=eval_config)
# Then they can do a forward pass
test_perplexity = run_epoch(session, mtest, test_data, tf.no_op())
print("Test Perplexity: %.3f" % test_perplexity)
如果你想要实际的预测而不是困惑,那就是 run_epoch 函数中的状态:
cost, state, _ = session.run([m.cost, m.final_state, eval_op],
{m.input_data: x,
m.targets: y,
m.initial_state: state})