BundleSequence

class torchhd.structures.BundleSequence(dimensions: int, vsa: Literal['BSC', 'MAP', 'HRR', 'FHRR', 'BSBC', 'VTB', 'MCR', 'CGR'] = 'MAP', *, device=None, dtype=None)[source]
class torchhd.structures.BundleSequence(input: VSATensor, *, size=0)

Hypervector bundling-based sequence data structure

Creates an empty sequence of dim dimensions or from an input tensor.

Parameters:
  • dimensions (int) – number of dimensions of the sequence.

  • vsa – (VSAOptions, optional): specifies the hypervector type and operations used (Default: "MAP").

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).

  • 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.

  • input (VSATensor) – tensor representing a sequence.

  • size (int, optional) – the length of the sequence provided as input. Default: 0.

Examples:

>>> S = structures.BundleSequence(10000)

>>> letters = list(string.ascii_lowercase)
>>> letters_hv = torchhd.random(len(letters), 10000)
>>> S = structures.BundleSequence(letters_hv[0], size=1)
__getitem__(index: int) VSATensor[source]

Gets the approximate value from given index.

Parameters:

index (int) – Index of the value in the sequence.

Examples:

>>> S[0]
tensor([ 1., -1.,  1.,  ..., -1.,  1., -1.])
__len__() int[source]

Returns the length of the sequence.

Examples:

>>> len(S)
0
append(input: VSATensor) None[source]

Appends the input tensor to the right of the sequence.

Parameters:

input (VSATensor) – Hypervector to append to the sequence.

Examples:

>>> letters = list(string.ascii_lowercase)
>>> letters_hv = torchhd.random(len(letters), 10000)
>>> S.append(letters_hv[0])
appendleft(input: VSATensor) None[source]

Appends the input tensor to the left of the sequence.

Parameters:

input (VSATensor) – Hypervector to append to the right of the sequence.

Examples:

>>> S.appendleft(letters_hv[1])
clear() None[source]

Empties the sequence.

Examples:

>>> S.clear()
concat(seq: BundleSequence) BundleSequence[source]

Concatenates the current sequence with the given one.

Parameters:

seq (Sequence) – Sequence to be concatenated with the current one.

Examples:

>>> S1 = structures.BundleSequence(dimensions=10000)
>>> S2 = S.concat(S1)
classmethod from_tensor(input: VSATensor)[source]

Creates a sequence from hypervectors.

See: bundle_sequence().

Parameters:

input (VSATensor) – Tensor containing hypervectors that form the sequence.

Examples::
>>> letters_hv = torchhd.random(len(letters), 10000)
>>> S = structures.BundleSequence.from_tensor(letters_hv)
pop(input: VSATensor) None[source]

Pops the input tensor from the right of the sequence.

Parameters:

input (VSATensor) – Hypervector to pop from the sequence.

Examples:

>>> S.pop(letters_hv[0])
popleft(input: VSATensor) None[source]

Pops the input tensor from the left of the sequence.

Parameters:

input (VSATensor) – Hypervector to pop left from the sequence.

Examples:

>>> S.popleft(letters_hv[1])
replace(index: int, old: VSATensor, new: VSATensor) None[source]

Replace the old hypervector value from the given index, for the new hypervector value.

Parameters:
  • index (int) – Index from the sequence to replace its value.

  • old (VSATensor) – Old value hypervector.

  • new (VSATensor) – New value hypervector.

Examples:

>>> S.replace(0, letters_hv[0], letters_hv[1])