spacy - 使用 SpaCy 的自定义 NER 训练模型无法训练
问题描述
这是我的 jupyter 笔记本中的全部代码
import spacy
import fitz
import pickle
import pandas as pd
import random
train_data = pickle.load(open('train_data.pkl', 'rb'))
train_data[0]
train_data[0] 的输出显示在这里
nlp = spacy.blank('en')
def train_model(train_data):
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last = True)
for _, annotation in train_data:
for ent in annotation['entities']:
ner.add_label(ent[2])
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
with nlp.disable_pipes(*other_pipes):
optimizer = nlp.begin_training()
for itn in range(10):
print('Starting iteration' + str(itn))
random.shuffle(train_data)
losses = {}
index = 0
# batch up the examples using spaCy's minibatch
#batches = minibatch(TRAIN_DATA, size=compounding(4.0, 32.0, 1.001))
for text, annotations in train_data:
try:
nlp.update(
[texts], # batch of texts
[annotations],# batch of annotations
sgd=optimizer,
drop=0.5, # dropout - make it harder to memorise data
losses=losses)
except Exception as e:
pass
print("Losses", losses)
train_model(train_data)
奇怪的是函数的输出是:
开始迭代0
损失{}
开始迭代1
损失{}
开始迭代2
损失{}
开始迭代3
损失{}
开始迭代4
损失{}
开始迭代5
损失{}
开始迭代6
损失{}
开始迭代7
损失{}
开始迭代8
损失{}
开始迭代9
损失{}
即使我可以运行 train_data 并获得输出,似乎根本没有数据进入模型!
spaCy 2.3.0 版
Python 3.7.3 版
解决方案
对于text, train_data 中的注释:
try:
nlp.update(
[***texts***], # batch of texts
[annotations],# batch of annotations
sgd=optimizer,
drop=0.5, # dropout - make it harder to memorise data
losses=losses).-----
用文本替换文本
推荐阅读
- r - ggplot2 合并数据框并绘图
- python - 图像匹配导致图像不应该是一个(Python opencv 教程)
- php - Add this loop into col-4 bootstrap grid layout
- angular - 如何使打字稿显式地评估胖箭头函数?
- go - Using function argument (parameter) in a constant context in Go
- amazon-web-services - 带有 USER_PASSWORD_AUTH 的 AWS Cognito 自定义身份验证流程
- azure-devops - 如何在 Web 部署 zip 中包含我的配置转换文件?
- c++ - 为什么 G++ 不警告 Const 成员的未使用结果?
- ios - 每列都有最后一列的值。SQLITE 数据库
- webpack - 如何从捆绑包中排除一些 npm 包的文件夹(或文件)?