How to access loss at each epoch from Pytorch Lighting?
I'm using Pytorch Lighting and Tensorboard as PyTorch Forecasting library is build using them. I want to create my own loss curves via matplotlib and don't want to use Tensorboard.
It is possible to access metrics at each epoch via a method? Validation Loss, Training Loss etc?
My code is below:
logger = TensorBoardLogger(&quot;logs&quot;, name = &quot;model&quot;)
trainer = pl.Trainer(#Some params)

Does logger or trainer have any method to access this information?
PL documentation isn't clear and there are many methods associated with logger and trainer.

My recommendation is that you:

Create a csv logger:

from pytorch_lightning.loggers import CSVLogger

csv_logger = CSVLogger(
    save_dir=str'./',
    name='csv_file'
)


Pass it to your trainer

# Initialize a trainer
trainer = Trainer(
    accelerator=&quot;auto&quot;,
    max_epochs=1,
    log_every_n_steps=10,
    logger=[csv_logger],
)


Have your model log your epoch results. This will trigger a write action into a txt file by the CSVlogger

class MNISTModel(LightningModule):
    def __init__(self):
        super().__init__()
        self.l1 = torch.nn.Linear(28 * 28, 10)

    def forward(self, x):
        return torch.relu(self.l1(x.view(x.size(0), -1)))

    def training_step(self, batch, batch_nb):
        x, y = batch
        loss = F.cross_entropy(self(x), y)
        self.log('loss_epoch', loss, on_step=False, on_epoch=True)
        return loss

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=0.02)


Use the logged values into the CSV file for plotting you results. In this way, if you are unhappy with your plot you would be able to just re-run everything with your plot script modifications without having to wait for the training to end again.

