numersoz
numersoz

Reputation: 115

How to plot loss curves with Matplotlib?

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("logs", name = "model")
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.

Upvotes: 2

Views: 1420

Answers (1)

Mike B
Mike B

Reputation: 3476

My recommendation is that you:

  1. Create a csv logger:
from pytorch_lightning.loggers import CSVLogger

csv_logger = CSVLogger(
    save_dir='./',
    name='csv_file'
)
  1. Pass it to your trainer
# Initialize a trainer
trainer = Trainer(
    accelerator="auto",
    max_epochs=1,
    log_every_n_steps=10,
    logger=[csv_logger],
)
  1. 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)
  1. 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.

Upvotes: 5

Related Questions