# Why am I getting KeyError: 'loss'?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%3E%5C
**Category:** Beginners\
**Created:** [June 18, 2021, 4:55am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948 "2021-06-18T04:55:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fortuna](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@fortuna](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ffortuna)%5C
**Post date:** [June 18, 2021, 4:55am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F1 "2021-06-18T04:55:20Z")

</div>

Why when I run trainer.train() it gives me Keyerror:‘loss’ previously i use something like start\_text and stop\_text and I read in previous solution that this the cause of error so I delete it, but it still give same error.Did you have any solution? Thanks

```auto
from transformers import AutoTokenizer, AutoModelWithLMHead
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
model = AutoModelWithLMHead.from_pretrained("distilgpt2")

from datasets import Dataset
dataset = Dataset.from_text('/content/drive/MyDrive/Colab_Notebooks/qna.txt')

tokenizer.pad_token = tokenizer.eos_token
def tokenize_function(examples):
    return tokenizer(examples["text"], padding="max_length", truncation=True)

tokenized_datasets = dataset.map(tokenize_function, batched=True)

from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir='/content/drive/MyDrive/Colab_Notebooks/GPT_checkpoint', # output directory
    num_train_epochs=3, # total number of training epochs
    per_device_train_batch_size=1, # batch size per device during training
    warmup_steps=500, # number of warmup steps for learning rate scheduler
    weight_decay=0.01, # strength of weight decay
    logging_dir='/content/drive/MyDrive/Colab_Notebooks/GPT_checkpoint/logs', # directory for storing logs
    logging_steps=10
)
trainer = Trainer(
    model=model, # the instantiated 🤗 Transformers model to be trained
    args=training_args, # training arguments, defined above
    train_dataset=tokenized_datasets
)

```

Here is the dataset sample:

```auto
Was Volta an Italian physicist?
yes
Was Volta an Italian physicist?
yes
Is Volta buried in the city of Pittsburgh?
no
Is Volta buried in the city of Pittsburgh?
no

```

Here is the full error message:

```auto
KeyError Traceback (most recent call last)
<ipython-input-17-3435b262f1ae> in <module>()
----> 1 trainer.train()

3 frames
/usr/local/lib/python3.7/dist-packages/transformers/file_utils.py in __getitem__ (self, k)
   1804 if isinstance(k, str):
   1805 inner_dict = {k: v for (k, v) in self.items()}
-> 1806 return inner_dict[k]
   1807 else:
   1808 return self.to_tuple()[k]

KeyError: 'loss'

```

---

<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:** [June 18, 2021, 1:25pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F2 "2021-06-18T13:25:54Z")

</div>

There are no labels in your dataset, so it can’t train (and the model does not produce a loss, hence your error). Maybe you wanted to use the `DataCollatorForMaskedLM` to generate those labels automatically?

---

<div class="post-metadata">

**Author:** ![pipi](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@pipi](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpipi)%5C
**Post date:** [December 3, 2021, 10:26am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F3 "2021-12-03T10:26:47Z")

</div>

my dataset has the `labels`, but also get a KeyError: ‘loss’

```auto
>>> d = next(iter(train_loader))
>>> d.keys()
dict_keys(['input_ids', 'attention_mask', 'labels'])
>>>
now exiting InteractiveConsole...
[INFO|trainer.py:1202] 2021-12-03 18:24:14,156 >> *****Running training*****
[INFO|trainer.py:1203] 2021-12-03 18:24:14,156 >> Num examples = 6667
[INFO|trainer.py:1204] 2021-12-03 18:24:14,156 >> Num Epochs = 3
[INFO|trainer.py:1205] 2021-12-03 18:24:14,156 >> Instantaneous batch size per device = 1
[INFO|trainer.py:1206] 2021-12-03 18:24:14,157 >> Total train batch size (w. parallel, distributed & accumulation) = 1
[INFO|trainer.py:1207] 2021-12-03 18:24:14,157 >> Gradient Accumulation steps = 1
[INFO|trainer.py:1208] 2021-12-03 18:24:14,157 >> Total optimization steps = 20001
  0%| | 0/20001 [00:00<?, ?it/s]Traceback (most recent call last):
  File "run_train.py", line 90, in <module>
    main()
  File "run_train.py", line 85, in main
    trainer.train()
  File "/usr/local/anaconda3/lib/python3.7/site-packages/transformers/trainer.py", line 1323, in train
    tr_loss_step = self.training_step(model, inputs)
  File "/usr/local/anaconda3/lib/python3.7/site-packages/transformers/trainer.py", line 1861, in training_step
    loss = self.compute_loss(model, inputs)
  File "/usr/local/anaconda3/lib/python3.7/site-packages/transformers/trainer.py", line 1905, in compute_loss
    loss = outputs["loss"] if isinstance(outputs, dict) else outputs[0]
  File "/usr/local/anaconda3/lib/python3.7/site-packages/transformers/file_utils.py", line 2125, in __getitem__
    return inner_dict[k]
KeyError: 'loss'

```

@sgugger is there any advice?

---

<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:** [December 3, 2021, 5:09pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F4 "2021-12-03T17:09:11Z")

</div>

You should debug the training step by step as highlighted in [this course chapter](https://huggingface.co/course/chapter8/4?fw=pt).

---

<div class="post-metadata">

**Author:** ![yasuoman](https://avatars.discourse-cdn.com/v4/letter/y/f07891/32.png) [@yasuoman](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fyasuoman)%5C
**Post date:** [January 10, 2022, 2:00am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F5 "2022-01-10T02:00:29Z")

</div>

Hi, I met the same situation.I found that Trainer.label\_smoother is None,so the Trainer class didn’t calculate the loss,I don’t know how to deal with this.

---

<div class="post-metadata">

**Author:** ![Mo777](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Mo777](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMo777)%5C
**Post date:** [February 16, 2022, 12:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F6 "2022-02-16T12:39:21Z")

</div>

Hey @sgugger and thank you for the great transformers. I have the same error while I want to fine-tune the facebook/bart-large-cnn for a summarization task. my dataset (after tokenization) looks like this :  
DatasetDict({  
train: Dataset({  
features: [‘attention\_mask’, ‘input\_ids’, ‘summary’, ‘text’],  
num\_rows: 10980  
})  
test: Dataset({  
features: [‘attention\_mask’, ‘input\_ids’, ‘summary’, ‘text’],  
num\_rows: 1161  
})  
})

and I am using thsi line to get the model:  
model = AutoModelForSeq2SeqLM.from\_pretrained(“facebook/bart-base”)  
and training arguments like :  
training\_args = TrainingArguments(“test\_trainer”)

I am also getting this message while trainer.train()  
The following columns in the training set don’t have a corresponding argument in `BartForConditionalGeneration.forward` and have been ignored: text, summary.

can you please guide me?

---

<div class="post-metadata">

**Author:** ![Mo777](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Mo777](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMo777)%5C
**Post date:** [February 16, 2022, 12:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F7 "2022-02-16T12:41:51Z")

</div>

I would suggest to use 0,1 instead of yes/no! maybe it helps

---

<div class="post-metadata">

**Author:** ![Mo777](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Mo777](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMo777)%5C
**Post date:** [February 16, 2022, 1:55pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F8 "2022-02-16T13:55:56Z")

</div>

this video helped me to find a solution for my problem

[![](https://us1.discourse-cdn.com/hellohellohello/original/2X/3/3d23969c64c651aabc365a2917e01ccc0d9b91a7.jpeg "Data processing for Summarization") ](https://www.youtube.com/watch?v=1m7BerpSq8A)

Thank you Huggingface

---

<div class="post-metadata">

**Author:** ![marja-w](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@marja-w](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarja-w)%5C
**Post date:** [February 27, 2023, 8:36pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F9 "2023-02-27T20:36:55Z")

</div>

Hello, I have the same problem and debugged into the code to find out the same thing. Have you resolved the issue? I am trying to fine-tune the BertForPreTraining Model.

---

<div class="post-metadata">

**Author:** ![mohit1707](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mohit1707](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmohit1707)%5C
**Post date:** [March 17, 2023, 11:30am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-am-i-getting-keyerror-loss%2F6948%2F10 "2023-03-17T11:30:01Z")

</div>

@pipi, I was facing the exact same issue and fixed it by just changing the name of the column which had labels for my dataset to “label” i.e. in your case you can change “labels” to “label” and trainer hopefully should run fine then.

This was really weird for me that trainer expects the column name to be as “label” only but anyway the fix worked for me and hopefully it works for you as well.
