Hopfield

class torchhd.memory.Hopfield(vector_dim: int, kappa: int | None = None, dtype=None, device=None, requires_grad=False)[source]

Classical Hopfield network

Parameters:
  • vector_dim (int) – The dimensionality of the vectors in the memory.

  • 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:
  • Memory: \((d, d)\)

Examples::
>>> items = torchhd.random(6, 512)
>>> hopfield = torchhd.memory.Hopfield(512)
>>> hopfield.write(items)
>>> read = hopfield.read(items).sign()
>>> torchhd.cosine_similarity(read, items)
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) Tensor[source]

Read value from Hopfield network at key most similar to the query.

Parameters:

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

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

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

write(items: Tensor) Tensor[source]

Write items to Hopfield Memory.

Parameters:

items (Tensor) – The item vectors to write to memory.

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