python保存模型 drop_每次迭代后保存spacy的NER模型

每次迭代后,我都试图保存到Spacy自定义NER模型。我们是否有类似于tensorflow中的API,以在每次/特定的迭代次数后节省模型权重。然后我可以重新加载保存的模型并从那里继续训练。在

另外,如何在linux中使用系统上的所有内核。我发现四个核心中只有两个被使用。他们使用多任务CNN为NER,我知道这需要更多的时间来重新训练CPU。还有其他方法可以加快NER模型的训练。在@plac.annotations(

model=("Model name. Defaults to blank 'en' model.", "option", "m", str),

output_dir=("Optional output directory", "option", "o", Path),

n_iter=("Number of training iterations", "option", "n", int))

def main(model=None, output_dir=None, n_iter=100):

"""Load the model, set up the pipeline and train the entity recognizer."""

if model is not None:

nlp = spacy.load(model) # load existing spaCy model

print("Loaded model '%s'" % model)

else:

nlp = spacy.blank('en') # create blank Language class

print("Created blank 'en' model")

if 'ner' not in nlp.pipe_names:

ner = nlp.create_pipe('ner')

nlp.add_pipe(ner, last=True)

# otherwise, get it so we can add labels

else:

ner = nlp.get_pipe('ner')

# add labels

for _, annotations in TRAIN_DATA:

for ent in annotations.get('entities'):

ner.add_label(ent[2])

other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']

with nlp.disable_pipes(*other_pipes): # only train NER

optimizer = nlp.begin_training()

for itn in range(n_iter):

random.shuffle(TRAIN_DATA)

losses = {}

for text, annotations in TRAIN_DATA:

nlp.update(

[text], # batch of texts

[annotations], # batch of annotations

drop=0.5, # dropout - make it harder to memorise data

sgd=optimizer, # callable to update weights

losses=losses)

print(losses)

# save model to output directory

if output_dir is not None:

output_dir = Path(output_dir)

if not output_dir.exists():

output_dir.mkdir()

nlp.to_disk(output_dir)

print("Saved model to", output_dir)

if __name__ == '__main__':

plac.call(main)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值