# Callbacks for logging results to GPT2

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcallbacks-for-logging-results-to-gpt2%2F14781%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 16, 2022, 4:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcallbacks-for-logging-results-to-gpt2%2F14781 "2022-02-16T04:41:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachin](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sachin/32/26534_2.png) [@sachin](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsachin)%5C
**Post date:** [February 16, 2022, 4:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcallbacks-for-logging-results-to-gpt2%2F14781%2F1 "2022-02-16T04:41:30Z")

</div>

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:

```python
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?

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [February 16, 2022, 3:33pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcallbacks-for-logging-results-to-gpt2%2F14781%2F2 "2022-02-16T15:33:50Z")

</div>

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.
