基础向量
BaseVectors 是一个抽象类,用于支持自定义向量实现的开发。
要在StaticVectors训练中使用,必须实现get_batch方法。为提高性能,应在get_batch中使用高效批处理,并实现to_ops方法将向量数据复制到当前设备。可参考BPEmb子词嵌入的自定义实现示例。
BaseVectors.__init__ 方法
创建一个新的向量存储。
| 名称 | 描述 |
|---|---|
| 仅关键字 | |
strings | The string store. A new string store is created if one is not provided. Defaults to None. Optional[StringStore] |
BaseVectors.__getitem__ 方法
通过键获取向量。如果表中未找到该键,则应引发KeyError错误。
| 名称 | 描述 |
|---|---|
key | The key to get the vector for. Union[int, str] |
| 返回值 | 该键的向量。numpy.ndarray[ndim=1, dtype=float32] |
BaseVectors.__len__ 方法
返回表中的向量数量。
| 名称 | 描述 |
|---|---|
| 返回值 | 表中向量的数量。int |
BaseVectors.__contains__ 方法
检查给定键是否存在向量条目。
| 名称 | 描述 |
|---|---|
key | The key to check. int |
| 返回值 | 判断该键是否有向量条目。bool |
BaseVectors.add 方法
如果可能,向表中添加一个键。如果无法添加任何键,则返回 -1。
| 名称 | 描述 |
|---|---|
key | The key to add. Union[str, int] |
| RETURNS | The row the vector was added to, or -1 if the operation is not supported. int |
BaseVectors.shape 属性
获取向量表中行数和维度数的(rows, dims)元组。
| 名称 | 描述 |
|---|---|
| RETURNS | A (rows, dims) pair. Tuple[int, int] |
BaseVectors.size 属性
向量大小,即 rows * dims。
| 名称 | 描述 |
|---|---|
| 返回值 | 向量大小。 int |
BaseVectors.is_full 属性
向量表是否已满且没有空槽位可供新键使用。
| 名称 | 描述 |
|---|---|
| 返回值 | 向量表是否已满。bool |
BaseVectors.get_batch 方法v3.2
高效批量获取所提供键的向量。需要使用StaticVectors中的向量进行训练。
| 名称 | 描述 |
|---|---|
keys | The keys. Iterable[Union[int, str]] |
BaseVectors.to_ops 方法
虚拟方法。实现此方法以更改嵌入矩阵,使用不同的Thinc操作。
| 名称 | 描述 |
|---|---|
ops | The Thinc ops to switch the embedding matrix to. Ops |
BaseVectors.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] |
BaseVectors.from_disk 方法
用于实现序列化的虚拟方法。实现从保存的管道中加载向量数据。
| 名称 | 描述 |
|---|---|
path | A path to a directory. Paths may be either strings or Path-like objects. Union[str,Path] |
| 返回值 | 修改后的向量对象。BaseVectors |
BaseVectors.to_bytes 方法
用于实现序列化的虚拟方法。实现该方法可将向量数据序列化为二进制字符串。
| 名称 | 描述 |
|---|---|
| 返回值 | 向量对象的序列化形式。bytes |
BaseVectors.from_bytes 方法
用于实现序列化的虚拟方法。实现从二进制字符串加载向量数据的功能。
| 名称 | 描述 |
|---|---|
data | The data to load from. bytes |
| 返回值 | 向量对象。 BaseVectors |