python - How to train an NER model with different beam objective parameters in Spacy?
问题描述
I'm attempting to update a pre-trained spacy model en_core_web_md
with a few rounds of a beam objective other than beam_width = 1
, and I cannot seem to find the right way to pass the different parameters into the **cfg
such that the model uses them for training (at THIS point).
This was my latest attempt:
pipe_exceptions = ["ner", "trf_wordpiecer", "trf_tok2vec"]
other_pipes = [pipe for pipe in nlp.pipe_names if pipe not in pipe_exceptions]
# only train NER
with nlp.disable_pipes(*other_pipes), warnings.catch_warnings():
# show warnings for misaligned entity spans once
warnings.filterwarnings("once", category=UserWarning, module='spacy')
# TRY TO FORCE BEAM TRAINING INSTEAD OF GREEDY METHOD
nlp.use_params({'ner':{'beam_width':16, 'beam_density':0.0001}})
print(nlp.meta)
sizes = compounding(1.0, 4.0, 1.001)
# batch up the examples using spaCy's minibatch
for itn in range(n_iter):
random.shuffle(TRAIN_DATA_2)
batches = minibatch(TRAIN_DATA_2, size=sizes)
losses = {}
for batch in batches:
texts, annotations = zip(*batch)
nlp.update(texts,
annotations,
sgd=optimizer,
drop=0.35,
losses=losses
)
print("Losses", losses)
However, after training, the model/ner/cfg
file still lists:
{
"beam_width":1,
"beam_density":0.0,
"beam_update_prob":1.0,
...
So, I have a few questions:
- Am I able to update an existing greedy trained model with a new beam objective?
- If true, how can I properly changed these training parameters (and confirm they are changed)?
- If false, for a new from scratch model, how can I properly changed these training parameters (and confirm they are changed)?
Why do this? I am attempting to train a model that provides probabilities for NER decisions that I can surface to my users. THIS post and a few others show how to use beam_parse to obtain probabilities after the fact from a greedy model. However, they all mention that the greedy model hasn't been trained with a global objective, so these scores aren't especially meaningful unless you also perform some iterations of beam training as well. (link to github issue)
解决方案
我在这个堆栈帖子中找到了答案。这是修改配置参数的语法。
nlp.entity.cfg['beam_width'] = 16
nlp.entity.cfg['beam_density'] = 0.0001
我把这些行放在前面optimizer = nlp.resume_training()
,这些值用于训练。
推荐阅读
- image - 如何在 Drupal 8 中显示没有标题的图像?
- c++ - C++ 链表——读入文件并根据数字对其进行排序
- javascript - 从一组坐标中找到“直接”最近的坐标
- javascript - JSON 数据到 Google 工作表
- python-3.x - 使用 Python Selenium 运行循环
- python - pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])
- sql-server - SQL:就性能而言,一行中的逗号分隔字符串 V/S 多行单列
- google-chrome - NoSuchMethodError: org.openqa.selenium.os.CommandLine.findExecutable(Ljava/lang/String;)Ljava/lang/String; 使用 SoapUI 启动 Chrome 时
- php - 如何在sql中同时使用like和between?
- hyperlink - firefox 不能点链接,在其他浏览器可以