VTBTensor

class torchhd.VTBTensor[来源]

向量派生变换绑定

Vector-Derived Transformation Binding: An Improved Binding Operation for Deep Symbol-Like Processing in Neural Networks中提出,作为对全息减少表示(HRR)的改进,该模型也使用实值超向量。

bind(other: VTBTensor) VTBTensor[来源]

使用提出的绑定方法将超向量与其他绑定。

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

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

Parameters:

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

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

  • 其他: \((*)\)

  • 输出: \((*)\)

示例:

>>> a, b = torchhd.VTBTensor.random(2, 9)
>>> a
VTBTensor([-0.173, -0.731,  0.026,  0.110,  0.326,  0.147,  0.105,  0.407, -0.343])
>>> b
VTBTensor([ 0.170,  0.049, -0.141,  0.873,  0.186,  0.262, -0.108,  0.171, 0.208])
>>> a.bind(b)
VTBTensor([-0.119, -0.485, -0.175,  0.024,  0.338,  0.129,  0.149,  0.133, -0.023])

>>> a, b = torchhd.VTBTensor.random(2, 9, dtype=torch.float64)
>>> a
VTBTensor([-0.168,  0.244, -0.418,  0.236,  0.382, -0.369, -0.006, -0.621, 0.123], dtype=torch.float64)
>>> b
VTBTensor([-0.284,  0.464,  0.021, -0.459, -0.414, -0.346,  0.241, -0.083, 0.372], dtype=torch.float64)
>>> a.bind(b)
VTBTensor([ 0.263,  0.209, -0.374,  0.177, -0.240, -0.194, -0.491,  0.376, 0.166], dtype=torch.float64)
bundle(other: VTBTensor) VTBTensor[来源]

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

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

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

Parameters:

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

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

  • 其他: \((*)\)

  • 输出: \((*)\)

示例:

>>> a, b = torchhd.VTBTensor.random(2, 9)
>>> a
VTBTensor([ 0.276,  0.206, -0.383,  0.553,  0.050,  0.334,  0.336, -0.067, 0.445])
>>> b
VTBTensor([-0.315,  0.626, -0.460, -0.345,  0.194,  0.121, -0.234, -0.207, -0.170])
>>> a.bundle(b)
VTBTensor([-0.039,  0.832, -0.842,  0.209,  0.244,  0.455,  0.101, -0.274, 0.275])

>>> a, b = torchhd.VTBTensor.random(2, 9, dtype=torch.float64)
>>> a
VTBTensor([-0.063,  0.154, -0.061, -0.229, -0.880,  0.365,  0.009, -0.057, -0.076], dtype=torch.float64)
>>> b
VTBTensor([-0.259, -0.158, -0.167, -0.599,  0.171,  0.033, -0.406,  0.361, -0.443], dtype=torch.float64)
>>> a.bundle(b)
VTBTensor([-0.321, -0.003, -0.228, -0.827, -0.709,  0.398, -0.397,  0.304, -0.519], dtype=torch.float64)
cosine_similarity(others: VTBTensor, *, eps=1e-08) Tensor[来源]

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

dot_similarity(others: VTBTensor) Tensor[来源]

与其他超向量的内积

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

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

当与随机超向量\(x\)捆绑时,结果是\(x\)。 VTB模型的空向量只是一组0值。

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

  • dimensions (int) – 超向量的维度,必须具有整数平方根。

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 Nonetorch.get_default_dtype()

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

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

示例:

>>> torchhd.VTBTensor.empty(3, 9)
VTBTensor([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> torchhd.VTBTensor.empty(3, 9, dtype=torch.float64)
    VTBTensor([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=torch.float64)
classmethod identity(num_vectors: int, dimensions: int, *, dtype=None, device=None, requires_grad=False) VTBTensor[来源]

创建一组身份超向量。

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

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

  • dimensions (int) – 超向量的维度,必须具有整数平方根。

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 Nonetorch.get_default_dtype()

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

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

示例:

>>> torchhd.VTBTensor.identity(3, 9)
    VTBTensor([[0.577, 0.000, 0.000, 0.000, 0.577, 0.000, 0.000, 0.000, 0.577],
            [0.577, 0.000, 0.000, 0.000, 0.577, 0.000, 0.000, 0.000, 0.577],
            [0.577, 0.000, 0.000, 0.000, 0.577, 0.000, 0.000, 0.000, 0.577]])
>>> torchhd.VTBTensor.identity(3, 9, dtype=torch.float64)
    VTBTensor([[0.577, 0.000, 0.000, 0.000, 0.577, 0.000, 0.000, 0.000, 0.577],
            [0.577, 0.000, 0.000, 0.000, 0.577, 0.000, 0.000, 0.000, 0.577],
            [0.577, 0.000, 0.000, 0.000, 0.577, 0.000, 0.000, 0.000, 0.577]],
            dtype=torch.float64)
inverse() VTBTensor[来源]

用于绑定的超向量反转。

对于VTB,逆运算是一个近似操作。

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

  • 输出: \((*)\)

示例:

>>> a = torchhd.VTBTensor.random(1, 9)

>>> a
VTBTensor([[-0.117,  0.322, -0.747, -0.196, -0.059,  0.103, -0.099,  0.389, -0.332]])
>>> a.inverse()
VTBTensor([[-0.117, -0.196, -0.099,  0.322, -0.059,  0.389, -0.747,  0.103, -0.332]])

>>> a = torchhd.VTBTensor.random(1, 9, dtype=torch.float64)
>>> a
VTBTensor([[-0.307, -0.019, -0.105,  0.174, -0.316,  0.366, -0.675, -0.147, 0.391]], dtype=torch.float64)
>>> a.inverse()
VTBTensor([[-0.307,  0.174, -0.675, -0.019, -0.316, -0.147, -0.105,  0.366, 0.391]], dtype=torch.float64)
multibundle() VTBTensor[来源]

捆绑多个超向量

negative() VTBTensor[来源]

对捆绑逆的超向量进行取反。

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

  • 输出: \((*)\)

示例:

>>> a = torchhd.VTBTensor.random(1, 9)
>>> a
VTBTensor([[ 0.1692, -0.0420,  0.1477,  0.4790, -0.3863, -0.2593,  0.3213, -0.1168, -0.6205]])
>>> a.negative()
VTBTensor([[-0.1692,  0.0420, -0.1477, -0.4790,  0.3863,  0.2593, -0.3213, 0.1168,  0.6205]])

>>> a = torchhd.VTBTensor.random(1, 9, dtype=torch.float64)
>>> a
VTBTensor([[ 0.2905,  0.2212,  0.2049,  0.1753, -0.5277, -0.6216, -0.2118, -0.2753, -0.0915]], dtype=torch.float64)
>>> a.negative()
VTBTensor([[-0.2905, -0.2212, -0.2049, -0.1753,  0.5277,  0.6216,  0.2118, 0.2753,  0.0915]], dtype=torch.float64)
normalize() VTBTensor[来源]

归一化超向量。

归一化保留了超向量的方向,但使其成为单位范数。 这意味着它被映射到单位球面上的最近点。

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

  • 输出: \((*)\)

示例:

>>> x = torchhd.VTBTensor.random(4, 9).multibundle()
>>> x
VTBTensor([-0.3706,  0.4308, -1.3276,  0.1773, -0.3008, -0.9385, -0.4677,
0.5111, -0.2048])
>>> x.normalize()
VTBTensor([-0.1950,  0.2267, -0.6987,  0.0933, -0.1583, -0.4939, -0.2462,
0.2690, -0.1078])
permute(shifts: int = 1) VTBTensor[来源]

置换超向量。

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

Parameters:

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

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

  • 输出: \((*)\)

示例:

>>> a = torchhd.VTBTensor.random(1, 9)
>>> a
VTBTensor([[ 0.1913, -0.5592,  0.3474, -0.1385,  0.5625,  0.0223, -0.1456, 0.2509, -0.3313]])
>>> a.permute()
VTBTensor([[-0.3313,  0.1913, -0.5592,  0.3474, -0.1385,  0.5625,  0.0223, -0.1456,  0.2509]])

>>> a = torchhd.VTBTensor.random(1, 9, dtype=torch.float64)
>>> a
VTBTensor([[ 0.6705,  0.1316,  0.2595,  0.1193, -0.4884, -0.4052,  0.0730, -0.2022,  0.0513]], dtype=torch.float64)
>>> a.permute()
VTBTensor([[ 0.0513,  0.6705,  0.1316,  0.2595,  0.1193, -0.4884, -0.4052, 0.0730, -0.2022]], dtype=torch.float64)
classmethod random(num_vectors: int, dimensions: int, *, generator=None, dtype=None, device=None, requires_grad=False) VTBTensor[来源]

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

生成的高维向量是从(维度 - 1)单位球体中均匀随机采样的。

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

  • dimensions (int) – 超向量的维度,必须具有整数平方根。

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

  • dtype (torch.dtype, 可选) – 返回张量的期望数据类型。默认值:如果 Nonetorch.get_default_dtype()

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

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

示例:

>>> torchhd.VTBTensor.random(3, 9)
    VTBTensor([[-0.420, -0.069,  0.014, -0.226,  0.399,  0.066, -0.606, -0.184, 0.451],
            [-0.074,  0.270, -0.044,  0.691,  0.456, -0.144, -0.252, -0.170, -0.349],
            [ 0.197,  0.347,  0.596, -0.323, -0.397, -0.173, -0.317,  0.217, -0.215]])
>>> torchhd.VTBTensor.random(3, 9, dtype=torch.float64)
    VTBTensor([[ 0.308,  0.055, -0.471, -0.587,  0.054,  0.371, -0.038,  0.432, 0.080],
            [-0.267, -0.510, -0.301,  0.435,  0.244,  0.122, -0.094,  0.552, 0.033],
            [-0.571,  0.540,  0.135, -0.112, -0.048, -0.516, -0.019, -0.226, 0.177]], dtype=torch.float64)