Log subset of metrics to console as text #6182
Replies: 2 comments 2 replies
-
You can still use Python's built-in logging for every kind of text logging within your module. We don't log anything to console except for the progressbar (well we do, but not the metric related stuff, more like warnings). |
Beta Was this translation helpful? Give feedback.
-
Revisiting this and summarizing what I would now consider best practice. While the custom callback mentioned above can work, I don't think it's a great solution since the formatting and metric names users will choose to log depends heavily on the type of model used. In my opinion, a better option is to define the hooks on the Lightning Module. This has a few benefits
def training_step(self, batch, batch_idx):
# Unpack batch and run forward prop
...
# Calculate metrics
outputs = {
'loss': F.cross_entropy(logits, gt_labels),
'char_acc': accuracy(...),
'seq_acc': sequence_accuracy(...)}
# Log values per-step (with train prefix) and per-epoch (with avg prefix)
for k, v in outputs.items():
self.log(f'train_{k}', v)
self.log(f'avg_train_{k}', v, on_step=False, on_epoch=True)
return outputs
def on_train_batch_end(self, outputs, batch, batch_idx, dataloader_idx):
metrics = self.trainer.callback_metrics
logger.info(f'Now we can access {metrics} as well as {self.trainer.num_training_batches}')
def on_validation_epoch_end(self):
metrics = self.trainer.callback_metrics
logger.info(f'The averages are pre-computed for us, like {metrics["avg_train_loss"]}') |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am in the processing of converting a vanilla PyTorch training system to Lightning and am having some difficulty understanding how to replicate my existing logging setup (as closely as possible). I like the benefit of built-in Tensorboard support, but I still need a good text log to allow inspection on machines without a GUI or easy browser connectivity.
Here's a sample of the first epoch from my current log file to illustrate what I'm looking to replicate. Note: char, seq, and region are short for character, sequence, and region accuracy
@awaelchli suggests Lightning's
CSVLogger
in #4876, but it falls short of a few desirable features|
separators stay in vertical columns despite the number of digits in the batch number varying, and large numbers are dynamically formatted with commasself.log()
inside mypl.LightningModule
since if the trainer has multiple loggers configured, they must all emit/save the same metrics at the same frequencyI followed the docs for creating a custom logger that uses Python's built-in logging framework. It works alright, but has all the same limitations as the
CSVLogger
. If it helps, I can share the complete code for my logging implementation.Has anyone found a solution that can address the above shortcomings? Should this be a feature request?
Beta Was this translation helpful? Give feedback.
All reactions