Hi,
I have a multilabel task (num_labels=8) and I want to use BertForSequenceClassification using Trainer to train the model.
But I get the following error:
ValueError: Expected input batch_size (8) to match target batch_size (64).
I assume that the problem is the data format of the labels. Currently, my label is a 8-dim list (e.g., [1,0,0,0,0,1,0,0]).
What is the right format for the label data?
Here my code:
class EmotionDataset(torch.utils.data.Dataset):
def init(self, encodings, labels):
self.encodings = encodings
self.labels = labelsdef __getitem__(self, idx): item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()} item['labels'] = torch.tensor(self.labels[idx]) return item def __len__(self): return len(self.labels)
MODEL_NAME = ‘dbmdz/bert-base-german-uncased’
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = BertForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=8)tokenize data
dataset_train = Dataset.from_pandas(df_train)
train_encodings = tokenizer(dataset_train['text], truncation=True, padding=True)
train_dataset = EmotionDataset(train_encodings, dataset_train['label])training_args = TrainingArguments(
output_dir=‘./results’, # output directory
num_train_epochs=1, # total # of training epochs
per_device_train_batch_size=8, # batch size per device during training
per_device_eval_batch_size=32, # batch size for evaluation
warmup_steps=500, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
logging_dir=‘./logs’, # directory for storing logs
)trainer = Trainer(
model=model, # the instantiated Transformers model to be trained
args=training_args, # training arguments, defined above
train_dataset=train_dataset, # training dataset
eval_dataset=test_dataset # evaluation dataset
)_ = trainer.train()
trainer.evaluate()
Thanks,
Max