![](/img/trans.png)
[英]Keras correctly saving checkpoints after extra epochs continuing training - initial epoch
[英]How to call callback after n epochs but always in the last epoch of training?
我想在 n 个时期之后调用回调,但总是在训练的最后一个时期。 这里解释了如何在 n 个 epoch 之后调用回调。
目前我正在使用以下方法:
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()
有没有更简单的方法可以在on_epoch_end
内的 if 语句中添加一个额外的条件并且不需要on_training_end
?
正如@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()
如果使用其他回调,例如tf.keras.callbacks.EarlyStopping
,我会继续使用on_train_end
的方法。 否则,不能保证在最后一个 epoch 之后调用回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.