question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to find the current epoch number inside the training loop?

See original GitHub issue

❓ Questions and Help

How to find the current epoch number inside the training loop?

I am training a Conv autoencoder on MNIST images and want to save the reconstructed images after every 10 epochs.

Code

class AutoEncoder(pl.LightningModule):
    def __init__(self):
        pass
    def forward(self, x):
        pass
    def training_step(self, batch, batch_idx):
         images = batch
         out  = self.forward(images)
         loss = self.loss(out, images)
         if epoch % 10:
            save(images)

I am looking for something like this where the output images are saved every 10 epoch.

What have you tried?

I haven’t tried anything since I am unable to find any documentation on current epoch no.

What’s your environment?

I am using pytorch 1.4 and Lightning version 0.7

  • OS: [e.g. iOS, Linux, Win]
  • Packaging [e.g. pip, conda]
  • Version [e.g. 0.5.2.1]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

69reactions
philip30commented, Apr 9, 2020

self.current_epoch would do. (self is a lightning module). Though it starts with 0. You can find the definition of the module here.

https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/core/lightning.py

Though I’d recommend you to use IDE e.g. pycharm so you can go to the definition of the class quickly.

10reactions
pme0commented, Mar 1, 2022

In case the iteration/step is required, there is also self.global_step

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to find the current epoch number inside the training loop?
I am training a Conv autoencoder on MNIST images and want to save the reconstructed images after every 10 epochs.
Read more >
pytorch_lightning.loops.epoch.training_epoch_loop
[docs]class TrainingEpochLoop(loops.Loop[_OUTPUTS_TYPE]): """Runs over all batches in a dataloader (one epoch). Args: min_steps: The minimum number of steps ...
Read more >
Get epoch inside keras optimizer - tensorflow - Stack Overflow
In Keras the epochs are stored and iterated within Model.fit() so you would need to write a custom training loop and write your...
Read more >
How to switch dataloaders between epochs in Lightning
You can use self.trainer.current_epoch inside pl.LightningDataModule to get the current epoch. 1 Like.
Read more >
Choose optimal number of epochs to train a neural network in ...
Finding the optimal number of epochs to avoid overfitting on MNIST dataset ... set and training set by partitioning the current training set ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found