跳转到主要内容

一个旨在有效地增强NLP训练数据的Python库。

项目描述

Adept Augmentations

欢迎使用Adept Augmentations,可用于在少样本命名实体识别(NER)设置中创建附加数据!

Adept Augmentation是一个Python包,它使用spacydatasets包提供NER训练数据的数据增强功能。目前,我们支持一个增强器EntitySwapAugmenter,但我们计划添加更多

EntitySwapAugmenter 函数接受一个 datasets.Datasetspacy.tokens.DocBin。另外,还可以选择提供一组要包含在增强中的 labels。它最初创建了一个属于特定标签的实体知识库。在执行 augmenter.augment() 函数 N 次时,它将创建 N 个新的句子,这些句子通过从知识库中随机交换原始实体与具有相同对应标签的实体来生成。

例如,假设我们有关于人物、地点和产品的知识库。然后,我们可以使用 augmenter.augment(N=2) 创建句子“Momofuko Ando 在大阪发明了即食面条。”的附加数据,结果得到“David 在马德里发明了即食面条。”或“Tom 在荷兰发明了 Adept Augmentations。”。

Adept Augmentation 支持使用 IOB、IOB2、BIOES 和 BILUO 标记方案的 NER 标签,以及不遵循任何标记方案的标签。

用法

数据集

from datasets import load_dataset

from adept_augmentations import EntitySwapAugmenter

dataset = load_dataset("conll2003", split="train[:3]")
augmenter = EntitySwapAugmenter(dataset)
aug_dataset = augmenter.augment(N=4)

for entry in aug_dataset["tokens"]:
    print(entry)

# ['EU', 'rejects', 'British', 'call', 'to', 'boycott', 'British', 'lamb', '.']
# ['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'German', 'lamb', '.']
# ['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'lamb', '.']
# ['Peter', 'Blackburn']
# ['BRUSSELS', '1996-08-22']

spaCy

import spacy
from spacy.tokens import DocBin

from adept_augmentations import EntitySwapAugmenter

nlp = spacy.load("en_core_web_sm")

# Create some example training data
TRAIN_DATA = [
    "Apple is looking at buying U.K. startup for $1 billion",
    "Microsoft acquires GitHub for $7.5 billion",
]
docs = nlp.pipe(TRAIN_DATA)

# Create a new DocBin
doc_bin = DocBin(docs=docs)

doc_bin = EntitySwapAugmenter(doc_bin).augment(4)
for doc in doc_bin.get_docs(nlp.vocab):
    print(doc.text)

# GitHub is looking at buying U.K. startup for $ 7.5 billion
# Microsoft is looking at buying U.K. startup for $ 1 billion
# Microsoft is looking at buying U.K. startup for $ 7.5 billion
# GitHub is looking at buying U.K. startup for $ 1 billion
# Microsoft acquires Apple for $ 7.5 billion
# Apple acquires Microsoft for $ 1 billion
# Microsoft acquires Microsoft for $ 7.5 billion
# GitHub acquires GitHub for $ 1 billion

潜在的性能提升

数据增强可以显著提高模型在低数据场景下的性能。为了展示这一点,我们在 50、100、200、400 和 800 个第一个 CoNLL03 训练样本上训练了一个 SpanMarker NER 模型。

增强数据集的生成方式如下

# Select N (50, 100, 200, 400 or 800) samples from the gold training dataset
train_dataset = dataset["train"].select(range(N))

# Generate augmented dataset, with 4 * N samples
augmented_dataset = Augmenter(train_dataset).augment(N=4)

# Combine the original with the augmented to produce the full dataset
# to produce a dataset 5 times as big as the original
train_dataset = concatenate_datasets([augmented_dataset, train_dataset])

注意,基线使用 5 个周期。这样,两个实验之间的训练时间和步骤相同。所有场景都执行 5 次,我们报告平均值和标准误差。

原始 - 5 个周期 增强 - 1 个周期
N=50 0.387 ± 0.042 F1 0.484 ± 0.054 F1
N=100 0.585 ± 0.070 F1 0.663 ± 0.038 F1
N=200 0.717 ± 0.053 F1 0.757 ± 0.025 F1
N=400 0.816 ± 0.017 F1 0.826 ± 0.011 F1
N=800 0.859 ± 0.004 F1 0.862 ± 0.002 F1

(注意:这些结果未经优化,并不表示 SpanMarker 的最大性能。)

从这些结果可以看出,使用 adept_augmentations 进行数据增强可以显著提高低数据环境中的性能。

实现增强器

  • EntitySwapAugmenter
  • KnowledgeBaseSwapAugmenter
  • CoreferenceSwapAugmenter
  • SyntaticTreeSwapAugmenter

潜在集成

可能,我们可以考虑与其他不保留金标准知识的增强包集成。灵感来源包括

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

adept-augmentations-0.1.tar.gz (14.5 kB 查看哈希值)

上传时间

构建分布

adept_augmentations-0.1-py3-none-any.whl (13.9 kB 查看哈希值)

上传时间 Python 3

由以下提供支持