其他

基础向量

classv3.7
词向量的抽象类

BaseVectors 是一个抽象类,用于支持自定义向量实现的开发。

要在StaticVectors训练中使用,必须实现get_batch方法。为提高性能,应在get_batch中使用高效批处理,并实现to_ops方法将向量数据复制到当前设备。可参考BPEmb子词嵌入的自定义实现示例。

BaseVectors.__init__ 方法

创建一个新的向量存储。

名称描述
仅关键字
stringsThe string store. A new string store is created if one is not provided. Defaults to None. Optional[StringStore]

BaseVectors.__getitem__ 方法

通过键获取向量。如果表中未找到该键,则应引发KeyError错误。

名称描述
keyThe key to get the vector for. Union[int, str]

BaseVectors.__len__ 方法

返回表中的向量数量。

名称描述

BaseVectors.__contains__ 方法

检查给定键是否存在向量条目。

名称描述
keyThe key to check. int

BaseVectors.add 方法

如果可能,向表中添加一个键。如果无法添加任何键,则返回 -1

名称描述
keyThe key to add. Union[str, int]

BaseVectors.shape 属性

获取向量表中行数和维度数的(rows, dims)元组。

名称描述

BaseVectors.size 属性

向量大小,即 rows * dims

名称描述

BaseVectors.is_full 属性

向量表是否已满且没有空槽位可供新键使用。

名称描述

BaseVectors.get_batch 方法v3.2

高效批量获取所提供键的向量。需要使用StaticVectors中的向量进行训练。

名称描述
keysThe keys. Iterable[Union[int, str]]

BaseVectors.to_ops 方法

虚拟方法。实现此方法以更改嵌入矩阵,使用不同的Thinc操作。

名称描述
opsThe Thinc ops to switch the embedding matrix to. Ops

BaseVectors.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]

BaseVectors.from_disk 方法

用于实现序列化的虚拟方法。实现从保存的管道中加载向量数据。

名称描述
pathA path to a directory. Paths may be either strings or Path-like objects. Union[str,Path]

BaseVectors.to_bytes 方法

用于实现序列化的虚拟方法。实现该方法可将向量数据序列化为二进制字符串。

名称描述

BaseVectors.from_bytes 方法

用于实现序列化的虚拟方法。实现从二进制字符串加载向量数据的功能。

名称描述
dataThe data to load from. bytes