How to write custom callback with monitor #13045
Unanswered
ZENGYIMING-EAMON
asked this question in
Lightning Trainer API: Trainer, LightningModule, LightningDataModule
Replies: 1 comment
-
you can access this class CustomCallback(pl.Callback):
def __init__(self, mode='min', monitor='some_lss/dataloader_idx_1'):
...
torch_inf = torch.tensor(np.Inf)
mode_dict = {"min": torch_inf, "max": -torch_inf}
self.monitor_op = {"min": torch.lt, "max": torch.gt}[mode]
self.best_metric_value = mode_dict[mode]
def on_validation_end(self, trainer, pl_module):
current_metric = trainer.callback_metrics[self.monitor]
if self.monitor_op(current_metric, self.best_metric_value):
self.best_metric_value = current_metric
# do whatever you want |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using PL-1.6.1.
I am using the official
pl.callbacks.ModelCheckpoint
withmonitor: 'some_lss/dataloader_idx_1', mode: 'min'
and it works fine.Now I write a custom callback
class CustomCallback(pl.Callback):
to save some images at the end of train epoch,by writing
def on_train_epoch_end(self,trainer,pl_module):
underclass CustomCallback(pl.Callback):
.Now I save it every 10 epochs using
if trainer.current_epoch%50==0:
underdef on_train_epoch_end(self,trainer,pl_module):
.But I want to change it to save every time the model meets a lower loss, i.e., every time the model meets a min
'some_lss/dataloader_idx_1'
. How can I incorporate themonitor: 'some_lss/dataloader_idx_1', mode: 'min'
into my custom callback?Beta Was this translation helpful? Give feedback.
All reactions