【发布时间】:2022-07-04 02:30:24
【问题描述】:
我正在尝试修改 spaCy 的 softmax 输出,但我不了解 spaCy 如何使用 Thinc 预测功能。
我曾假设每次将 Thinc 模型预测函数作为 spaCy 管道的一部分调用时,它都会以相同的格式返回数据。但是,当我在下面的代码中的 \'preds\' 处放置断点时,我可以看到从 self._func 返回的数据以两种格式返回数据:
- numpy 数组列表 - 一个数组 我相信包含每个模型分类的 softmax 分数 预言。
- 一个 space.ml.parser_model.ParserStepModel 对象。我不是 确定模型如何或为什么以这种格式返回数据。
我希望有人能解释为什么 Thinc 模型返回 ParserStepModel 对象以及它如何用作 spaCy 管道的一部分。另外,如果有人知道我如何检测 \'preds\' 数据类型是什么(我尝试过 isinstance 失败)。
import spacy
from thinc.model import Model, InT, OutT
import numpy as np
def predict(self, X:InT) -> OutT:
preds = self._func(self, X, is_train=False)[0]
return preds
Model.predict = predict
nlp = spacy.load(\'en_core_web_sm\')
def show_ents(doc):
if doc.ents:
for ent in doc.ents:
print(ent.text + \' - \' + str(ent.start_char) + \' - \' + str(ent.end) + \' - \' +
ent.label_ + \' - \' + str(spacy.explain(ent.label_)))
else:
print(\'No named entities found.\')
doc = nlp(\'Apple is looking at buying U.K. startup for $1 billion\')
show_ents(doc)