本文介绍了如何在TensorFlow的估计器训练中监视验证损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个问题,即如何在TensorFlow的估计器的训练过程中监视验证损失.我已经询问过类似的问题(在估算器训练期间进行验证),但是它没什么帮助.

I want to ask a question about how to monitor validation loss in the training process of estimators in TensorFlow. I have checked a similar question (validation during training of Estimator) asked before, but it did not help much.

如果我使用估算器来构建模型,则将输入函数提供给Estimator.train()函数.但是无法在训练过程中添加另一个validation_x和validation_y数据.因此,当培训开始时,我只能看到培训损失.当训练过程运行时间更长时,预期训练损失会减少.但是,此信息无助于防止过度拟合.更有价值的信息是验证损失.通常,验证损失是带有时期数的U形.为了防止过度拟合,我们希望找到验证损失最小的时期.

If I use estimators to build a model, I will give an input function to the Estimator.train() function. But there is no way to add another validation_x, and validation_y data in the training process. Therefore, when the training started, I can only see the training loss. The training loss is expected to decrease when the training process running longer. However, this information is not helpful to prevent overfitting. The more valuable information is validation loss. Usually, the validation loss is the U-shape with the number of epochs. To prevent overfitting, we want to find the number of epochs that the validation loss is minimum.

所以这是我的问题.在使用估计量的训练过程中,如何在每个时期获得验证损失?

So this is my problem. How can I get validation loss for each epoch in the training process of using estimators?

推荐答案

您需要创建一个验证input_fn,或者使用estimator.train()和estimator.evaluate()来替代,或者使用pypy来使用tf.estimator.train_and_evaluate()

You need to create a validation input_fn and either use estimator.train() and estimator.evaluate() alternatively or simpy use tf.estimator.train_and_evaluate()

x = ...
y = ...

...

# For example, if x and y are numpy arrays < 2 GB
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
val_dataset = tf.data.Dataset.from_tensor_slices((x_val_, y_val))

...

estimator = ...

for epoch in n_epochs:
    estimator.train(input_fn = train_dataset)
    estimator.evaluate(input_fn = val_dataset)

estimator.evaluate()将计算损失和在model_fn中定义的任何其他指标,并将事件保存在job_dir中的新"eval"目录中.

estimator.evaluate() will compute the loss and any other metrics that are defined in your model_fn and will save the events in a new "eval" directory inside your job_dir.

这篇关于如何在TensorFlow的估计器训练中监视验证损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:16