You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SetFit训练无损失输出且评估步骤无法完成问题求助

SetFit二分类训练无损失输出、评估无法完成的排查方案
  • 补充TrainingArguments必要参数
    SetFit的TrainingArguments必须指定output_dir,否则模型保存环节会出错,直接导致评估流程卡壳。同时添加日志参数才能输出训练/验证损失:

    args = TrainingArguments(
        output_dir="./setfit_model_save",  # 必须设置模型输出目录
        batch_size=32,
        num_epochs=8,
        evaluation_strategy="epoch",
        save_strategy="epoch",
        load_best_model_at_end=True,
        logging_steps=1,  # 每步打印训练日志
        logging_dir="./setfit_logs"  # 日志存储目录
    )
    
  • 修正metric参数的传递方式
    SetFit的Trainer不支持直接传入字符串类型的metric,需要加载具体的metric对象:

    from datasets import load_metric
    
    # 加载accuracy指标对象
    metric = load_metric("accuracy")
    trainer = Trainer(
        model=model,
        args=args,
        train_dataset=dataset["train"],
        eval_dataset=dataset["eval"],
        metric=metric,  # 传入加载后的指标对象
        column_mapping={"text": "text", "label": "label"}
    )
    
  • 验证数据集label与模型labels的对应关系
    确保数据集中的label是整数类型,且和模型初始化时的labels列表顺序匹配:比如0对应negative,1对应positive。可以先检查数据集的label格式:

    print(dataset["train"]["label"])  # 确认输出是[0,1,0,...]这类整数
    

    如果数据集中的label是字符串(如"negative"/"positive"),需要先映射为整数:

    def convert_label(example):
        example["label"] = ["negative", "positive"].index(example["label"])
        return example
    
    dataset = dataset.map(convert_label)
    
  • 调整SetFit版本
    SetFit 1.0.1可能存在训练日志或评估流程的潜在bug,尝试降级到1.0.0版本测试:

    pip install setfit==1.0.0
    

内容的提问来源于stack exchange,提问作者Apatus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 23:52:15