在上一个问题中,serving_input_receiver_fn探讨了的目的和结构,并在回答中:
def serving_input_receiver_fn():
"""For the sake of the example, let's assume your input to the network will be a 28x28 grayscale image that you'll then preprocess as needed"""
input_images = tf.placeholder(dtype=tf.uint8,
shape=[None, 28, 28, 1],
name='input_images')
# here you do all the operations you need on the images before they can be fed to the net (e.g., normalizing, reshaping, etc). Let's assume "images" is the resulting tensor.
features = {'input_data' : images} # this …Run Code Online (Sandbox Code Playgroud) 假设你正在训练的自定义tf.estimator.Estimator与tf.estimator.train_and_evaluate在类似的设置使用验证数据集@ simlmx的:
classifier = tf.estimator.Estimator(
model_fn=model_fn,
model_dir=model_dir,
params=params)
train_spec = tf.estimator.TrainSpec(
input_fn = training_data_input_fn,
)
eval_spec = tf.estimator.EvalSpec(
input_fn = validation_data_input_fn,
)
tf.estimator.train_and_evaluate(
classifier,
train_spec,
eval_spec
)
Run Code Online (Sandbox Code Playgroud)
通常,当训练数据集的损失继续改善而不是验证数据集时,使用验证数据集来切断训练以防止过度拟合.
目前,tf.estimator.EvalSpec允许用户指定steps评估模型的数量(默认为100).
一个(如果可能不使用tf.contrib函数)如何指定在评估丢失n数量(n * steps)之后终止训练,其中评估损失没有改善,然后将"最佳"模型/检查点(由验证数据集确定)保存到唯一文件名(例如best_validation.checkpoint)