Lighteval 文件

新增新指標

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

新增新指標

首先,檢查您是否可以使用語料庫指標樣本指標中的引數化函式。

如果不行,您可以使用 `custom_task` 系統來註冊您的新指標。

要檢視與自定義任務一起新增的自定義指標的示例,請參閱IFEval 自定義任務

要將您的自定義指標貢獻到 lighteval 倉庫,您首先需要執行 pip install -e .[dev] 來安裝所需的開發依賴,然後執行 pre-commit install 來安裝 pre-commit 鉤子。

  • 建立一個新的 Python 檔案,其中應包含您指標的完整邏輯。
  • 該檔案還需要以下列匯入開頭:
from aenum import extend_enum
from lighteval.metrics import Metrics

您需要定義一個樣本級指標。

def custom_metric(predictions: list[str], formatted_doc: Doc, **kwargs) -> bool:
    response = predictions[0]
    return response == formatted_doc.choices[formatted_doc.gold_index]

這裡的樣本級指標只返回一個指標,如果您想為每個樣本返回多個指標,您需要返回一個以指標為鍵、以值為值的字典。

def custom_metric(predictions: list[str], formatted_doc: Doc, **kwargs) -> dict:
    response = predictions[0]
    return {"accuracy": response == formatted_doc.choices[formatted_doc.gold_index], "other_metric": 0.5}

然後,如果需要,您可以定義一個聚合函式,一個常見的聚合函式是 np.mean

def agg_function(items):
    flat_items = [item for sublist in items for item in sublist]
    score = sum(flat_items) / len(flat_items)
    return score

最後,您可以定義您的指標。如果它是一個樣本級指標,您可以使用以下程式碼與SampleLevelMetric

my_custom_metric = SampleLevelMetric(
    metric_name={custom_metric_name},
    higher_is_better={either True or False},
    category={MetricCategory},
    use_case={MetricUseCase},
    sample_level_fn=custom_metric,
    corpus_level_fn=agg_function,
)

如果您的指標為每個樣本定義了多個指標,您可以使用以下程式碼與SampleLevelMetricGrouping

custom_metric = SampleLevelMetricGrouping(
    metric_name={submetric_names},
    higher_is_better={n: {True or False} for n in submetric_names},
    category={MetricCategory},
    use_case={MetricUseCase},
    sample_level_fn=custom_metric,
    corpus_level_fn={
        "accuracy": np.mean,
        "other_metric": agg_function,
    },
)

最後,新增以下內容,以便在作為模組載入時將您的指標新增到我們的指標列表中。

# Adds the metric to the metric list!
extend_enum(Metrics, "metric_name", metric_function)
if __name__ == "__main__":
    print("Imported metric")

然後,您可以在啟動 lighteval 時使用 --custom-tasks path_to_your_file 將您的自定義指標傳遞給它。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.