评估文档
使用自定义管道的 `evaluator`
并获得增强的文档体验
开始使用
使用自定义管道的 evaluator
evaluator
被设计为可以直接与 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"])
按照 transformers
的 TextClassificationPipeline
中的约定,我们的管道应该是可调用的,并返回字典列表。此外,我们使用 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 分钟后,您将获得评估结果!