Get current epoch inside process function of evaluator

Hi. I have an evaluation decorated function like:

  @trainer.on(Events.EPOCH_COMPLETED(every=cfg.train.validation_interval))
  def run_validation():
      epoch = trainer.state.epoch
      state = evaluator.run(val_dataloader)
      log_metrics(
          logger, state.times["COMPLETED"], "Validation", state.metrics, epoch
      )

Inside the evaluator I do some text generations and would like to save them to new files named after the current epoch. However, I do not know how to get the current epoch inside this process function (engine.state.epoch always returns 1).
How can I get the current epoch inside the evaluation engine? Thanks in advance for any help you can provide.

Hi @AfonsoSalgadoSousa

You can add trainer.state.epoch to evaluator.state such that you’ll get easily trainer’s epoch:

  @trainer.on(Events.EPOCH_COMPLETED(every=cfg.train.validation_interval))
  def run_validation():
      epoch = trainer.state.epoch
      evaluator.state.trainer_epoch = epoch
      state = evaluator.run(val_dataloader)
      log_metrics(
          logger, state.times["COMPLETED"], "Validation", state.metrics, epoch
      )

so in the handler where you woudl like to save files you can fetch trainer’s epoch as

def eval_fn(...):
    ...
    epoch = evaluator.state.trainer_epoch
    ...

Hope this helps