流水线

可训练管道

class
可训练管道组件的基础类

该类是一个基类,不能直接实例化。可训练管道组件如EntityRecognizerTextCategorizer都继承自它,它定义了组件在spaCy管道中作为可训练组件应遵循的接口。有关如何使用TrainablePipe基类实现自定义组件,请参阅编写可训练组件的文档。

explosion/spaCy/master/spacy/pipeline/trainable_pipe.pyx

TrainablePipe.__init__ 方法

创建一个新的管道实例。在您的应用程序中,通常会使用快捷方式,通过其字符串名称并使用nlp.add_pipe来实例化该组件。

名称描述
vocabThe shared vocabulary. Vocab
modelThe Thinc Model powering the pipeline component. Model[List[Doc], Any]
nameString name of the component instance. Used to add entries to the losses during training. str
**cfgAdditional config parameters and settings. Will be available as the dictionary cfg and is serialized with the component.

TrainablePipe.__call__ 方法

将管道应用于单个文档。文档会被原地修改并返回。 这通常在调用nlp对象处理文本时自动完成, 所有管道组件会按顺序应用于Doc对象。 __call__pipe方法都会委托给 predictset_annotations方法执行。

名称描述
docThe document to process. Doc

TrainablePipe.pipe 方法

将管道应用于文档流。这通常在调用nlp对象处理文本时自动进行,所有管道组件会按顺序应用于Doc对象。__call__pipe都会委托给predictset_annotations方法。

名称描述
streamA stream of documents. Iterable[Doc]
仅关键字
batch_sizeThe number of documents to buffer. Defaults to 128. int

TrainablePipe.set_error_handler 方法v3.0

定义一个回调函数,当使用__call__pipe处理一个或多个文档时抛出错误将被调用。错误处理程序将接收原始组件的名称、组件本身、正在处理的文档列表以及原始错误作为参数。

名称描述
error_handlerA 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定义自定义函数,则返回一个默认函数,该函数仅重新抛出异常。

名称描述

TrainablePipe.initialize 方法v3.0

初始化组件以进行训练。get_examples应为一个返回可迭代Example对象的函数。这些数据示例用于初始化模型组件,可以是完整的训练数据或代表性样本。初始化包括验证网络、推断缺失形状以及根据数据设置标签方案。该方法通常由Language.initialize调用。

名称描述
get_examplesFunction that returns gold-standard annotations in the form of Example objects. Callable[[], Iterable[Example]]
仅关键字
nlpThe current nlp object. Defaults to None. Optional[Language]

TrainablePipe.predict 方法

将组件的模型应用于一批Doc对象,而不修改它们。

名称描述
docsThe documents to predict. Iterable[Doc]

TrainablePipe.set_annotations 方法

使用预先计算的分数修改一批Doc对象。

名称描述
docsThe documents to modify. Iterable[Doc]
scoresThe scores to set, produced by Tagger.predict.

TrainablePipe.update 方法

从一批包含预测和黄金标准注释的Example对象中学习,并更新组件的模型。

名称描述
examplesA batch of Example objects to learn from. Iterable[Example]
仅关键字
dropThe dropout rate. float
sgdAn optimizer. Will be created via create_optimizer if not set. Optional[Optimizer]
lossesOptional record of the loss during training. Updated using the component name as the key. Optional[Dict[str, float]]

TrainablePipe.rehearse 方法实验性v3.0

对一批数据执行"预演"更新。预演更新旨在教导当前模型做出与初始模型相似的预测,以尝试解决"灾难性遗忘"问题。此功能为实验性功能。

名称描述
examplesA batch of Example objects to learn from. Iterable[Example]
仅关键字
sgdAn optimizer. Will be created via create_optimizer if not set. Optional[Optimizer]
lossesOptional record of the loss during training. Updated using the component name as the key. Optional[Dict[str, float]]

TrainablePipe.get_loss 方法

计算这批文档及其预测分数的损失和损失梯度。

名称描述
examplesThe batch of examples. Iterable[Example]
scoresScores representing the model’s predictions.

TrainablePipe.score 方法v3.0

对一批示例进行评分。

名称描述
examplesThe examples to score. Iterable[Example]
仅关键字
\*\*kwargsAny additional settings to pass on to the scorer. Any

TrainablePipe.create_optimizer 方法

为管道组件创建一个优化器。默认为使用默认设置的Adam

名称描述

TrainablePipe.use_params 方法上下文管理器

修改管道的模型,以使用给定的参数值。在上下文结束时,原始参数将被恢复。

名称描述
paramsThe parameter values to use in the model. dict

TrainablePipe.finish_update 方法

使用当前参数梯度更新参数。默认调用self.model.finish_update

名称描述
sgdAn optimizer. Optional[Optimizer]

TrainablePipe.add_label 方法

向管道添加一个新标签,该标签将由模型预测。具体实现取决于特定组件,但一般来说,如果输出维度已设置或模型已完全初始化,则不应调用add_label。如果违反这些条件,函数将引发错误。此规则的例外情况是当组件为可调整大小时,此时应调用set_output以确保模型被正确调整大小。

名称描述
labelThe label to add. str

请注意,通常情况下,如果您向initialize方法提供了代表性数据样本,就不需要调用pipe.add_label。在这种情况下,样本中发现的所有标签都会自动添加到模型中,输出维度也会被自动推断

TrainablePipe.is_resizable 属性

检查该组件的模型输出维度是否可以调整。如果此方法返回True,则可以调用set_output来更改模型的输出维度。

对于不可调整大小的内置组件,您必须从头开始创建并训练一个具有适当架构和输出维度的新模型。对于自定义组件,您可以实现一个resize_output函数,并将其作为属性添加到组件的模型中。

名称描述

TrainablePipe.set_output 方法

更改组件模型的输出维度。如果组件不可调整大小,此方法将引发NotImplementedError。如果组件可调整大小,则会调用模型的属性resize_output。这是一个接收原始模型和新输出维度nO的函数,并会就地修改模型。在调整已训练模型的尺寸时,应注意避免"灾难性遗忘"问题。

名称描述
nOThe new output dimension. int

TrainablePipe.to_disk 方法

将管道序列化到磁盘。

名称描述
pathA 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]
仅关键字
excludeString names of serialization fields to exclude. Iterable[str]

TrainablePipe.from_disk 方法

从磁盘加载管道。就地修改对象并返回它。

名称描述
pathA path to a directory. Paths may be either strings or Path-like objects. Union[str,Path]
仅关键字
excludeString names of serialization fields to exclude. Iterable[str]

TrainablePipe.to_bytes 方法

将管道序列化为字节串。

名称描述
仅关键字
excludeString names of serialization fields to exclude. Iterable[str]

TrainablePipe.from_bytes 方法

从字节串加载管道。原地修改对象并返回它。

名称描述
bytes_dataThe data to load from. bytes
仅关键字
excludeString names of serialization fields to exclude. Iterable[str]

属性

名称描述
vocabThe shared vocabulary that’s passed in on initialization. Vocab
modelThe model powering the component. Model[List[Doc], Any]
nameThe name of the component instance in the pipeline. Can be used in the losses. str
cfgKeyword arguments passed to TrainablePipe.__init__. Will be serialized with the component. Dict[str, Any]

序列化字段

在序列化过程中,spaCy会导出多个用于恢复对象不同方面的数据字段。如果需要,您可以通过exclude参数传入字符串名称来将它们排除在序列化之外。

名称描述
cfgThe config file. You usually don’t want to exclude this.
modelThe binary model data. You usually don’t want to exclude this.