How can we use Spacy minibatch and GoldParse to train NER model using BILUO tagging scheme?(如何使用Spacy mini Batch和GoldParse来训练使用BILUO标签方案的NER模型?)
本文介绍了如何使用Spacy mini Batch和GoldParse来训练使用BILUO标签方案的NER模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Spacy NER模型的输入数据在BILUO
标记方案中,我希望将其用作某些需求的一部分。当我试着在没有小批量的情况下简单地训练模型时,它工作得很好(注释部分)。但是我不知道如何在这里使用MiniBatch和GoldParse来提高模型的精度。我的期望在这里是有效的,因为我找不到一个具有这种组合的例子?此外,我已经用开始、结束、标签格式的方法训练了模型。请帮我弄清楚这一节。我的代码如下
import spacy
from spacy.gold import offsets_from_biluo_tags
from spacy.gold import biluo_tags_from_offsets
import random
from spacy.util import minibatch, compounding
from os import path
from tqdm import tqdm
def train_spacy(data, iterations, model=None):
TRAIN_DATA = data
print(f"downloads = {model}")
if model is not None and path.exists(model):
print(f"training existing model")
nlp = spacy.load(model)
print("Model is Loaded '%s'" % model)
else:
print(f"Creating new model")
nlp = spacy.blank('en') # create blank Language class
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last=True)
else:
ner = nlp.get_pipe('ner')
# Based on template, get labels and save those for further training
LABEL = ["Name", "ORG"]
for i in LABEL:
# print(i)
ner.add_label(i)
# get names of other pipes to disable them during training
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
with nlp.disable_pipes(*other_pipes): # only train NER
if model is None:
optimizer = nlp.begin_training()
else:
optimizer = nlp.entity.create_optimizer()
tags = dict()
for itn in range(iterations):
print("Starting iteration " + str(itn))
random.shuffle(TRAIN_DATA)
losses = {}
# for text, annotations in tqdm(TRAIN_DATA):
# print(f"text={text}, an={annotations}")
# tags['entities'] = offsets_from_biluo_tags(nlp(text), annotations)
# print(f"a={tags}")
# nlp.update([text], # batch of texts
# [tags], # batch of annotations
# drop=0.5, # dropout - make it harder to memorise data
# sgd=optimizer, # callable to update weights
# losses=losses)
# print(losses)
batches = minibatch(TRAIN_DATA, size=compounding(4.0, 16.0, 1.001))
# type 2 with mini batch
for batch in batches:
texts, annotations = zip(*batch)
print(texts)
tags = {'entities': annotations}
nlp.update(
texts, # batch of texts
[tags], # batch of annotations
drop=0.4, # dropout - make it harder to memorise data
losses=losses,
sgd=optimizer
)
print(losses)
return nlp
data_biluo = [
('I am Shah Khan, I work in MS Co', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'B-ORG', 'L-ORG']),
('I am Tom Tomb, I work in Telecom Networks', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'B-ORG', 'L-ORG'])
]
model = train_spacy(data_biluo, 10)
model.to_disk('./Vectors/')
推荐答案
您的微型批次有两个问题:
tags
应为带偏移量的NER标签的可迭代- 您的
data_biluo
没有说明句子中间的,
。
一旦你纠正了这些错误,你就可以走了:
import spacy
from spacy.gold import offsets_from_biluo_tags, GoldParse
from spacy.util import minibatch, compounding
import random
from tqdm import tqdm
def train_spacy(data, iterations, model=None):
TRAIN_DATA = data
print(f"downloads = {model}")
if model is not None and path.exists(model):
print(f"training existing model")
nlp = spacy.load(model)
print("Model is Loaded '%s'" % model)
else:
print(f"Creating new model")
nlp = spacy.blank('en') # create blank Language class
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last=True)
else:
ner = nlp.get_pipe('ner')
# Based on template, get labels and save those for further training
LABEL = ["Name", "ORG"]
for i in LABEL:
# print(i)
ner.add_label(i)
# get names of other pipes to disable them during training
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
with nlp.disable_pipes(*other_pipes): # only train NER
if model is None:
optimizer = nlp.begin_training()
else:
optimizer = nlp.entity.create_optimizer()
tags = dict()
for itn in range(iterations):
print("Starting iteration " + str(itn))
random.shuffle(TRAIN_DATA)
losses = {}
batches = minibatch(TRAIN_DATA, size=compounding(4.0, 16.0, 1.001))
# type 2 with mini batch
for batch in batches:
texts, _ = zip(*batch)
golds = [GoldParse(nlp.make_doc(t),entities = a) for t,a in batch]
nlp.update(
texts, # batch of texts
golds, # batch of annotations
drop=0.4, # dropout - make it harder to memorise data
losses=losses,
sgd=optimizer
)
print(losses)
return nlp
data_biluo = [
('I am Shah Khan, I work in MS Co', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'O', 'B-ORG', 'L-ORG']),
('I am Tom Tomb, I work in Telecom Networks', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'O', 'B-ORG', 'L-ORG'])
]
model = train_spacy(data_biluo, 10)
Starting iteration 0
{'ner': 17.999998331069946}
Starting iteration 1
{'ner': 16.6766300201416}
Starting iteration 2
{'ner': 16.997647166252136}
Starting iteration 3
{'ner': 16.486496448516846}
Starting iteration 4
{'ner': 15.695325374603271}
Starting iteration 5
{'ner': 14.312554001808167}
Starting iteration 6
{'ner': 12.099276185035706}
Starting iteration 7
{'ner': 11.473928153514862}
Starting iteration 8
{'ner': 8.814643770456314}
Starting iteration 9
{'ner': 7.233813941478729}
这篇关于如何使用Spacy mini Batch和GoldParse来训练使用BILUO标签方案的NER模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何使用Spacy mini Batch和GoldParse来训练使用BILUO标签方案的NER模型?
基础教程推荐
猜你喜欢
- 将 YAML 文件转换为 python dict 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01