Tok2Vec
应用"token-to-vector"模型并将其输出设置到Doc.tensor属性中。这种方法主要用于在多个组件间共享单一子网络,例如让DependencyParser、Tagger和EntityRecognizer共享同一个嵌入和CNN网络。
为了使用Tok2Vec的预测结果,后续组件应在其模型中使用Tok2VecListener层作为tok2vec子网络。该层在预测时会从doc.tensor属性读取数据。在训练过程中,Tok2Vec组件会为每个批次保存其预测结果和反向传播回调,以便后续组件能够将梯度传播到共享权重。采用这种实现方式是因为它可以避免依赖模型内部的对象标识来实现参数共享。
配置与实现
默认配置由管道组件工厂定义,描述了组件应如何配置。您可以通过nlp.add_pipe中的config参数或在训练用的config.cfg中覆盖其设置。有关架构及其参数和超参数的详细信息,请参阅模型架构文档。
| 设置 | 描述 |
|---|---|
model | The model to use. Defaults to HashEmbedCNN. Model[List[Doc], List[Floats2d] |
explosion/spaCy/master/spacy/pipeline/tok2vec.py
Tok2Vec.__init__ 方法
创建一个新的管道实例。在您的应用程序中,通常会使用快捷方式,通过其字符串名称并使用nlp.add_pipe来实例化该组件。
| 名称 | 描述 |
|---|---|
vocab | The shared vocabulary. Vocab |
model | The Thinc Model powering the pipeline component. Model[List[Doc], List[Floats2d] |
name | String name of the component instance. Used to add entries to the losses during training. str |
Tok2Vec.__call__ 方法
将管道应用于一个文档,并将上下文相关嵌入添加到Doc.tensor属性中,使其能够被下游组件用作特征。文档会被原地修改并返回。这通常在nlp对象对文本调用时自动发生,所有管道组件会按顺序应用于Doc。__call__和pipe都会委托给predict和set_annotations方法。
| 名称 | 描述 |
|---|---|
doc | The document to process. Doc |
| 返回值 | 处理后的文档。Doc |
Tok2Vec.pipe 方法
将管道应用于文档流。这通常在调用nlp对象处理文本时自动进行,所有管道组件会按顺序应用于Doc对象。无论是__call__还是pipe方法,都会委托给predict和set_annotations方法执行。
| 名称 | 描述 |
|---|---|
stream | A stream of documents. Iterable[Doc] |
| 仅关键字 | |
batch_size | The number of documents to buffer. Defaults to 128. int |
| YIELDS | 按顺序处理后的文档。Doc |
Tok2Vec.initialize 方法
初始化组件用于训练并返回一个Optimizer。get_examples应为一个返回可迭代Example对象的函数。至少需要提供一个示例。这些数据示例用于初始化组件模型,可以是完整训练数据或代表性样本。初始化过程包括验证网络、推断缺失形状以及根据数据设置标签方案。该方法通常由Language.initialize调用。
| 名称 | 描述 |
|---|---|
get_examples | Function that returns gold-standard annotations in the form of Example objects. Must contain at least one Example. Callable[[], Iterable[Example]] |
| 仅关键字 | |
nlp | The current nlp object. Defaults to None. Optional[Language] |
Tok2Vec.predict 方法
在不修改的情况下,将组件的模型应用于一批Doc对象。
| 名称 | 描述 |
|---|---|
docs | The documents to predict. Iterable[Doc] |
| 返回值 | 模型对每个文档的预测结果。 |
Tok2Vec.set_annotations 方法
使用预先计算的分数修改一批Doc对象。
| 名称 | 描述 |
|---|---|
docs | The documents to modify. Iterable[Doc] |
scores | The scores to set, produced by Tok2Vec.predict. |
Tok2Vec.update 方法
从一批包含预测和黄金标准注释的Example对象中学习,并更新组件的模型。委托给predict。
| 名称 | 描述 |
|---|---|
examples | A batch of Example objects to learn from. Iterable[Example] |
| 仅关键字 | |
drop | The dropout rate. float |
sgd | An optimizer. Will be created via create_optimizer if not set. Optional[Optimizer] |
losses | Optional record of the loss during training. Updated using the component name as the key. Optional[Dict[str, float]] |
| RETURNS | The updated losses dictionary. Dict[str, float] |
Tok2Vec.create_optimizer 方法
为管道组件创建一个优化器。
| 名称 | 描述 |
|---|---|
| 返回值 | 优化器。Optimizer |
Tok2Vec.use_params 方法上下文管理器
修改管道的模型以使用给定的参数值。在上下文结束时,原始参数将被恢复。
| 名称 | 描述 |
|---|---|
params | The parameter values to use in the model. dict |
Tok2Vec.to_disk 方法
将管道序列化到磁盘。
| 名称 | 描述 |
|---|---|
path | A path to a directory, which will be created if it doesn’t exist. Paths may be either strings or Path-like objects. Union[str,Path] |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
Tok2Vec.from_disk 方法
从磁盘加载管道。就地修改对象并返回它。
| 名称 | 描述 |
|---|---|
path | A path to a directory. Paths may be either strings or Path-like objects. Union[str,Path] |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| RETURNS | The modified Tok2Vec object. Tok2Vec |
Tok2Vec.to_bytes 方法
将管道序列化为字节串。
| 名称 | 描述 |
|---|---|
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| RETURNS | The serialized form of the Tok2Vec object. bytes |
Tok2Vec.from_bytes 方法
从字节串加载管道。原地修改对象并返回它。
| 名称 | 描述 |
|---|---|
bytes_data | The data to load from. bytes |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| RETURNS | The Tok2Vec object. Tok2Vec |
序列化字段
在序列化过程中,spaCy会导出多个用于恢复对象不同方面的数据字段。如果需要,您可以通过exclude参数传入字符串名称来将它们排除在序列化之外。
| 名称 | 描述 |
|---|---|
vocab | The shared Vocab. |
cfg | The config file. You usually don’t want to exclude this. |
model | The binary model data. You usually don’t want to exclude this. |