# 【Solved】How can I get loss by using trainer when training gpt2?

**URL:** <https://discuss.huggingface.co/t/solved-how-can-i-get-loss-by-using-trainer-when-training-gpt2/20631>\
**Category:** Beginners\
**Created:** [July 20, 2022, 2:51pm UTC](https://discuss.huggingface.co/t/solved-how-can-i-get-loss-by-using-trainer-when-training-gpt2/20631 "2022-07-20T14:51:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azily](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Azily](https://discuss.huggingface.co/u/Azily)\
**Post date:** [July 20, 2022, 2:51pm UTC](https://discuss.huggingface.co/t/solved-how-can-i-get-loss-by-using-trainer-when-training-gpt2/20631/1 "2022-07-20T14:51:16Z")

</div>

I want to use trainer to train my gpt-2 model, while when I pass the dataset to the trainer, it tell me that there is a keyError: ‘loss’. So I try to find the cause and find that the function ‘compute\_loss’ is not work as what I want. So I write a new compute\_loss function but find that the inputs don’t have the column ‘labels’, which I create when using tokenizer, I think is may be deleted when used by trainer.

I wonder how should I use trainer correctly so that I can make compute\_loss work well

this is my compute\_loss function, but not work because it does not find a column named labels

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/a/a741c1bda084013f1ebf708f08cb3f118123ec86.png)

---

<div class="post-metadata">

**Author:** ![Azily](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Azily](https://discuss.huggingface.co/u/Azily)\
**Post date:** [July 20, 2022, 2:53pm UTC](https://discuss.huggingface.co/t/solved-how-can-i-get-loss-by-using-trainer-when-training-gpt2/20631/2 "2022-07-20T14:53:22Z")

</div>

there are more pictures about the problem  
this is my dataset, it is only used to test how to use trainer so I don’t distinguish them

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/0/079d06c8419dcd33a7f1b9d920842ef13d1b4300.png)

---

<div class="post-metadata">

**Author:** ![Azily](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Azily](https://discuss.huggingface.co/u/Azily)\
**Post date:** [July 20, 2022, 2:53pm UTC](https://discuss.huggingface.co/t/solved-how-can-i-get-loss-by-using-trainer-when-training-gpt2/20631/3 "2022-07-20T14:53:54Z")

</div>

this is my trainer

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/7/7abf1f1a9ab559e4b4ab406756f3572256e0cc61.png)

---

<div class="post-metadata">

**Author:** ![Azily](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@Azily](https://discuss.huggingface.co/u/Azily)\
**Post date:** [July 21, 2022, 8:52am UTC](https://discuss.huggingface.co/t/solved-how-can-i-get-loss-by-using-trainer-when-training-gpt2/20631/4 "2022-07-21T08:52:24Z")

</div>

By using GPT2LMHeadModel instead of GPT2Model, the problem doesn’t appear again. So I wonder what is the difference between them.
