Callbacks for logging results to GPT2

I have a GPT2 model which I’m training (and works) that takes in an example input sequence and outputs a target sequence. I am able to log the model results of the validation set after the model has finished training but was hoping I could log after the end of an epoch, and was hoping to use callbacks.

What I’m hoping to log goes something like this:

class MyCallback(transformers.TrainerCallback):
    def on_epoch_end(args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        valid_dl = trainer.get_eval_dataloader()
        
        for x, _ in valid_dl:
            x_tokenized = tokenizer(x)
            output = tokenizer.decode(model.generate(x_tokenized))
            break
       logging_fn(x, output)

I do not know how to access model, tokenizer and trainer in the above scenario. Any thoughts on how I could achieve above effect?

That is not supported as it would let the user modify the state of the Trainer which would then mess up with the rest of the training loop.