MAPTensor

class torchhd.MAPTensor[来源]

乘加排列

Multiplicative Binding, Representation Operators & Analogy中提出的这个模型,使用元素来自\(\{-1,1\}\)的密集双极超向量进行工作。

bind(other: MAPTensor) MAPTensor[来源]

使用元素级乘法将超向量与其他超向量绑定。

这会产生一个与两者都不相似的超向量。

绑定用于关联信息,例如,为变量赋值。

Parameters:

其他 (MAPTensor) – 其他输入超向量

Shapes:
  • 自我: \((*)\)

  • 其他: \((*)\)

  • 输出: \((*)\)

示例:

>>> a, b = torchhd.MAPTensor.random(2, 10)
>>> a
tensor([ 1., -1.,  1.,  1., -1.,  1.,  1., -1.,  1.,  1.])
>>> b
tensor([-1.,  1., -1.,  1.,  1.,  1.,  1.,  1., -1., -1.])
>>> a.bind(b)
tensor([-1., -1., -1.,  1., -1.,  1.,  1., -1., -1., -1.])
bundle(other: MAPTensor) MAPTensor[来源]

将超向量与其他向量使用元素求和进行捆绑。

这会产生一个与两者极为相似的超向量。

捆绑操作用于将信息聚合到一个单一的超级向量中。

Parameters:

其他 (MAPTensor) – 其他输入超向量

Shapes:
  • 自我: \((*)\)

  • 其他: \((*)\)

  • 输出: \((*)\)

示例:

>>> a, b = torchhd.MAPTensor.random(2, 10)
>>> a
tensor([-1., -1., -1., -1., -1.,  1., -1.,  1.,  1.,  1.])
>>> b
tensor([ 1., -1.,  1., -1., -1.,  1., -1., -1.,  1.,  1.])
>>> a.bundle(b)
tensor([ 0., -2.,  0., -2., -2.,  2., -2.,  0.,  2.,  2.])
clipping(kappa) MAPTensor[来源]

执行剪切功能,剪切下限和上限值。

Parameters:

kappa (int) – 指定裁剪函数的范围。

Shapes:
  • 自我: \((*)\)

  • 输出: \((*)\)

示例:

>>> a = torchhd.MAPTensor.random(30, 10).multibundle()
>>> a
MAP([-8.,  0.,  6.,  8.,  4., -6.,  0., -2.,  0., -4.])
>>> a.clipping(4)
MAP([-4.,  0.,  4.,  4.,  4., -4.,  0., -2.,  0., -4.])
cosine_similarity(others: MAPTensor, *, dtype=None, eps=1e-08) Tensor[来源]

与其他超向量的余弦相似度

dot_similarity(others: MAPTensor, *, dtype=None) Tensor[来源]

与其他超向量的内积

classmethod empty(num_vectors: int, dimensions: int, *, dtype=None, device=None, requires_grad=False) MAPTensor[来源]

创建一组表示空集的超向量。

当与超向量\(x\)捆绑时,结果是\(x\)

Parameters:
  • num_vectors (int) – 要生成的超向量的数量。

  • 维度 (int) – 超向量的维度。

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 None 则取决于 VSATensor。

  • device (torch.device, 可选) – 返回张量的期望设备。默认值:如果 None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。device 对于 CPU 张量类型将是 CPU,对于 CUDA 张量类型将是当前的 CUDA 设备。

  • requires_grad (bool, 可选) – 如果自动求导应该记录返回张量上的操作。默认值:False

示例:

>>> torchhd.MAPTensor.empty(3, 6)
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])
classmethod identity(num_vectors: int, dimensions: int, *, dtype=None, device=None, requires_grad=False) MAPTensor[来源]

创建一组身份超向量。

当与随机超向量 \(x\) 绑定时,结果是 \(x\)

Parameters:
  • num_vectors (int) – 要生成的超向量的数量。

  • 维度 (int) – 超向量的维度。

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 None 则取决于 VSATensor。

  • device (torch.device, 可选) – 返回张量的期望设备。默认值:如果 None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。device 对于 CPU 张量类型将是 CPU,对于 CUDA 张量类型将是当前的 CUDA 设备。

  • requires_grad (bool, 可选) – 如果自动求导应该记录返回张量上的操作。默认值:False

示例:

>>> torchhd.MAPTensor.identity(3, 6)
tensor([[1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.]])
inverse() MAPTensor[来源]

反转超向量以进行绑定。

MAP中的每个超向量都是其自身的逆,因此这将返回自身的副本。

Shapes:
  • 自我: \((*)\)

  • 输出: \((*)\)

示例:

>>> a = torchhd.MAPTensor.random(1, 10)
>>> a
tensor([[-1., -1., -1.,  1.,  1.,  1., -1.,  1., -1.,  1.]])
>>> a.inverse()
tensor([[-1., -1., -1.,  1.,  1.,  1., -1.,  1., -1.,  1.]])
multibind() MAPTensor[来源]

绑定多个超向量

multibundle() MAPTensor[来源]

捆绑多个超向量

negative() MAPTensor[来源]

对超向量进行取反以实现捆绑逆操作

Shapes:
  • 自我: \((*)\)

  • 输出: \((*)\)

示例:

>>> a = torchhd.MAPTensor.random(1, 10)
>>> a
tensor([[-1., -1.,  1.,  1.,  1., -1.,  1., -1., -1., -1.]])
>>> a.negative()
tensor([[ 1.,  1., -1., -1., -1.,  1., -1.,  1.,  1.,  1.]])
normalize() MAPTensor[来源]

归一化超向量。

归一化将所有正数项设置为+1,所有其他项设置为-1。

Shapes:
  • 自我: \((*)\)

  • 输出: \((*)\)

示例:

>>> x = torchhd.MAPTensor.random(4, 6).multibundle()
>>> x
MAPTensor([-2., -4.,  4.,  0.,  4., -2.])
>>> x.normalize()
MAPTensor([-1., -1.,  1., -1.,  1., -1.])
permute(shifts: int = 1) MAPTensor[来源]

置换超向量。

排列运算符通常用于为超向量分配顺序。

Parameters:

shifts (int, optional) – 张量元素移动的位置数。

Shapes:
  • 自我: \((*)\)

  • 输出: \((*)\)

示例:

>>> a = torchhd.MAPTensor.random(1, 10)
>>> a
tensor([[ 1.,  1.,  1., -1., -1., -1.,  1., -1., -1.,  1.]])
>>> a.permute()
tensor([[ 1.,  1.,  1.,  1., -1., -1., -1.,  1., -1., -1.]])
classmethod random(num_vectors: int, dimensions: int, *, generator=None, dtype=None, device=None, requires_grad=False) MAPTensor[来源]

创建一组随机独立的超向量。

生成的高维向量是从dimensions维超空间中均匀随机采样的。

Parameters:
  • num_vectors (int) – 要生成的超向量的数量。

  • 维度 (int) – 超向量的维度。

  • generator (torch.Generator, 可选) – 用于采样的伪随机数生成器。

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 None 则取决于 VSATensor。

  • device (torch.device, 可选) – 返回张量的期望设备。默认值:如果 None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。device 对于 CPU 张量类型将是 CPU,对于 CUDA 张量类型将是当前的 CUDA 设备。

  • requires_grad (bool, 可选) – 如果自动求导应该记录返回张量上的操作。默认值:False

示例:

>>> torchhd.MAPTensor.random(3, 6)
tensor([[-1.,  1., -1.,  1.,  1., -1.],
        [ 1., -1.,  1.,  1.,  1.,  1.],
        [-1.,  1.,  1.,  1., -1., -1.]])
>>> torchhd.MAPTensor.random(3, 6, dtype=torch.long)
tensor([[-1,  1, -1, -1,  1,  1],
        [ 1,  1, -1, -1, -1, -1],
        [-1, -1, -1,  1, -1, -1]])