可训练管道
该类是一个基类,不能直接实例化。可训练管道组件如EntityRecognizer或TextCategorizer都继承自它,它定义了组件在spaCy管道中作为可训练组件应遵循的接口。有关如何使用TrainablePipe基类实现自定义组件,请参阅编写可训练组件的文档。
explosion/spaCy/master/spacy/pipeline/trainable_pipe.pyx
TrainablePipe.__init__ 方法
创建一个新的管道实例。在您的应用程序中,通常会使用快捷方式,通过其字符串名称并使用nlp.add_pipe来实例化该组件。
| 名称 | 描述 |
|---|---|
vocab | The shared vocabulary. Vocab |
model | The Thinc Model powering the pipeline component. Model[List[Doc], Any] |
name | String name of the component instance. Used to add entries to the losses during training. str |
**cfg | Additional config parameters and settings. Will be available as the dictionary cfg and is serialized with the component. |
TrainablePipe.__call__ 方法
将管道应用于单个文档。文档会被原地修改并返回。
这通常在调用nlp对象处理文本时自动完成,
所有管道组件会按顺序应用于Doc对象。
__call__和pipe方法都会委托给
predict和
set_annotations方法执行。
| 名称 | 描述 |
|---|---|
doc | The document to process. Doc |
| 返回值 | 处理后的文档。Doc |
TrainablePipe.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 |
TrainablePipe.set_error_handler 方法v3.0
定义一个回调函数,当使用__call__或pipe处理一个或多个文档时抛出错误将被调用。错误处理程序将接收原始组件的名称、组件本身、正在处理的文档列表以及原始错误作为参数。
| 名称 | 描述 |
|---|---|
error_handler | A function that performs custom error handling. Callable[[str, Callable[[Doc],Doc], List[Doc], Exception] |
TrainablePipe.get_error_handler 方法v3.0
获取为该组件的__call__和pipe方法执行错误处理的回调函数。如果之前未使用set_error_handler定义自定义函数,则返回一个默认函数,该函数仅重新抛出异常。
| 名称 | 描述 |
|---|---|
| 返回值 | 执行自定义错误处理的函数。可调用[[str, 可调用[[Doc],Doc], 列表[Doc], 异常] |
TrainablePipe.initialize 方法v3.0
初始化组件以进行训练。get_examples应为一个返回可迭代Example对象的函数。这些数据示例用于初始化模型组件,可以是完整的训练数据或代表性样本。初始化包括验证网络、推断缺失形状以及根据数据设置标签方案。该方法通常由Language.initialize调用。
| 名称 | 描述 |
|---|---|
get_examples | Function that returns gold-standard annotations in the form of Example objects. Callable[[], Iterable[Example]] |
| 仅关键字 | |
nlp | The current nlp object. Defaults to None. Optional[Language] |
TrainablePipe.predict 方法
将组件的模型应用于一批Doc对象,而不修改它们。
| 名称 | 描述 |
|---|---|
docs | The documents to predict. Iterable[Doc] |
| 返回值 | 模型对每个文档的预测结果。 |
TrainablePipe.set_annotations 方法
使用预先计算的分数修改一批Doc对象。
| 名称 | 描述 |
|---|---|
docs | The documents to modify. Iterable[Doc] |
scores | The scores to set, produced by Tagger.predict. |
TrainablePipe.update 方法
从一批包含预测和黄金标准注释的Example对象中学习,并更新组件的模型。
| 名称 | 描述 |
|---|---|
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] |
TrainablePipe.rehearse 方法实验性v3.0
对一批数据执行"预演"更新。预演更新旨在教导当前模型做出与初始模型相似的预测,以尝试解决"灾难性遗忘"问题。此功能为实验性功能。
| 名称 | 描述 |
|---|---|
examples | A batch of Example objects to learn from. Iterable[Example] |
| 仅关键字 | |
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] |
TrainablePipe.get_loss 方法
计算这批文档及其预测分数的损失和损失梯度。
| 名称 | 描述 |
|---|---|
examples | The batch of examples. Iterable[Example] |
scores | Scores representing the model’s predictions. |
| RETURNS | The loss and the gradient, i.e. (loss, gradient). Tuple[float, float] |
TrainablePipe.score 方法v3.0
对一批示例进行评分。
| 名称 | 描述 |
|---|---|
examples | The examples to score. Iterable[Example] |
| 仅关键字 | |
\*\*kwargs | Any additional settings to pass on to the scorer. Any |
| RETURNS | The scores, e.g. produced by the Scorer. Dict[str, Union[float, Dict[str, float]]] |
TrainablePipe.create_optimizer 方法
为管道组件创建一个优化器。默认为使用默认设置的Adam。
| 名称 | 描述 |
|---|---|
| 返回值 | 优化器。Optimizer |
TrainablePipe.use_params 方法上下文管理器
修改管道的模型,以使用给定的参数值。在上下文结束时,原始参数将被恢复。
| 名称 | 描述 |
|---|---|
params | The parameter values to use in the model. dict |
TrainablePipe.finish_update 方法
使用当前参数梯度更新参数。默认调用self.model.finish_update。
| 名称 | 描述 |
|---|---|
sgd | An optimizer. Optional[Optimizer] |
TrainablePipe.add_label 方法
向管道添加一个新标签,该标签将由模型预测。具体实现取决于特定组件,但一般来说,如果输出维度已设置或模型已完全初始化,则不应调用add_label。如果违反这些条件,函数将引发错误。此规则的例外情况是当组件为可调整大小时,此时应调用set_output以确保模型被正确调整大小。
| 名称 | 描述 |
|---|---|
label | The label to add. str |
| 返回值 | 如果标签已存在则返回0,否则返回1。int |
请注意,通常情况下,如果您向initialize方法提供了代表性数据样本,就不需要调用pipe.add_label。在这种情况下,样本中发现的所有标签都会自动添加到模型中,输出维度也会被自动推断。
TrainablePipe.is_resizable 属性
检查该组件的模型输出维度是否可以调整。如果此方法返回True,则可以调用set_output来更改模型的输出维度。
对于不可调整大小的内置组件,您必须从头开始创建并训练一个具有适当架构和输出维度的新模型。对于自定义组件,您可以实现一个resize_output函数,并将其作为属性添加到组件的模型中。
| 名称 | 描述 |
|---|---|
| 返回值 | 模型初始化后是否可以更改输出维度。bool |
TrainablePipe.set_output 方法
更改组件模型的输出维度。如果组件不可调整大小,此方法将引发NotImplementedError。如果组件可调整大小,则会调用模型的属性resize_output。这是一个接收原始模型和新输出维度nO的函数,并会就地修改模型。在调整已训练模型的尺寸时,应注意避免"灾难性遗忘"问题。
| 名称 | 描述 |
|---|---|
nO | The new output dimension. int |
TrainablePipe.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] |
TrainablePipe.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] |
| 返回值 | 修改后的管道。TrainablePipe |
TrainablePipe.to_bytes 方法
将管道序列化为字节串。
| 名称 | 描述 |
|---|---|
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| 返回值 | 管道的序列化形式。bytes |
TrainablePipe.from_bytes 方法
从字节串加载管道。原地修改对象并返回它。
| 名称 | 描述 |
|---|---|
bytes_data | The data to load from. bytes |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| 返回值 | 该管道。TrainablePipe |
属性
| 名称 | 描述 |
|---|---|
vocab | The shared vocabulary that’s passed in on initialization. Vocab |
model | The model powering the component. Model[List[Doc], Any] |
name | The name of the component instance in the pipeline. Can be used in the losses. str |
cfg | Keyword arguments passed to TrainablePipe.__init__. Will be serialized with the component. Dict[str, Any] |
序列化字段
在序列化过程中,spaCy会导出多个用于恢复对象不同方面的数据字段。如果需要,您可以通过exclude参数传入字符串名称来将它们排除在序列化之外。
| 名称 | 描述 |
|---|---|
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. |