The components Submodule
pyhdc.components exposes the individual building blocks that underpin
every encoding. Most users never need this module: the encoding classes
assemble the right components automatically. It is useful when:
You are writing a custom encoding subclass
You want to apply a specific operation (e.g.,
remap_to_unit) without a full encoding contextYou are debugging or testing individual operations in isolation
Submodule layout
pyhdc.components
├── binding : all binding functions
├── bundling : all bundling functions
├── similarity : all similarity functions + remap_to_unit
├── elements : element generator functions (how random values are drawn)
├── thinning : thinning functions (post-process sparse binary vectors)
└── input_formatting : internal normalisation utilities
The EncodingSpec wiring
When you define a custom encoding by subclassing Encoding and
implementing _get_encoding_spec(), you return an EncodingSpec that
names the component functions to use:
from pyhdc.encodings.base import Encoding, EncodingSpec
from pyhdc.components.binding import ElementMultiplication
from pyhdc.components.bundling import ElementAdditionCut
from pyhdc.components.similarity import CosineSimilarity
from pyhdc.components.elements import UniformBipolar
from pyhdc.components.thinning import NoThin
import numpy as np
class MyEncoding(Encoding):
def _get_encoding_spec(self) -> EncodingSpec:
return EncodingSpec(
dtype=np.float32,
element_generator=UniformBipolar,
similarity_fn=CosineSimilarity,
bundling_fn=ElementAdditionCut,
thinning_fn=NoThin,
binding_fn=ElementMultiplication,
unbinding_fn=ElementMultiplication, # self-inverse
generator_output_type="floats",
)
elements submodule
Element generators control how individual hypervector values are drawn.
Function |
Description |
|---|---|
|
Uniform random from {-1, +1} (Bernoulli p=0.5 then ×2−1) |
|
Uniform random in [0, 2π] |
|
Normal distribution N(0, 1) |
|
Bernoulli(p=0.5) → {0, 1} |
|
Bernoulli(p=0.5) → {-1, +1} (note: typo in source; “Biploar”) |
|
k-sparse binary: exactly k elements are 1, rest are 0 |
|
Per-segment sparse binary: k ones placed uniformly within each segment |
thinning submodule
Thinning operations post-process a bundled binary hypervector to reduce density.
Function |
Description |
|---|---|
|
No-op; returns the input unchanged. Used by encodings that do not thin. |
similarity submodule
The similarity module exports the four metric functions and the remap utility:
CosineSimilarity(*hvs): cosine similarityHammingDistance(*hvs): normalised Hamming, output in [-1, 1]Overlap(*hvs): normalised overlap, output in [-1, 1]AngleDistance(*hvs): angle-based distance, output in [-1, 1]remap_to_unit(sim): maps [-1, 1] → [0, 1]
Each function accepts one or two arguments in the same calling conventions
as the Encoding.similarity() method.
input_formatting submodule
Internal utilities used by encoding methods to normalise inputs. These are considered private API and may change between releases:
_extract_data(hv): extract the raw array from a Hypervector or pass through if already an array_normalize_inputs(*hvs): validate and normalise a sequence of inputs_detect_batch_structure(*hvs): determine whether inputs are 1-D or 2-D and which batched calling convention applies