Python spaCy和#x27之间的差异;s(v3.0)`nlp.make_doc(text)`和`nlp(text)`?培训时为什么要使用“nlp.make_doc(text)”呢?
我知道我们应该创建对象并将其传递给Python spaCy和#x27之间的差异;s(v3.0)`nlp.make_doc(text)`和`nlp(text)`?培训时为什么要使用“nlp.make_doc(text)”呢?,python,spacy,named-entity-recognition,ner,Python,Spacy,Named Entity Recognition,Ner,我知道我们应该创建对象并将其传递给nlp.update()方法。根据中的示例,我们有 对于原始文本,列数据中的实体偏移: 文档=nlp.make\u文档(原始文本) example=example.from_dict(doc,{“实体”:实体}) update([示例],sgd=optimizer) 看看make_doc()方法的用法,我们似乎只是对输入文本进行标记,然后对标记进行注释 但是示例对象应该具有参考/“金标准”和预测值。当我们调用nlp.make_doc()时,信息是如何在文档中结
nlp.update()
方法。根据中的示例,我们有
对于原始文本,列数据中的实体偏移:
文档=nlp.make\u文档(原始文本)
example=example.from_dict(doc,{“实体”:实体})
update([示例],sgd=optimizer)
看看make_doc()
方法的用法,我们似乎只是对输入文本进行标记,然后对标记进行注释
但是示例
对象应该具有参考/“金标准”和预测值。当我们调用nlp.make_doc()
时,信息是如何在文档中结束的
此外,当尝试从示例
对象获取预测的实体标记(使用经过训练的nlp
管道)时,我没有得到任何实体(尽管如果我使用nlp(文本)
创建对象,我可以得到实体。如果我尝试使用nlp(文本)
而不是nlp,则训练会崩溃。使用nlp制作文档(文本)
。。。
>>>spacy.pipeline.\u parser\u internal.ner.biluopusdown.set\u costs()
ValueError()
您也可以在Github讨论板上自由提问此类问题。同时感谢您在提问之前花时间思考并阅读一些代码。我希望每个问题都是这样
无论如何,我认为示例。from_dict()
构造函数可能会妨碍理解类的工作方式。这会让您更清楚吗
from spacy.tokens import Doc, Span
from spacy.training import Example
import spacy
nlp = spacy.blank("en")
# Build a reference Doc object, representing the gold standard.
y = Doc(
nlp.vocab,
words=["I", "work", "at", "Berlin!", ".", "It", "'s", "a", "hipster", "bar", "."]
)
# There are other ways we could set up the Doc object, including just passing
# stuff into the constructor. I wanted to show modifying the Doc to set annotations.
ent_start = y.text.index("Berlin!")
assert ent_start != -1
ent_end = ent_start + len("Berlin!")
y.ents = [y.char_span(ent_start, ent_end, label="ORG")]
# Okay, so we have our gold-standard, aka reference aka y, Doc object.
# Now, at runtime we won't necessarily be tokenizing that input text that way.
# It's a weird entity. If we only learn from the gold tokens, we can never learn
# to tag this correctly, no matter how many examples we see, if the predicted tokens
# don't match this tokenization. Because we'll always be learning from "Berlin!" but
# seeing "Berlin", "!" at runtime. We'll have train/test skew. Since spaCy cares how
# it does on actual text, not just on the benchmark (which is usually run with
# gold tokens), we want to train from samples that have the runtime tokenization. So
# the Example object holds a pair (x, y), where the x is the input.
x = nlp.make_doc(y.text)
example = Example(x, y)
# Show the aligned gold-standard NER tags. These should have the entity as B-ORG L-ORG.
print(example.get_aligned_ner())
另一条可能解释这一点的信息是,管道组件尝试处理部分注释,这样您就可以拥有预设某些实体的规则。这就是当您将一个完全注释的Doc
作为x
——它将这些注释作为输入的一部分,并且当模型试图构造最佳的操作序列以供学习时,没有有效的操作。这种情况下的可用性可以改进。nlp.make\u doc(text)
只在文本上运行标记化,不运行ner
或其他管道。我明白了。在这种情况下,我们永远不会在nlp.update()
步骤上传递任何预测,对吗?如果它们是由某个管道生成的,对吧。首先,感谢您的反馈!作为“新贡献者”,这让我在将来更愿意贡献。也感谢你的详细回答。它肯定变得更清晰了。最后一个问题:在你的例子中,example.predicted
实际上永远不会包含任何实体正确吗?因为x=nlp.make_doc(y.text)
?