As a follow-up from my previous question, I am trying to fine-tune a model, but I am getting an error: IndexError: tuple index out of range
.
I am trying to classify individual sentences with a binary classification. I am using transformers
version 4.2.1 and datasets
version 1.2.1
The dataset(s) are .csv files with two columns: “sentence” and “label”. The following is the code that led to the error - if anyone can help identify my error, please let me know
import numpy as np
from transformers import TrainingArguments, Trainer
from transformers import BertTokenizer, BertForSequenceClassification
from datasets import load_dataset, load_metric
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
dataset = load_dataset('csv', data_files={'train': "train_data.csv",
'test': "test_data.csv"})
metric = load_metric('f1', 'accuracy')
encoded_dataset = dataset.map(lambda x: tokenizer(x['sentence'], padding=True, truncation=True), batched=True,load_from_cache_file=False)
batch_size = 16
args = TrainingArguments(
"test_20210201_1200",
evaluation_strategy = "epoch",
learning_rate=2e-5,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
num_train_epochs=5,
weight_decay=0.01,
seed=18,
label_names='label',
load_best_model_at_end=True,
metric_for_best_model='f1',
)
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return metric.compute(predictions=predictions, references=labels)
trainer = Trainer(
model,
args,
train_dataset=encoded_dataset['train'],
eval_dataset=encoded_dataset['test'],
tokenizer=tokenizer,
compute_metrics=compute_metrics
)
All of that runs with no problem. However, I get the following error next:
trainer.train()
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-2-3435b262f1ae> in <module>
----> 1 trainer.train()
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer.py in train(self, model_path, trial)
933
934 self.control = self.callback_handler.on_epoch_end(self.args, self.state, self.control)
--> 935 self._maybe_log_save_evaluate(tr_loss, model, trial, epoch)
936
937 if self.args.tpu_metrics_debug or self.args.debug:
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer.py in _maybe_log_save_evaluate(self, tr_loss, model, trial, epoch)
1002 metrics = None
1003 if self.control.should_evaluate:
-> 1004 metrics = self.evaluate()
1005 self._report_to_hp_search(trial, epoch, metrics)
1006
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer.py in evaluate(self, eval_dataset, ignore_keys, metric_key_prefix)
1440 start_time = time.time()
1441
-> 1442 output = self.prediction_loop(
1443 eval_dataloader,
1444 description="Evaluation",
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer.py in prediction_loop(self, dataloader, description, prediction_loss_only, ignore_keys, metric_key_prefix)
1569 losses_host = losses if losses_host is None else torch.cat((losses_host, losses), dim=0)
1570 if logits is not None:
-> 1571 preds_host = logits if preds_host is None else nested_concat(preds_host, logits, padding_index=-100)
1572 if labels is not None:
1573 labels_host = labels if labels_host is None else nested_concat(labels_host, labels, padding_index=-100)
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer_pt_utils.py in nested_concat(tensors, new_tensors, padding_index)
83 ), f"Expected `tensors` and `new_tensors` to have the same type but found {type(tensors)} and {type(new_tensors)}."
84 if isinstance(tensors, (list, tuple)):
---> 85 return type(tensors)(nested_concat(t, n, padding_index=padding_index) for t, n in zip(tensors, new_tensors))
86 elif isinstance(tensors, torch.Tensor):
87 return torch_pad_and_concatenate(tensors, new_tensors, padding_index=padding_index)
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer_pt_utils.py in <genexpr>(.0)
83 ), f"Expected `tensors` and `new_tensors` to have the same type but found {type(tensors)} and {type(new_tensors)}."
84 if isinstance(tensors, (list, tuple)):
---> 85 return type(tensors)(nested_concat(t, n, padding_index=padding_index) for t, n in zip(tensors, new_tensors))
86 elif isinstance(tensors, torch.Tensor):
87 return torch_pad_and_concatenate(tensors, new_tensors, padding_index=padding_index)
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer_pt_utils.py in nested_concat(tensors, new_tensors, padding_index)
85 return type(tensors)(nested_concat(t, n, padding_index=padding_index) for t, n in zip(tensors, new_tensors))
86 elif isinstance(tensors, torch.Tensor):
---> 87 return torch_pad_and_concatenate(tensors, new_tensors, padding_index=padding_index)
88 elif isinstance(tensors, np.ndarray):
89 return numpy_pad_and_concatenate(tensors, new_tensors, padding_index=padding_index)
/usr/local/bin/miniconda3/envs/tfhub/lib/python3.8/site-packages/transformers/trainer_pt_utils.py in torch_pad_and_concatenate(tensor1, tensor2, padding_index)
46 def torch_pad_and_concatenate(tensor1, tensor2, padding_index=-100):
47 """Concatenates `tensor1` and `tensor2` on first axis, applying padding on the second if necessary."""
---> 48 if len(tensor1.shape) == 1 or tensor1.shape[1] == tensor2.shape[1]:
49 return torch.cat((tensor1, tensor2), dim=0)
50
IndexError: tuple index out of range
Thanks in advance!