自定义估计器在训练后调用评估时卡住

自定义估计器在训练后调用评估时卡住

本文介绍了TensorFlow 自定义估计器在训练后调用评估时卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TensorFlow (v1.10) 基于他们的指南.

I made a custom estimator (see this colab) in TensorFlow (v1.10) based on their guide.

我用以下方法训练了玩具模型:

I trained the toy model with:

tf.estimator.train_and_evaluate(est, train_spec, eval_spec)

然后,使用一些测试集数据,尝试使用以下方法评估模型:

and then, with some test set data, try to evaluate the model with:

test_fn = lambda: input_fn(DATASET['test'], run_params)
test_res = est.evaluate(input_fn=test_fn)

(其中 train_fnvalid_fn 在功能上与 test_fn 相同,例如足以用于 tf.estimator.train_and_evaluate 上班).

(where the train_fn and valid_fn are functionally identical to test_fn, e.g. sufficient for tf.estimator.train_and_evaluate to work).

我希望会发生一些事情,但这就是我得到的:

I would expect something to happen, however this is what I get:

INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2018-11-09-13:38:44
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from ./test/model.ckpt-100
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.

然后它就永远运行了.

怎么会?

推荐答案

这是因为你无限期地重复数据集:

This is because you repeat the dataset indefinitely:

# In input_fn
dataset = dataset.repeat().batch(batch_size)

默认情况下,estimator.evaluate() 会一直运行,直到 input_fn 引发输入结束异常.因为您无限期地重复测试数据集,所以它永远不会引发异常并继续运行.

By default, estimator.evaluate() runs until the input_fn raises an end-of-input exception. Because you repeat the test dataset indefinitely, it never raises the exception and keeps running.

您可以在测试时删除重复,也可以使用原始 'eval_spec' 中使用的 'steps' 参数对给定数量的步骤进行评估.

You can either remove the repeat when testing, or run the evaluation for a given number of steps using the 'steps' argument as it is used in your original 'eval_spec'.

这篇关于TensorFlow 自定义估计器在训练后调用评估时卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:54