Problem "Target size must be the same as input size "
Hello everyone, I’m encountering an issue with my code for the NLI classification task. I have three labels, and I’m using BERT. When attempting to run my program, I encounter the following error message: “raise ValueError (f"Target size ({target.size()}) must be the same as input size.” Thank you in advance for any assistance. ({input.size()})“)ValueError: Target size (torch.Size([1])) must be the same as input size (torch.Size([1, 3]))”-----------------------------------code--------------------------------------------------------------------------------Function----------------------def compute_metrics(eval_pred):logits = eval_pred.predictions[0]labels = eval_pred.label_idspredictions = np.argmax(logits, axis=-1)return {“f1”: f1_score(labels, predictions)}def tokenize_function(example):give_premise = 'Premise: ’give_hypothesis = "\nHypothesis: "give_classification = '\n\nClassification: ’prompt = [give_premise + premise + give_hypothesis + hypothesis + give_classification for premise, hypothesis in zip(example[“premise”], example[“hypothesis”])]example[‘input_ids’] = tokenizer(prompt, padding=“max_length”, truncation=True, return_tensors=“pt”).input_idsreturn example#--------------Loadingdataset----------------#utilFunction.balanced_dataset(“…/train.csv”, 200, “balanced_train.csv”)#utilFunction.balanced_dataset(“…/validation_matched.csv”,20, “balanced_validation_matched.csv”)#utilFunction.balanced_dataset(“…/validation_mismatched.csv”,20, “balanced_validation_mismatched.csv”)data_files = {“train”: “balanced_train.csv”, “test”: “balanced_validation_matched.csv”, “validation”: “balanced_validation_mismatched.csv”}#dataset= load_dataset(“csv”, data_files=data_files)df = pd.read_csv(‘balanced_train.csv’)df = df.rename(columns={‘NliClass’: ‘labels’})df[‘labels’] = df[‘labels’].astype(int)dataset = datasets.Dataset.from_pandas(df)print(dataset)label2id = {0: ‘ENTAILMENT’, 1: ‘NEUTRAL’, 2: ‘CONTRADICTION’}id2label = {‘ENTAILMENT’: 0,  ‘NEUTRAL’: 1, ‘CONTRADICTION’: 2}#--------------LoadingModel And Tokenizer----------------model_name=“bert-base-uncased”original_model = BertForSequenceClassification.from_pretrained(model_name, problem_type=“multi_label_classification”, num_labels=3,id2label=id2label, label2id=label2id, ignore_mismatched_sizes=True)print(original_model)print(original_model.config.id2label)tokenizer = AutoTokenizer.from_pretrained(model_name)tokenized_datasets = dataset.map(tokenize_function, batched=True)ls = [‘input_ids’, ‘labels’]for var in df:if var not in ls:tokenized_datasets = tokenized_datasets.remove_columns(var)#--------------Printtrainable parameter and generate original output----------------print(utilFunction.print_number_of_trainable_model_parameters(original_model))#---------------------DEFINE-PEFT-LORA---------------------------lora_config = LoraConfig(task_type=TaskType.SEQ_CLS, r=1, lora_alpha=1, lora_dropout=0.1)model = get_peft_model(original_model, lora_config)metric = evaluate.load(“accuracy”)peft_training_args = TrainingArguments(output_dir=“test_trainer”, evaluation_strategy=“epoch”, num_train_epochs=25, per_device_train_batch_size=1)peft_trainer = Trainer(model=model,args=peft_training_args,train_dataset=tokenized_datasets,#eval_dataset=tokenized_datasets[“validation”],compute_metrics=compute_metrics,)#---------------------TRAIN ---------------------------peft_trainer.train()