SparseDistributed

class torchhd.memory.SparseDistributed(memory_size: int, key_dim: int, value_dim: int, p: float = 0.000368, kappa: int | None = None, dtype=None, device=None, requires_grad=False)[source]

Sparse Distributed Memory

The Sparse Distributed Memory (SDM) is specified by its (typically random) keys and their values.

Parameters:
  • memory_size (int) – The number of memory key-value pairs.

  • key_dim (int) – The dimensionality of the key vectors.

  • value_dim (int) – The dimensionality of the value vectors.

  • p (float, optional) – The expected fraction of memory address that will contain any value. Default: 0.000368.

  • kappa (int, optional) – The maximum count for each memory cell, values are clipped between [-kappa, kappa]. Default: no clipping.

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None depends on VSATensor.

  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Shapes:
  • Keys: \((n, a)\)

  • Values: \((n, c)\)

Examples::
>>> keys = torchhd.random(6, 512)
>>> sdm = torchhd.memory.SparseDistributed(100000, 512, 512)
>>> # use as associative memory
>>> sdm.write(keys, keys)
>>> read = sdm.read(keys).sign()
>>> torchhd.cosine_similarity(read, keys)
tensor([[ 1.0000,  0.0156, -0.0039, -0.0742,  0.0000, -0.0195],
        [ 0.0156,  1.0000, -0.0352, -0.0586,  0.0000, -0.0039],
        [-0.0039, -0.0352,  1.0000,  0.0156,  0.0820, -0.0234],
        [-0.0742, -0.0586,  0.0156,  1.0000, -0.0039,  0.0000],
        [ 0.0000,  0.0000,  0.0820, -0.0039,  1.0000,  0.0195],
        [-0.0195, -0.0039, -0.0234,  0.0000,  0.0195,  1.0000]])
read(query: Tensor) VSATensor[source]

Read value from Sparse Distributed Memory whose key is most similar to the query.

Parameters:

query (Tensor) – The query vector for the memory lookup.

Shapes:
  • Query: \((*, d)\)

  • Result: \((*, d)\)

write(keys: Tensor, values: Tensor) None[source]

Write value to Sparse Distributed Memory at address.

Parameters:
  • address (Tensor) – The address vector for the write to memory.

  • value (Tensor) – The value vector written to memory.

Shapes:
  • Address: \((*, d)\)

  • Value: \((*, d)\)