我们如何使用train_on_batch进行早停? [英] How can we perform early stopping with train_on_batch?

查看:367
本文介绍了我们如何使用train_on_batch进行早停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手动在循环中运行时期,并在循环中进一步嵌套迷你批生产.在每个小批量生产中,我都需要调用train_on_batch来启用自定义模型的训练.

I manually run the epochs in a loop, as well as further nested mini-batches in the loop. At each mini-batch, I need to call train_on_batch, to enable the training of a customized model.

是否有手动方法来恢复提前停止的功能,即打破循环?

Is there a manual way to restore the functionality of early stopping, i.e. to break the loop?

推荐答案

在实践中,提前停止"主要通过以下方式完成:(1)训练X个时期,(2)保存模型每次达到新的最佳性能时,(3)选择最佳型号. 最佳性能"定义为达到最高(例如准确性)或最低(例如损失)验证指标-以下示例脚本:

In practice, 'early stopping' is largely done via: (1) train for X epochs, (2) save the model each time it achieves a new best performance, (3) select the best model. "Best performance" defined as achieving the highest (e.g. accuracy) or lowest (e.g. loss) validation metric - example script below:

best_val_loss = 999 # arbitrary init - should be high if 'best' is low, and vice versa
num_epochs = 5
epoch = 0

while epoch < num_epochs:
    model.train_on_batch(x_train, y_train)  # get x, y somewhere in the loop
    val_loss = model.evaluate(x_val, y_val)

    if val_loss < best_val_loss:
        model.save(best_model_path) # OR model.save_weights()
        print("Best model w/ val loss {} saved to {}".format(val_loss, best_model_path))
    # ...
    epoch += 1

请参见保存Keras模型.如果您希望直接早停,则定义一些指标(即条件)即可结束火车循环.例如,

See saving Keras models. If you rather early-stop directly, then define some metric - i.e. condition - that'll end the train loop. For example,

while True:
    loss = model.train_on_batch(...)
    if loss < .02:
        break

这篇关于我们如何使用train_on_batch进行早停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆