I’m having trouble getting my model to train. It keeps returning the error:
ValueError: Target size (torch.Size([8])) must be the same as input size (torch.Size([8, 8]))
for the given code:
from transformers import BertTokenizer, BertForSequenceClassification, TrainingArguments, Trainer
from datasets import load_dataset
import torch

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=8)
dataset = load_dataset('json', data_files={'train': 'train.jsonl', 'test': 'test.jsonl'})

def preprocess_data(examples):
    # encode a batch of sentences
    encoding = tokenizer(examples["sentence1"], padding="max_length", truncation=True)
    # add labels as a list
    encoding["labels"] = examples["label"]
    
    return encoding


# tokenize sentences + add labels
encoded_dataset = dataset.map(preprocess_data)
for k,v in encoded_dataset.items():
     print(k, v.shape)
# turn into PyTorch dataset
encoded_dataset.set_format("torch")


small_train_dataset = encoded_dataset["train"].shuffle(seed=42).select(range(100))
small_eval_dataset = encoded_dataset["test"].shuffle(seed=42).select(range(100))

training_args = TrainingArguments(
    output_dir=".",
    evaluation_strategy="epoch",
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    num_train_epochs=1,
)

trainer = Trainer(
    model=model, args=training_args, train_dataset=small_train_dataset, eval_dataset=small_eval_dataset)

trainer.train()


I understand this is a mismatch in the target dimension but I’m not exactly sure how to correct it? Is it a mistake in how I’m preparing the data?