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_ids
predictions = 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_ids
return example
#--------------Loading dataset----------------
#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}
#--------------Loading Model 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)
#--------------Print trainable 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()