KeyError: 'labels'
Closed this issue · 1 comments
You did an amazing job with your research and implementing the code.
Hi..I have been trying to test and use the repository, but I am encountering an error while training the model.
======== Epoch 1 / 3 ========
KeyError Traceback (most recent call last)
Cell In[22], line 3
1 for epoch_i in range(config["num_train_epochs"]):
2 print(f"======== Epoch {epoch_i + 1} / {config['num_train_epochs']} ========")
----> 3 train_info = gan_trainer.train_epoch(log_env=None)
4 result = gan_trainer.validation(log_env=None)
File ~/Desktop/pii Detection/GAN/gan-plus-nlp/base/base_trainer.py:15, in BaseTrainer.train_epoch(self, log_env)
13 self.train_mode_on()
14 for step, batch in enumerate(self.train_dataloader):
---> 15 epoch_info = self.training_step(batch, log_env)
16 self.on_epoch_end(train_info=train_info, epoch_info=epoch_info)
18 self.on_train_end(train_info)
File ~/Desktop/pii Detection/GAN/gan-plus-nlp/trainer/gan_trainer.py:155, in GANTrainerSequenceClassification.training_step(self, batch, log_env)
148 batch_size = batch["input_ids"].shape[0]
149 seq_len = batch["input_ids"].shape[1]
151 output = self.model(
152 input_ids=batch["input_ids"],
153 input_mask=batch["attention_mask"],
154 token_type_ids=batch.get("token_type_ids", None),
--> 155 labels=batch["labels"],
156 labeled_mask=batch["labeled_mask"],
157 )
158 noise = get_noise(
159 (batch_size, self.config["noise_size"]),
160 dist=self.config.get("noise_type", "uniform"),
161 range=self.config.get("noise_range", (-2, 2)),
162 device=self.device,
163 )
164 gen_states = self.generator(noise)
File ~/anaconda3/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:237, in BatchEncoding.getitem(self, item)
230 """
231 If the key is a string, returns the value of the dict associated to key
('input_ids', 'attention_mask',
232 etc.).
233
234 If the key is an integer, get the tokenizers.Encoding
for batch item with index key
.
235 """
236 if isinstance(item, str):
--> 237 return self.data[item]
238 elif self._encodings is not None:
239 return self._encodings[item]
KeyError: 'labels'
The model has been tweaked, but I have not been able to succeed.
Can please give some guidance or workable code? thanks a lot!
Look forward to hearing from you soon!
Hi @shankara-93 !
Could you please provide more details on what model did you use?
As I can see from your error logs the dataset has no column called 'labels' (all trainers try to access 'labels' column by default). Please see example usage here.