Keras 早停、获取预测值和真实值数据

诸神缄默不语-个人CSDN博文目录

1. 早停

early_stopping_callback = EarlyStopping(monitor='val_loss', patience=epochs_to_wait_for_improve)

这个是叠callback的写法:

early_stopping_callback = EarlyStopping(monitor='val_loss', patience=epochs_to_wait_for_improve)
checkpoint_callback = ModelCheckpoint(model_name+'.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
            steps_per_epoch=len(X_train) / batch_size, validation_data=(X_test, y_test),
            epochs=n_epochs, callbacks=[early_stopping_callback, checkpoint_callback])

手动停止模型训练:在Callback(keras.callbacks.Callback)里面设置self.model.stop_training=True,一般在on_batch_end()中设置

设置在某个监视指标达到阈值后停止训练,可以参考:

def on_batch_end(self, epoch, logs=None):
    current = self.get_monitor_value(logs)
    if current is None:
        return

    if self.monitor_op(current - self.min_delta, self.best):
        self.best = current
        self.wait = 0
        if self.restore_best_weights:
            self.best_weights = self.model.get_weights()
    else:
        self.wait += 1
        if self.wait >= self.patience:
            self.stopped_epoch = epoch
            self.model.stop_training = True
            if self.restore_best_weights:
                if self.verbose > 0:
                    print('Restoring model weights from the end of '
                          'the best epoch')
                self.model.set_weights(self.best_weights)

2. 获取预测值和真实值的数据

待补。参考资料:Keras 在fit-generator中获取验证数据的y_true和y_preds_valueerror: output of generator should be a tuple -CSDN博客 keras使用中fit_generator的一些问题 - 知乎

本文撰写过程中参考的网络资料

  1. python - Is there a way in Keras to immediately stop training? - Stack Overflow
  2. python - Keras: early stopping model saving - Stack Overflow
  3. python - How to tell Keras stop training based on loss value? - Stack Overflow:这个是希望限制loss小于指定值后就停止训练
  4. Keras笔记——ModelCheckpoint-CSDN博客:这个是关于模型checkpoint保存的
  5. keras的fit_generator与callback函数 - 简书
  6. https://github.com/keras-team/keras/blob/master/keras/callbacks.py
  7. 回调函数 Callbacks - Keras 中文文档
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值