简体   繁体   English

如何在 n 个时期后调用回调,但始终在训练的最后一个时期?

[英]How to call callback after n epochs but always in the last epoch of training?

I want to call a callback after n epochs, but always in the last epoch of training.我想在 n 个时期之后调用回调,但总是在训练的最后一个时期。 Here explains how I can call the callback after n epochs. 这里解释了如何在 n 个 epoch 之后调用回调。

At the moment I am using the following approach:目前我正在使用以下方法:

class MyCallBack(keras.callbacks.Callback):

    def on_epoch_end(self, epoch, log=None)

        if epoch % 10 == 0:  # <- add additional condition here
            self._do_the_stuff()
            
            
    def _do_the_stuff(self):
        print('Do the stuff')
        
        
    def on_training_end(self, logs=None):
        self._do_the_stuff()

Is there a simpler way where I add an additional condition to the if statement inside on_epoch_end and don't need on_training_end ?有没有更简单的方法可以在on_epoch_end内的 if 语句中添加一个额外的条件并且不需要on_training_end

As suggested by @Ewran in the comments above, it is possible to access the total number of epochs by `self.params['epochs'].正如@Ewran 在上面的评论中所建议的那样,可以通过 `self.params['epochs'] 访问 epoch 的总数。

class MyCallBack(keras.callbacks.Callback):

    def on_epoch_end(self, epoch, log=None)

        if epoch % self.epoch_freq == 0 or epoch == self.params.get('epochs', -1):
            self._do_the_stuff()
            
            
    def _do_the_stuff(self):
        print('Do the stuff')
        
        
    def on_training_end(self, logs=None):
        self._do_the_stuff()

If other callbacks such as tf.keras.callbacks.EarlyStopping are used, I would continue to use the approach with on_train_end .如果使用其他回调,例如tf.keras.callbacks.EarlyStopping ,我会继续使用on_train_end的方法。 Otherwise it is not guaranteed that the callback is called after the last epoch.否则,不能保证在最后一个 epoch 之后调用回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 额外的纪元继续训练后Keras正确保存检查点-初始纪元 - Keras correctly saving checkpoints after extra epochs continuing training - initial epoch 在训练时期多次调用 Keras 回调 - Call Keras callback during training epoch more than once 如何在 Keras 的每个训练周期后进行预测? - How to predict after each epoch of training in Keras? 在Keras中每N个时期改变训练数据集 - Change training dataset every N epochs in Keras Keras 训练的准确率只有一点点变化,几个 epoch 之后它总是一样的 - Keras training accuracy only changing a bit, and after a few epochs it is always the same 我怎样才能从上一个时代继续训练? - how can i continue training from last epoch? 如何在 N 个 epoch 后调整学习率? - How to adjust the learning rate after N number of epochs? 创建一个每 n 个周期激活一次的 Keras 回调 - Creating a Keras Callback that activates every n epochs 使用 lstm 训练的 model 需要多少个 epoch - how many epochs required for model with lstm training 在训练过程中的每个特定时期之后如何访问神经网络的权重(参数) - How to access the weights (parameters) of Neural Network after every certain epochs during training process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM