BSBCTensor
- class torchhd.BSBCTensor[source]
Binary Sparse Block Codes (B-SBC)
Proposed in High-dimensional computing with sparse vectors, this model works with sparse vector segments.
Because the vectors are sparse and have a fixed magnitude, we only represent the index of the non-zero value.
- classmethod __torch_function__(func, types, args=(), kwargs=None)[source]
This __torch_function__ implementation wraps subclasses such that methods called on subclasses return a subclass instance instead of a
torch.Tensor
instance.One corollary to this is that you need coverage for torch.Tensor methods if implementing __torch_function__ for subclasses.
We recommend always calling
super().__torch_function__
as the base case when doing the above.While not mandatory, we recommend making __torch_function__ a classmethod.
- bind(other: BSBCTensor) BSBCTensor [source]
Bind the hypervector with other using circular convolution.
This produces a hypervector dissimilar to both.
Binding is used to associate information, for instance, to assign values to variables.
- Parameters:
other (BSBCTensor) – other input hypervector
- Shapes:
Self: \((*)\)
Other: \((*)\)
Output: \((*)\)
Examples:
>>> a, b = torchhd.BSBCTensor.random(2, 10, block_size=64) >>> a BSBCTensor([18, 55, 40, 62, 39, 26, 35, 24, 49, 41]) >>> b BSBCTensor([46, 36, 21, 23, 25, 12, 29, 53, 54, 41]) >>> a.bind(b) BSBCTensor([ 0, 27, 61, 21, 0, 38, 0, 13, 39, 18])
- bundle(other: BSBCTensor, *, generator=None) BSBCTensor [source]
Bundle the hypervector with other using majority voting.
This produces a hypervector maximally similar to both.
The bundling operation is used to aggregate information into a single hypervector.
Ties in the majority vote are broken at random. For a deterministic result provide a random number generator.
- Parameters:
other (BSC) – other input hypervector
generator (
torch.Generator
, optional) – a pseudorandom number generator for sampling.
- Shapes:
Self: \((*)\)
Other: \((*)\)
Output: \((*)\)
Examples:
>>> a, b = torchhd.BSBCTensor.random(2, 10, block_size=64) >>> a BSBCTensor([32, 26, 22, 22, 34, 30, 2, 2, 40, 43]) >>> b BSBCTensor([33, 27, 39, 54, 27, 60, 60, 4, 24, 5]) >>> a.bundle(b) BSBCTensor([32, 26, 39, 54, 27, 60, 2, 4, 40, 5])
- cosine_similarity(others: BSBCTensor, *, dtype=None) Tensor [source]
Cosine similarity with other hypervectors
- dot_similarity(others: BSBCTensor, *, dtype=None) Tensor [source]
Inner product with other hypervectors
- classmethod empty(num_vectors: int, num_blocks: int, *, block_size: int, generator=None, dtype=torch.int64, device=None, requires_grad=False) BSBCTensor [source]
Creates a set of hypervectors representing empty sets.
When bundled with a hypervector \(x\), the result is \(x\). Because of the low precession of the SBC model an empty set cannot be explicitly represented, therefore the returned hypervectors are identical to random-hypervectors.
- Parameters:
num_vectors (int) – the number of hypervectors to generate.
num_blocks (int) – the number of sparse blocks of the hypervectors.
block_size (int) – the number of elements per block which controls the angular granularity.
generator (
torch.Generator
, optional) – a pseudorandom number generator for sampling.dtype (
torch.dtype
, optional) – the desired data type of returned tensor. Default:int64
.device (
torch.device
, optional) – the desired device of returned tensor. Default: ifNone
, 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
.
Examples:
>>> torchhd.BSBCTensor.empty(3, 6, block_size=64) BSBCTensor([[54, 3, 22, 27, 41, 21], [17, 31, 55, 3, 44, 52], [42, 37, 60, 54, 13, 41]])
- classmethod identity(num_vectors: int, num_blocks: int, *, block_size: int, dtype=torch.int64, device=None, requires_grad=False) BSBCTensor [source]
Creates a set of identity hypervectors.
When bound with a random-hypervector \(x\), the result is \(x\).
- Parameters:
num_vectors (int) – the number of hypervectors to generate.
num_blocks (int) – the number of sparse blocks of the hypervectors.
block_size (int) – the number of elements per block which controls the angular granularity.
dtype (
torch.dtype
, optional) – the desired data type of returned tensor. Default: ifint64
depends on VSATensor.device (
torch.device
, optional) – the desired device of returned tensor. Default: ifNone
, 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
.
Examples:
>>> torchhd.BSBCTensor.identity(3, 6, block_size=64) BSBCTensor([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]])
- inverse() BSBCTensor [source]
Invert the hypervector for binding.
- Shapes:
Self: \((*)\)
Output: \((*)\)
Examples:
>>> a = torchhd.BSBCTensor.random(1, 10, block_size=64) >>> a BSBCTensor([[ 5, 30, 15, 43, 19, 36, 4, 14, 57, 34]]) >>> a.inverse() BSBCTensor([[59, 34, 49, 21, 45, 28, 60, 50, 7, 30]])
- multibind() BSBCTensor [source]
Bind multiple hypervectors
- multibundle() BSBCTensor [source]
Bundle multiple hypervectors
- permute(shifts: int = 1) BSBCTensor [source]
Permute the hypervector.
The permutation operator is commonly used to assign an order to hypervectors.
- Parameters:
shifts (int, optional) – The number of places by which the elements of the tensor are shifted.
- Shapes:
Self: \((*)\)
Output: \((*)\)
Examples:
>>> a = torchhd.BSBCTensor.random(1, 10, block_size=64) >>> a BSBCTensor([[33, 24, 1, 36, 2, 57, 11, 59, 33, 3]]) >>> a.permute(4) BSBCTensor([[11, 59, 33, 3, 33, 24, 1, 36, 2, 57]])
- classmethod random(num_vectors: int, num_blocks: int, *, block_size: int, generator=None, dtype=torch.int64, device=None, requires_grad=False) BSBCTensor [source]
Creates a set of random independent hypervectors.
The resulting hypervectors sample uniformly random integers between 0 and
block_size
.- Parameters:
num_vectors (int) – the number of hypervectors to generate.
num_blocks (int) – the dimensionality of the hypervectors.
block_size (int) – the number of elements per block which controls the angular granularity.
generator (
torch.Generator
, optional) – a pseudorandom number generator for sampling.dtype (
torch.dtype
, optional) – the desired data type of returned tensor. Default:int64
.device (
torch.device
, optional) – the desired device of returned tensor. Default: ifNone
, 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
.
Examples:
>>> torchhd.BSBCTensor.random(3, 6, block_size=64) BSBCTensor([[ 7, 1, 39, 8, 55, 22], [51, 38, 59, 45, 13, 29], [19, 26, 30, 5, 15, 51]]) >>> torchhd.BSBCTensor.random(3, 6, block_size=128, dtype=torch.float32) BSBCTensor([[116., 25., 100., 10., 21., 86.], [ 69., 49., 2., 56., 78., 70.], [ 77., 47., 37., 106., 8., 30.]])