ValueError:模型没有从输入中返回损失,只有以下键:logits

问题描述 投票:0回答:0

我想制作一个隐喻检测器模型。 作为一个预训练模型,我使用了一个 DistilBert 模型,我之前用掩码对它进行了微调(这是我用来制作新隐喻检测模型的模型)。 新模型是给我一个错误的模型(代码中的那个)。我尝试做序列分类。但是,在训练它时会出现错误。

数据集的一个例子是: 001 T1 M 黑暗是光明的哥哥。 150 T2 N 骑士骑着野马。 它有 150 行,M/N 是标签,第四列是句子。

dataset = load_dataset('text', data_files='/content/drive/MyDrive/project/metaphors_dataset.txt')

# 90% train, 10% test + validation
train_testvalid = dataset["train"].train_test_split(test_size=0.1)
# Split the 10% test + valid in half test, half valid
test_valid = train_testvalid['test'].train_test_split(test_size=0.5)
# gather everyone if you want to have a single DatasetDict
train_test_valid_dataset = DatasetDict({
    'train': train_testvalid['train'],
    'test': test_valid['test'],
    'valid': test_valid['train']})

print(train_test_valid_dataset)

tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

def preprocess_function(examples):
   return tokenizer(examples["text"], truncation=True)
 
tokenized_train = train_test_valid_dataset["train"].map(preprocess_function, batched=True)
tokenized_test = train_test_valid_dataset["test"].map(preprocess_function, batched=True)

path_model = "/content/drive/MyDrive/project/my_model.pt"
model = AutoModelForSequenceClassification.from_pretrained(path_model, num_labels=2, from_tf=True) 

def compute_metrics(pred):
  labels = pred.label_ids
  preds = pred.predictions.argmax(-1)
  accuracy = accuracy_score(labels, preds)
  f1 = f1_score(labels, preds, average='weighted')
  return {"accuracy": accuracy, "f1": f1}

training_args = TrainingArguments(
    output_dir=repo_name,
    evaluation_strategy='epoch',
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    num_train_epochs=2,
    weight_decay=0.01,
    save_total_limit=1,
    save_strategy='epoch',
    load_best_model_at_end=True,
    metric_for_best_model='f1',
    greater_is_better=True,
    adam_epsilon=1e-8,
    adam_beta2=0.01
)
trainer = Trainer(
   model=model,
   args=training_args,
   train_dataset=tokenized_train,
   eval_dataset=tokenized_test,
   tokenizer=tokenizer,
   data_collator=data_collator,
   compute_metrics=compute_metrics,
)

错误在这一行:

trainer.train()

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-97-3435b262f1ae> in <module>
----> 1 trainer.train()

3 frames
/usr/local/lib/python3.9/dist-packages/transformers/trainer.py in train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
   1631             self._inner_training_loop, self._train_batch_size, args.auto_find_batch_size
   1632         )
-> 1633         return inner_training_loop(
   1634             args=args,
   1635             resume_from_checkpoint=resume_from_checkpoint,

/usr/local/lib/python3.9/dist-packages/transformers/trainer.py in _inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
   1900                         tr_loss_step = self.training_step(model, inputs)
   1901                 else:
-> 1902                     tr_loss_step = self.training_step(model, inputs)
   1903 
   1904                 if (

/usr/local/lib/python3.9/dist-packages/transformers/trainer.py in training_step(self, model, inputs)
   2643 
   2644         with self.compute_loss_context_manager():
-> 2645             loss = self.compute_loss(model, inputs)
   2646 
   2647         if self.args.n_gpu > 1:

/usr/local/lib/python3.9/dist-packages/transformers/trainer.py in compute_loss(self, model, inputs, return_outputs)
   2688         else:
   2689             if isinstance(outputs, dict) and "loss" not in outputs:
-> 2690                 raise ValueError(
   2691                     "The model did not return a loss from the inputs, only the following keys: "
   2692                     f"{','.join(outputs.keys())}. For reference, the inputs it received are {','.join(inputs.keys())}."

ValueError: The model did not return a loss from the inputs, only the following keys: logits. For reference, the inputs it received are input_ids,attention_mask.

谢谢!

我想让它开始训练,然后评估它。

python bert-language-model text-classification distilbert
© www.soinside.com 2019 - 2024. All rights reserved.