Pytorch-lightning: How to find the current epoch number inside the training loop?

Created on 9 Apr 2020  ยท  4Comments  ยท  Source: PyTorchLightning/pytorch-lightning

โ“ 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]
question

Most helpful comment

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.

All 4 comments

Hi! thanks for your contribution!, great first issue!

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.

Thanks @philip30 :)

@philip30 tqdm starts from epoch 1 but actually it is epoch 0?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

polars05 picture polars05  ยท  3Comments

versatran01 picture versatran01  ยท  3Comments

iakremnev picture iakremnev  ยท  3Comments

Vichoko picture Vichoko  ยท  3Comments

anthonytec2 picture anthonytec2  ยท  3Comments