eval_loss not found when training a peft model The metric_for_best_model training argument is set to ‘eval_loss’, which is not found in the evaluation metrics
记录lora微调qwen 2.5 VL加上验证集碰到的一个bug,加了验证集但是返回的 evaluation metrics没有eval_loss
KeyError: “The metric_for_best_model training argument is set to ‘eval_loss’, which is not found in the evaluation metrics. The available evaluation metrics are: [‘eval_runtime’, ‘eval_samples_per_second’, ‘eval_steps_per_second’, ‘epoch’, ‘memory_allocated (GB)’, ‘max_memory_allocated (GB)’, ‘total_memory_available (GB)’]. Consider changing the metric_for_best_model via the TrainingArguments.”
解决方案:
修改Transformer库 trainer函数
Class Trainer init 函数里面大概第782行左右
if _is_peft_model(self.model) and self.args.label_names is None:
logger.warning(
f"No label_names provided for model class `{self.model.__class__.__name__}`."
" Since `PeftModel` hides base models input arguments, if label_names is not given, label_names can't be set automatically within `Trainer`."
" Note that empty label_names list will be used instead."
)
default_label_names = find_labels(self.model.__class__)
self.label_names = default_label_names if self.args.label_names is None else self.args.label_names
self.can_return_loss = can_return_loss(self.model.__class__)
self.control = self.callback_handler.on_init_end(self.args, self.state, self.control)
改成
if _is_peft_model(self.model):
if hasattr(self.model, "get_base_model"):
model_to_inspect = self.model.get_base_model()
default_label_names = find_labels(model_to_inspect.__class__)
self.can_return_loss = can_return_loss(model_to_inspect.__class__)
else:
default_label_names = find_labels(self.model.__class__)
self.can_return_loss = can_return_loss(self.model.__class__)
self.label_names = default_label_names if self.args.label_names is None else self.args.label_names
self.control = self.callback_handler.on_init_end(self.args, self.state, self.control)
参考:
eval_loss not found when training a peft model using trainer.py https://github.com/huggingface/transformers/issues/33420