Evaluate 文件

結合自定義管道使用評估器

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

結合自定義管道使用評估器

評估器旨在與 transformer 管道開箱即用。然而,在許多情況下,你可能有一個不屬於 transformer 生態系統的模型或管道。你仍然可以使用 evaluator 輕鬆地為它們計算指標。在本指南中,我們將展示如何為 Scikit-Learn pipeline 和 Spacy pipeline 執行此操作。讓我們從 Scikit-Learn 的情況開始。

Scikit-Learn

首先,我們需要訓練一個模型。我們將在 IMDb 資料集上訓練一個簡單的文字分類器,所以讓我們先下載資料集。

from datasets import load_dataset

ds = load_dataset("imdb")

然後,我們可以構建一個簡單的 TF-IDF 預處理器和一個包裝在 Pipeline 中的樸素貝葉斯分類器。

from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer

text_clf = Pipeline([
        ('vect', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', MultinomialNB()),
])

text_clf.fit(ds["train"]["text"], ds["train"]["label"])

遵循 transformersTextClassificationPipeline 中的約定,我們的管道應該是可呼叫的,並返回一個字典列表。此外,我們使用 task 屬性來檢查管道是否與 evaluator 相容。為此,我們可以編寫一個小的包裝類。

class ScikitEvalPipeline:
    def __init__(self, pipeline):
        self.pipeline = pipeline
        self.task = "text-classification"

    def __call__(self, input_texts, **kwargs):
        return [{"label": p} for p in self.pipeline.predict(input_texts)]

pipe = ScikitEvalPipeline(text_clf)

我們現在可以將這個 pipeline 傳遞給 evaluator

from evaluate import evaluator

task_evaluator = evaluator("text-classification")
task_evaluator.compute(pipe, ds["test"], "accuracy")

>>> {'accuracy': 0.82956}

實現這個簡單的包裝器,就足以將任何框架的任何模型與 evaluator 一起使用。在 __call__ 方法中,你可以實現所有必要的邏輯,以高效地透過你的模型進行前向傳播。

Spacy

我們將使用 spacytextblob 專案的 polarity 特性來獲得一個簡單的情感分析器。首先,你需要安裝該專案並下載資源。

pip install spacytextblob
python -m textblob.download_corpora
python -m spacy download en_core_web_sm

然後,我們可以簡單地載入 nlp 管道並新增 spacytextblob 管道。

import spacy

nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')

這個程式碼片段展示了我們如何使用透過 spacytextblob 新增的 polarity 特性來獲取文字的情感。

texts = ["This movie is horrible", "This movie is awesome"]
results = nlp.pipe(texts)

for txt, res in zip(texts, results):
    print(f"{text} | Polarity: {res._.blob.polarity}")

現在,我們可以像前面的 Scikit-Learn 示例一樣,將其包裝在一個簡單的包裝類中。它只需要返回一個包含預測標籤的字典列表。如果極性大於0,我們將預測為積極情緒,否則為消極情緒。

class SpacyEvalPipeline:
    def __init__(self, nlp):
        self.nlp = nlp
        self.task = "text-classification"

    def __call__(self, input_texts, **kwargs):
        results =[]
        for p in self.nlp.pipe(input_texts):
            if p._.blob.polarity>=0:
                results.append({"label": 1})
            else:
                results.append({"label": 0})
        return results

pipe = SpacyEvalPipeline(nlp)

該類與 evaluator 相容,我們可以使用與前面示例相同的例項以及 IMDb 測試集。

eval.compute(pipe, ds["test"], "accuracy")
>>> {'accuracy': 0.6914}

這將比 Scikit-Learn 示例花費更長的時間,但在大約10-15分鐘後,你將得到評估結果!

< > 在 GitHub 上更新

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