[英]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.