我们如何使用train_on_batch进行早停? [英] How can we perform early stopping with 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屋!