Evaluate 文档

评估器

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

评估器

用于自动评估的评估器类。

评估器类

使用评估器的主要入口点

evaluate.evaluator

< >

( task: str = None ) Evaluator

参数

返回

评估器

适合该任务的评估器。

用于构建 Evaluator 的实用工厂方法。评估器封装了一个任务和一个默认指标名称。它们利用 transformerspipeline 功能来简化对给定任务的多种模型、数据集和指标组合的评估。

示例

>>> from evaluate import evaluator
>>> # Sentiment analysis evaluator
>>> evaluator("sentiment-analysis")

所有评估器类的基类

class evaluate.Evaluator

< >

( task: str default_metric_name: str = None )

Evaluator 类是所有评估器继承的类。有关不同评估器之间共享的方法,请参考此类。实现评估器操作的基类。

check_required_columns

< >

( data: typing.Union[str, datasets.arrow_dataset.Dataset] columns_names: typing.Dict[str, str] )

参数

  • data (str or Dataset) — 指定我们将进行评估的数据集。
  • columns_names (List[str]) — 数据集中要检查的列名列表。键是 evaluate.EvaluationModule.compute() 方法的参数,而值是要检查的列名。

确保评估所需的列存在于数据集中。

示例

>>> from datasets import load_dataset
>>> from evaluate import evaluator
>>> data = load_dataset("rotten_tomatoes', split="train")
>>> evaluator.check_required_columns(data, {"input_column": "text", "label_column": "label"})

compute_metric

< >

( metric: EvaluationModule metric_inputs: typing.Dict strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 random_state: typing.Optional[int] = None )

计算并返回指标。

get_dataset_split

< >

( data subset = None split = None ) split

参数

  • data (str) — 数据集名称。
  • subset (str) — 具有多种配置的数据集的配置名称(例如 ‘glue/cola’)。
  • split (str, 默认为 None) — 要使用的分割。

返回

split

包含要使用哪个分割的 `str`

如果给定 `None`,则推断使用哪个分割。

示例

>>> from evaluate import evaluator
>>> evaluator("text-classification").get_dataset_split(data="rotten_tomatoes")
WARNING:evaluate.evaluator.base:Dataset split not defined! Automatically evaluating with split: TEST
'test'

load_data

< >

( data: typing.Union[str, datasets.arrow_dataset.Dataset] subset: str = None split: str = None ) data (Dataset)

参数

  • data (Dataset or str, 默认为 None) — 指定我们将进行评估的数据集。如果类型是 str,我们将其视为数据集名称并加载它。否则我们假设它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 指定要传递给 load_datasetname 的数据集子集。用于具有多种配置的数据集(例如 glue/sst2)。
  • split (str, 默认为 None) — 用户定义的数据集分割名称(例如 train、validation、test)。支持切片分割 (test[:n])。如果未定义且数据是 str 类型,将通过 choose_split() 自动选择最佳分割。

返回

data (Dataset)

将用于评估的已加载数据集。

加载具有给定子集和分割的数据集。

示例

>>> from evaluate import evaluator
>>> evaluator("text-classification").load_data(data="rotten_tomatoes", split="train")
Dataset({
    features: ['text', 'label'],
    num_rows: 8530
})

predictions_processor

< >

( *args **kwargs )

Evaluator 类的一个核心方法,它处理流水线输出以兼容指标。

prepare_data

< >

( data: Dataset input_column: str label_column: str *args **kwargs ) dict

参数

  • data (Dataset) — 指定我们将进行评估的数据集。
  • input_column (str, 默认为 "text") — `data` 指定的数据集中包含文本特征的列的名称。
  • second_input_column(str, 可选) — 如果有第二个文本特征,则为包含该特征的列的名称。否则,设置为 None
  • label_column (str, 默认为 "label") — `data` 指定的数据集中包含标签的列的名称。

返回

字典

指标输入。`list`:流水线输入。

准备数据。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset

>>> ds = load_dataset("rotten_tomatoes", split="train")
>>> evaluator("text-classification").prepare_data(ds, input_column="text", second_input_column=None, label_column="label")

prepare_metric

< >

( metric: typing.Union[str, evaluate.module.EvaluationModule] )

参数

  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型是 str,我们将其视为指标名称并加载它。否则我们假设它代表一个预加载的指标。

准备指标。

示例

>>> from evaluate import evaluator
>>> evaluator("text-classification").prepare_metric("accuracy")

prepare_pipeline

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] tokenizer: typing.Union[ForwardRef('PreTrainedTokenizerBase'), ForwardRef('FeatureExtractionMixin')] = None feature_extractor: typing.Union[ForwardRef('PreTrainedTokenizerBase'), ForwardRef('FeatureExtractionMixin')] = None device: int = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将为任务初始化默认的流水线。如果参数类型为 str 或模型实例,我们用它来初始化一个新的 Pipeline。否则我们假设参数指定了一个预初始化的流水线。
  • preprocessor (PreTrainedTokenizerBaseFeatureExtractionMixin, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建流水线的模型,此参数可用于覆盖默认的预处理器。如果 model_or_pipelineNone 或一个预初始化的流水线,我们将忽略此参数。

准备流水线。

示例

>>> from evaluate import evaluator
>>> evaluator("text-classification").prepare_pipeline(model_or_pipeline="distilbert-base-uncased")

特定任务的评估器

ImageClassificationEvaluator

class evaluate.ImageClassificationEvaluator

< >

( task = 'image-classification' default_metric_name = None )

图像分类评估器。目前,此图像分类评估器可以从 evaluator() 使用默认任务名称 image-classification 加载。此类中的方法假定数据格式与 ImageClassificationPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None feature_extractor: typing.Union[str, ForwardRef('FeatureExtractionMixin'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'image' label_column: str = 'label' label_mapping: typing.Optional[typing.Dict[str, numbers.Number]] = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将为任务(在这种情况下是 text-classification 或其别名 sentiment-analysis)初始化默认的流水线。如果参数类型为 str 或模型实例,我们用它来初始化一个新的 Pipeline。否则我们假设参数指定了一个预初始化的流水线。
  • data (strDataset, 默认为 None) — 指定我们将进行评估的数据集。如果类型是 str,我们将其视为数据集名称并加载它。否则我们假设它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集分割。如果传入 None,则根据 choose_split 函数推断。
  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型是 str,我们将其视为指标名称并加载它。否则我们假设它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建流水线的模型,此参数可用于覆盖默认的 tokenizer。如果 model_or_pipelineNone 或一个预初始化的流水线,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 “simple”) — 指定评估策略。可能的值有:
  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,传递给 bootstrapconfidence_level 值。
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,传递给 bootstrapn_resamples 值。
  • device (int, 默认为 None) — 用于管道(pipeline)的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,设置为正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会自动推断,如果 CUDA:0 可用则使用它,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则将 random_state 值传递给 bootstrap。这对于调试非常有用。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("image-classification")
>>> data = load_dataset("beans", split="test[:40]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="nateraw/vit-base-beans",
>>>     data=data,
>>>     label_column="labels",
>>>     metric="accuracy",
>>>     label_mapping={'angular_leaf_spot': 0, 'bean_rust': 1, 'healthy': 2},
>>>     strategy="bootstrap"
>>> )

QuestionAnsweringEvaluator

class evaluate.QuestionAnsweringEvaluator

< >

( task = 'question-answering' default_metric_name = None )

问答评估器。该评估器处理抽取式问答,即从上下文中提取问题的答案。

目前,可以使用默认任务名称 question-answeringevaluator() 加载此问答评估器。

此类中的方法假定数据格式与 QuestionAnsweringPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None question_column: str = 'question' context_column: str = 'context' id_column: str = 'id' label_column: str = 'answers' squad_v2_format: typing.Optional[bool] = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化任务的默认管道(在此情况下为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或模型实例,我们用它来初始化一个新的、带有给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的管道。
  • data (strDataset, 默认为 None) — 指定我们将要进行评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集分割。如果传入 None,则根据 choose_split 函数进行推断。
  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 当 model_or_pipeline 代表一个我们为其构建管道的模型时,此参数可用于覆盖默认的分词器。如果 model_or_pipelineNone 或一个预初始化的管道,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 “simple”) — 指定评估策略。可能的值有:
  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,则将 confidence_level 值传递给 bootstrap
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,则将 n_resamples 值传递给 bootstrap
  • device (int, 默认为 None) — 用于管道(pipeline)的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,设置为正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会自动推断,如果 CUDA:0 可用则使用它,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则将 random_state 值传递给 bootstrap。这对于调试非常有用。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("question-answering")
>>> data = load_dataset("squad", split="validation[:2]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="sshleifer/tiny-distilbert-base-cased-distilled-squad",
>>>     data=data,
>>>     metric="squad",
>>> )

支持答案可能在上下文中缺失的数据集,例如 SQuAD v2 数据集。在这种情况下,向 compute() 调用传递 squad_v2_format=True 更为安全。

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("question-answering")
>>> data = load_dataset("squad_v2", split="validation[:2]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="mrm8488/bert-tiny-finetuned-squadv2",
>>>     data=data,
>>>     metric="squad_v2",
>>>     squad_v2_format=True,
>>> )

TextClassificationEvaluator

class evaluate.TextClassificationEvaluator

< >

( task = 'text-classification' default_metric_name = None )

文本分类评估器。目前,可以使用默认任务名称 text-classification 或别名 "sentiment-analysis"evaluator() 加载此文本分类评估器。此类中的方法假定数据格式与 TextClassificationPipeline 兼容——即单个文本特征作为输入,分类标签作为输出。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None feature_extractor: typing.Union[str, ForwardRef('FeatureExtractionMixin'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'text' second_input_column: typing.Optional[str] = None label_column: str = 'label' label_mapping: typing.Optional[typing.Dict[str, numbers.Number]] = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化任务的默认管道(在此情况下为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或模型实例,我们用它来初始化一个新的、带有给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的管道。
  • data (strDataset, 默认为 None) — 指定我们将要进行评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集分割。如果传入 None,则根据 choose_split 函数进行推断。
  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 当 model_or_pipeline 代表一个我们为其构建管道的模型时,此参数可用于覆盖默认的分词器。如果 model_or_pipelineNone 或一个预初始化的管道,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 “simple”) — 指定评估策略。可能的值有:
  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,则将 confidence_level 值传递给 bootstrap
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,则将 n_resamples 值传递给 bootstrap
  • device (int, 默认为 None) — 用于管道(pipeline)的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,设置为正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会自动推断,如果 CUDA:0 可用则使用它,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则将 random_state 值传递给 bootstrap。这对于调试非常有用。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("text-classification")
>>> data = load_dataset("imdb", split="test[:2]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="huggingface/prunebert-base-uncased-6-finepruned-w-distil-mnli",
>>>     data=data,
>>>     metric="accuracy",
>>>     label_mapping={"LABEL_0": 0.0, "LABEL_1": 1.0},
>>>     strategy="bootstrap",
>>>     n_resamples=10,
>>>     random_state=0
>>> )

TokenClassificationEvaluator

class evaluate.TokenClassificationEvaluator

< >

( task = 'token-classification' default_metric_name = None )

词元分类评估器。

目前,可以使用默认任务名称 token-classificationevaluator() 加载此词元分类评估器。

此类中的方法假定数据格式与 TokenClassificationPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: str = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: typing.Optional[int] = None random_state: typing.Optional[int] = None input_column: str = 'tokens' label_column: str = 'ner_tags' join_by: typing.Optional[str] = ' ' )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化任务的默认管道(在此情况下为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或模型实例,我们用它来初始化一个新的、带有给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的管道。
  • data (strDataset, 默认为 None) — 指定我们将要进行评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集分割。如果传入 None,则根据 choose_split 函数进行推断。
  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 当 model_or_pipeline 代表一个我们为其构建管道的模型时,此参数可用于覆盖默认的分词器。如果 model_or_pipelineNone 或一个预初始化的管道,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 “simple”) — 指定评估策略。可能的值有:
  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,则将 confidence_level 值传递给 bootstrap
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,则将 n_resamples 值传递给 bootstrap
  • device (int, 默认为 None) — 用于管道(pipeline)的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,设置为正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会自动推断,如果 CUDA:0 可用则使用它,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则将 random_state 值传递给 bootstrap。这对于调试非常有用。

计算给定管道和数据集组合的指标。

数据集的输入和标签列应分别格式化为单词列表和标签列表,遵循 conll2003 数据集 的格式。不支持输入为单个字符串、标签为偏移量列表的数据集。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("token-classification")
>>> data = load_dataset("conll2003", split="validation[:2]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="elastic/distilbert-base-uncased-finetuned-conll03-english",
>>>     data=data,
>>>     metric="seqeval",
>>> )

例如,评估器接受以下数据集格式

dataset = Dataset.from_dict(
    mapping={
        "tokens": [["New", "York", "is", "a", "city", "and", "Felix", "a", "person", "."]],
        "ner_tags": [[1, 2, 0, 0, 0, 0, 3, 0, 0, 0]],
    },
    features=Features({
        "tokens": Sequence(feature=Value(dtype="string")),
        "ner_tags": Sequence(feature=ClassLabel(names=["O", "B-LOC", "I-LOC", "B-PER", "I-PER"])),
        }),
)

例如,评估器接受以下数据集格式

dataset = Dataset.from_dict(
    mapping={
        "tokens": [["New York is a city and Felix a person."]],
        "starts": [[0, 23]],
        "ends": [[7, 27]],
        "ner_tags": [["LOC", "PER"]],
    },
    features=Features({
        "tokens": Value(dtype="string"),
        "starts": Sequence(feature=Value(dtype="int32")),
        "ends": Sequence(feature=Value(dtype="int32")),
        "ner_tags": Sequence(feature=Value(dtype="string")),
    }),
)

文本生成评估器 (TextGenerationEvaluator)

class evaluate.TextGenerationEvaluator

< >

( task = 'text-generation' default_metric_name = None predictions_prefix: str = 'generated' )

文本生成评估器。目前,可以使用默认任务名称 text-generationevaluator() 加载此文本生成评估器。此类中的方法假定数据格式与 TextGenerationPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None feature_extractor: typing.Union[str, ForwardRef('FeatureExtractionMixin'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'text' label_column: str = 'label' label_mapping: typing.Optional[typing.Dict[str, numbers.Number]] = None )

文本到文本生成评估器 (Text2TextGenerationEvaluator)

class evaluate.Text2TextGenerationEvaluator

< >

( task = 'text2text-generation' default_metric_name = None )

文本到文本生成评估器。目前,可以使用默认任务名称 text2text-generationevaluator() 加载此文本到文本生成评估器。此类中的方法假定数据格式与 Text2TextGenerationPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'text' label_column: str = 'label' generation_kwargs: dict = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化该任务的默认管道(在这种情况下为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或模型实例,我们用它来初始化一个新的、带有给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的管道。
  • data (strDataset, 默认为 None) — 指定我们将用于评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集划分。如果传入 None,则根据 choose_split 函数推断。
  • metric (strEvaluationModule, 默认为 None) — 指定评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建管道的模型,则此参数可用于覆盖默认的分词器。如果 model_or_pipelineNone 或一个预初始化的管道,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 "simple") — 指定评估策略。可能的值有:

  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,则传递给 bootstrapconfidence_level 值。
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,则传递给 bootstrapn_resamples 值。
  • device (int, 默认为 None) — 用于管道的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会进行推断,如果可用则使用 CUDA:0,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则传递给 bootstraprandom_state 值。对调试很有用。
  • input_column (str, 默认为 "text") — 由 data 指定的数据集中包含输入文本的列的名称。
  • label_column (str, 默认为 "label") — 由 data 指定的数据集中包含标签的列的名称。
  • generation_kwargs (Dict, 可选, 默认为 None) — generation_kwargs 传递给管道并设置文本生成策略。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("text2text-generation")
>>> data = load_dataset("cnn_dailymail", "3.0.0", split="validation[:40]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="facebook/bart-large-cnn",
>>>     data=data,
>>>     input_column="article",
>>>     label_column="highlights",
>>>     metric="rouge",
>>> )

摘要评估器 (SummarizationEvaluator)

class evaluate.SummarizationEvaluator

< >

( task = 'summarization' default_metric_name = None )

文本摘要评估器。目前,可以使用默认任务名称 summarizationevaluator() 加载此文本摘要评估器。此类中的方法假定数据格式与 SummarizationEvaluator 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'text' label_column: str = 'label' generation_kwargs: dict = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化该任务的默认管道(在这种情况下为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或模型实例,我们用它来初始化一个新的、带有给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的管道。
  • data (strDataset, 默认为 None) — 指定我们将用于评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集划分。如果传入 None,则根据 choose_split 函数推断。
  • metric (strEvaluationModule, 默认为 None) — 指定评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建管道的模型,则此参数可用于覆盖默认的分词器。如果 model_or_pipelineNone 或一个预初始化的管道,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 "simple") — 指定评估策略。可能的值有:

  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,则传递给 bootstrapconfidence_level 值。
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,则传递给 bootstrapn_resamples 值。
  • device (int, 默认为 None) — 用于管道的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会进行推断,如果可用则使用 CUDA:0,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则传递给 bootstraprandom_state 值。对调试很有用。
  • input_column (str, 默认为 "text") — 由 data 指定的数据集中包含输入文本的列的名称。
  • label_column (str, 默认为 "label") — 由 data 指定的数据集中包含标签的列的名称。
  • generation_kwargs (Dict, 可选, 默认为 None) — generation_kwargs 传递给管道并设置文本生成策略。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("summarization")
>>> data = load_dataset("cnn_dailymail", "3.0.0", split="validation[:40]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="facebook/bart-large-cnn",
>>>     data=data,
>>>     input_column="article",
>>>     label_column="highlights",
>>> )

翻译评估器 (TranslationEvaluator)

class evaluate.TranslationEvaluator

< >

( task = 'translation' default_metric_name = None )

翻译评估器。目前,可以使用默认任务名称 translationevaluator() 加载此翻译生成评估器。此类中的方法假定数据格式与 TranslationPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'text' label_column: str = 'label' generation_kwargs: dict = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化该任务的默认管道(在这种情况下为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或模型实例,我们用它来初始化一个新的、带有给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的管道。
  • data (strDataset, 默认为 None) — 指定我们将用于评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个预加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集划分。如果传入 None,则根据 choose_split 函数推断。
  • metric (strEvaluationModule, 默认为 None) — 指定评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个预加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建管道的模型,则此参数可用于覆盖默认的分词器。如果 model_or_pipelineNone 或一个预初始化的管道,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 "simple") — 指定评估策略。可能的值有:

  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,则传递给 bootstrapconfidence_level 值。
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,则传递给 bootstrapn_resamples 值。
  • device (int, 默认为 None) — 用于管道的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,则会进行推断,如果可用则使用 CUDA:0,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,则传递给 bootstraprandom_state 值。对调试很有用。
  • input_column (str, 默认为 "text") — data 指定的数据集中包含输入文本的列名。
  • label_column (str, 默认为 "label") — data 指定的数据集中包含标签的列名。
  • generation_kwargs (Dict, 可选, 默认为 None) — generation kwargs 会传递给 pipeline 并设置文本生成策略。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("translation")
>>> data = load_dataset("wmt19", "fr-de", split="validation[:40]")
>>> data = data.map(lambda x: {"text": x["translation"]["de"], "label": x["translation"]["fr"]})
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="Helsinki-NLP/opus-mt-de-fr",
>>>     data=data,
>>> )

AutomaticSpeechRecognitionEvaluator

class evaluate.AutomaticSpeechRecognitionEvaluator

< >

( task = 'automatic-speech-recognition' default_metric_name = None )

自动语音识别评估器。此自动语音识别评估器目前可从 evaluator() 使用默认任务名称 automatic-speech-recognition 加载。此类中的方法假定数据格式与 AutomaticSpeechRecognitionPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'path' label_column: str = 'sentence' generation_kwargs: dict = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化任务的默认 pipeline(在本例中为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或是模型实例,我们用它来初始化一个新的、使用给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的 pipeline。
  • data (strDataset, 默认为 None) — 指定我们将要进行评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个已加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集拆分。如果传入 None,则根据 choose_split 函数进行推断。
  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个已加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建 pipeline 的模型,则此参数可用于覆盖默认的 tokenizer。如果 model_or_pipelineNone 或是一个预初始化的 pipeline,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 “simple”) — 指定评估策略。可能的值有:
  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,传递给 bootstrapconfidence_level 值。
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,传递给 bootstrapn_resamples 值。
  • device (int, 默认为 None) — 用于 pipeline 的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,将进行推断,如果可用则使用 CUDA:0,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,传递给 bootstraprandom_state 值。对调试很有用。

计算给定管道和数据集组合的指标。

示例

>>> from evaluate import evaluator
>>> from datasets import load_dataset
>>> task_evaluator = evaluator("automatic-speech-recognition")
>>> data = load_dataset("mozilla-foundation/common_voice_11_0", "en", split="validation[:40]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline="https://huggingface.co/openai/whisper-tiny.en",
>>>     data=data,
>>>     input_column="path",
>>>     label_column="sentence",
>>>     metric="wer",
>>> )

AudioClassificationEvaluator

class evaluate.AudioClassificationEvaluator

< >

( task = 'audio-classification' default_metric_name = None )

音频分类评估器。此音频分类评估器目前可从 evaluator() 使用默认任务名称 audio-classification 加载。此类中的方法假定数据格式与 transformers.AudioClassificationPipeline 兼容。

compute

< >

( model_or_pipeline: typing.Union[str, ForwardRef('Pipeline'), typing.Callable, ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] = None data: typing.Union[str, datasets.arrow_dataset.Dataset] = None subset: typing.Optional[str] = None split: typing.Optional[str] = None metric: typing.Union[str, evaluate.module.EvaluationModule] = None tokenizer: typing.Union[str, ForwardRef('PreTrainedTokenizer'), NoneType] = None feature_extractor: typing.Union[str, ForwardRef('FeatureExtractionMixin'), NoneType] = None strategy: typing.Literal['simple', 'bootstrap'] = 'simple' confidence_level: float = 0.95 n_resamples: int = 9999 device: int = None random_state: typing.Optional[int] = None input_column: str = 'file' label_column: str = 'label' label_mapping: typing.Optional[typing.Dict[str, numbers.Number]] = None )

参数

  • model_or_pipeline (strPipelineCallablePreTrainedModelTFPreTrainedModel, 默认为 None) — 如果未指定此参数,我们将初始化任务的默认 pipeline(在本例中为 text-classification 或其别名 sentiment-analysis)。如果参数类型为 str 或是模型实例,我们用它来初始化一个新的、使用给定模型的 Pipeline。否则,我们假定该参数指定了一个预初始化的 pipeline。
  • data (strDataset, 默认为 None) — 指定我们将要进行评估的数据集。如果类型为 str,我们将其视为数据集名称并加载它。否则,我们假定它代表一个已加载的数据集。
  • subset (str, 默认为 None) — 定义要加载的数据集子集。如果传入 None,则加载默认子集。
  • split (str, 默认为 None) — 定义要加载的数据集拆分。如果传入 None,则根据 choose_split 函数进行推断。
  • metric (strEvaluationModule, 默认为 None) — 指定我们在评估器中使用的指标。如果类型为 str,我们将其视为指标名称并加载它。否则,我们假定它代表一个已加载的指标。
  • tokenizer (strPreTrainedTokenizer, 可选, 默认为 None) — 如果 model_or_pipeline 代表一个我们为其构建 pipeline 的模型,则此参数可用于覆盖默认的 tokenizer。如果 model_or_pipelineNone 或是一个预初始化的 pipeline,我们将忽略此参数。
  • strategy (Literal["simple", "bootstrap"], 默认为 “simple”) — 指定评估策略。可能的值有:
  • confidence_level (float, 默认为 0.95) — 如果选择 "bootstrap" 策略,传递给 bootstrapconfidence_level 值。
  • n_resamples (int, 默认为 9999) — 如果选择 "bootstrap" 策略,传递给 bootstrapn_resamples 值。
  • device (int, 默认为 None) — 用于 pipeline 的 CPU/GPU 支持的设备序号。将其设置为 -1 将使用 CPU,正整数将在相应的 CUDA 设备 ID 上运行模型。如果提供 None,将进行推断,如果可用则使用 CUDA:0,否则使用 CPU。
  • random_state (int, 可选, 默认为 None) — 如果选择 "bootstrap" 策略,传递给 bootstraprandom_state 值。对调试很有用。

计算给定管道和数据集组合的指标。

示例

请记住,为了处理音频文件,您需要安装 ffmpeg(https://ffmpeg.net.cn/download.html

>>> from evaluate import evaluator
>>> from datasets import load_dataset

>>> task_evaluator = evaluator("audio-classification")
>>> data = load_dataset("superb", 'ks', split="test[:40]")
>>> results = task_evaluator.compute(
>>>     model_or_pipeline=""superb/wav2vec2-base-superb-ks"",
>>>     data=data,
>>>     label_column="label",
>>>     input_column="file",
>>>     metric="accuracy",
>>>     label_mapping={0: "yes", 1: "no", 2: "up", 3: "down"}
>>> )

评估器也支持原始音频数据,形式为 numpy 数组。但是,请注意,调用音频列会自动解码和重采样音频文件,这对于大型数据集可能会很慢。

>>> from evaluate import evaluator
>>> from datasets import load_dataset

>>> task_evaluator = evaluator("audio-classification")
>>> data = load_dataset("superb", 'ks', split="test[:40]")
>>> data = data.map(lambda example: {"audio": example["audio"]["array"]})
>>> results = task_evaluator.compute(
>>>     model_or_pipeline=""superb/wav2vec2-base-superb-ks"",
>>>     data=data,
>>>     label_column="label",
>>>     input_column="audio",
>>>     metric="accuracy",
>>>     label_mapping={0: "yes", 1: "no", 2: "up", 3: "down"}
>>> )
< > 在 GitHub 上更新