Embeddings Module

The bayes_hdc.embeddings module provides encoders for transforming data into hypervectors.

RandomEncoder

class bayes_hdc.embeddings.RandomEncoder(codebook, num_features, num_values, dimensions, vsa_model_name='map')[source]

Bases: object

Encoder using random hypervectors for discrete features.

Each unique feature value is mapped to a random hypervector from a codebook. Multiple features are bundled together to form the final representation.

Parameters:
  • codebook (Array)

  • num_features (int)

  • num_values (int)

  • dimensions (int)

  • vsa_model_name (str)

codebook: Array
num_features: int
num_values: int
dimensions: int
vsa_model_name: str = 'map'
static create(num_features, num_values, dimensions=10000, vsa_model='map', key=None)[source]

Create a random encoder.

Parameters:
  • num_features (int) – Number of features to encode

  • num_values (int) – Number of possible values per feature

  • dimensions (int) – Dimensionality of hypervectors (default: 10000)

  • vsa_model (str | VSAModel) – VSA model to use (‘bsc’, ‘map’, ‘hrr’, ‘fhrr’) or VSAModel instance

  • key (Array | None) – JAX random key (default: PRNGKey(0))

Returns:

Initialized RandomEncoder

Return type:

RandomEncoder

encode(indices)[source]

Encode discrete features as hypervectors.

Parameters:

indices (Array) – Feature indices of shape (num_features,) with values in [0, num_values). Out-of-bounds indices are clamped to valid range.

Returns:

Encoded hypervector of shape (dimensions,)

Return type:

Array

encode_batch(indices)[source]

Encode a batch of samples.

Parameters:

indices (Array) – Batch of feature indices of shape (batch_size, num_features)

Returns:

Encoded hypervectors of shape (batch_size, dimensions)

Return type:

Array

__init__(codebook, num_features, num_values, dimensions, vsa_model_name='map')
Parameters:
  • codebook (Array)

  • num_features (int)

  • num_values (int)

  • dimensions (int)

  • vsa_model_name (str)

Return type:

None

Example:

from bayes_hdc import MAP, RandomEncoder
import jax

model = MAP.create(dimensions=10000)
key = jax.random.PRNGKey(42)

encoder = RandomEncoder.create(
    num_features=20,
    num_values=10,
    dimensions=10000,
    vsa_model=model,
    key=key
)

# Encode discrete features
data = jax.random.randint(key, (20,), 0, 10)
encoded = encoder.encode(data)

LevelEncoder

class bayes_hdc.embeddings.LevelEncoder(level_hvs, num_levels, dimensions, min_value, max_value, vsa_model_name='map', encoding_type='linear')[source]

Bases: object

Encoder for continuous values using level hypervectors.

Continuous values are encoded by interpolating between level hypervectors, creating a smooth representation where similar values map to similar hypervectors.

Parameters:
  • level_hvs (Array)

  • num_levels (int)

  • dimensions (int)

  • min_value (float)

  • max_value (float)

  • vsa_model_name (str)

  • encoding_type (str)

level_hvs: Array
num_levels: int
dimensions: int
min_value: float
max_value: float
vsa_model_name: str = 'map'
encoding_type: str = 'linear'
static create(num_levels=100, dimensions=10000, min_value=0.0, max_value=1.0, vsa_model='map', encoding_type='linear', key=None)[source]

Create a level encoder.

Parameters:
  • num_levels (int) – Number of levels for discretization (default: 100)

  • dimensions (int) – Dimensionality of hypervectors (default: 10000)

  • min_value (float) – Minimum value of the range (default: 0.0)

  • max_value (float) – Maximum value of the range (default: 1.0)

  • vsa_model (str | VSAModel) – VSA model to use (‘bsc’, ‘map’, ‘hrr’, ‘fhrr’)

  • encoding_type (str) – ‘linear’ or ‘circular’ (default: ‘linear’)

  • key (Array | None) – JAX random key

Returns:

Initialized LevelEncoder

Return type:

LevelEncoder

encode(value)[source]

Encode a continuous value as a hypervector.

Parameters:

value (float | Array) – Continuous value to encode (scalar or array)

Returns:

Encoded hypervector of shape (dimensions,) or batch shape + (dimensions,)

Return type:

Array

encode_batch(values)[source]

Encode a batch of continuous values.

Parameters:

values (Array) – Batch of values of shape (batch_size,) or (batch_size, num_features)

Returns:

Encoded hypervectors

Return type:

Array

__init__(level_hvs, num_levels, dimensions, min_value, max_value, vsa_model_name='map', encoding_type='linear')
Parameters:
  • level_hvs (Array)

  • num_levels (int)

  • dimensions (int)

  • min_value (float)

  • max_value (float)

  • vsa_model_name (str)

  • encoding_type (str)

Return type:

None

Example:

from bayes_hdc import LevelEncoder

encoder = LevelEncoder.create(
    num_levels=100,
    dimensions=10000,
    min_value=0.0,
    max_value=1.0,
    vsa_model=model,
    key=key
)

# Encode continuous value
encoded = encoder.encode(0.75)

ProjectionEncoder

class bayes_hdc.embeddings.ProjectionEncoder(projection_matrix, input_dim, dimensions, vsa_model_name='map')[source]

Bases: object

Encoder using random projection for high-dimensional data.

Projects high-dimensional input data into hypervector space using a random projection matrix. Useful for images, text embeddings, etc.

Parameters:
  • projection_matrix (Array)

  • input_dim (int)

  • dimensions (int)

  • vsa_model_name (str)

projection_matrix: Array
input_dim: int
dimensions: int
vsa_model_name: str = 'map'
static create(input_dim, dimensions=10000, vsa_model='map', key=None)[source]

Create a projection encoder.

Parameters:
  • input_dim (int) – Dimensionality of input data

  • dimensions (int) – Dimensionality of hypervectors (default: 10000)

  • vsa_model (str | VSAModel) – VSA model to use (‘bsc’, ‘map’, ‘hrr’, ‘fhrr’)

  • key (Array | None) – JAX random key

Returns:

Initialized ProjectionEncoder

Return type:

ProjectionEncoder

encode(x)[source]

Encode input data as a hypervector.

Parameters:

x (Array) – Input data of shape (input_dim,)

Returns:

Encoded hypervector of shape (dimensions,)

Return type:

Array

encode_batch(x)[source]

Encode a batch of inputs.

Parameters:

x (Array) – Batch of inputs of shape (batch_size, input_dim)

Returns:

Encoded hypervectors of shape (batch_size, dimensions)

Return type:

Array

__init__(projection_matrix, input_dim, dimensions, vsa_model_name='map')
Parameters:
  • projection_matrix (Array)

  • input_dim (int)

  • dimensions (int)

  • vsa_model_name (str)

Return type:

None

Example:

from bayes_hdc import ProjectionEncoder

encoder = ProjectionEncoder.create(
    input_dim=784,
    dimensions=10000,
    vsa_model=model,
    key=key
)

# Encode high-dimensional input
image = jax.random.normal(key, (784,))
encoded = encoder.encode(image)

KernelEncoder

class bayes_hdc.embeddings.KernelEncoder(omega, bias, input_dim, dimensions, gamma, vsa_model_name='map')[source]

Bases: object

Encoder using RBF kernel approximation (Random Fourier Features).

Approximates the RBF kernel k(x,y) = exp(-gamma ||x-y||^2) via random Fourier features, mapping input to a hypervector space that preserves kernel similarity.

Parameters:
  • omega (Array)

  • bias (Array)

  • input_dim (int)

  • dimensions (int)

  • gamma (float)

  • vsa_model_name (str)

omega: Array
bias: Array
input_dim: int
dimensions: int
gamma: float
vsa_model_name: str = 'map'
static create(input_dim, dimensions=10000, gamma=1.0, vsa_model='map', key=None)[source]

Create a kernel encoder.

Parameters:
  • input_dim (int) – Dimensionality of input data

  • dimensions (int) – Dimensionality of output hypervectors

  • gamma (float) – RBF kernel scale parameter (1 / 2*sigma^2)

  • vsa_model (str | VSAModel) – VSA model (‘map’, ‘hrr’, ‘fhrr’ for real-valued)

  • key (Array | None) – JAX random key

Returns:

Initialized KernelEncoder

Return type:

KernelEncoder

encode(x)[source]

Encode input using RBF kernel approximation.

Parameters:

x (Array)

Return type:

Array

encode_batch(x)[source]

Encode a batch of inputs.

Parameters:

x (Array)

Return type:

Array

__init__(omega, bias, input_dim, dimensions, gamma, vsa_model_name='map')
Parameters:
  • omega (Array)

  • bias (Array)

  • input_dim (int)

  • dimensions (int)

  • gamma (float)

  • vsa_model_name (str)

Return type:

None

GraphEncoder

class bayes_hdc.embeddings.GraphEncoder(node_embeddings, num_nodes, dimensions, vsa_model_name='map')[source]

Bases: object

Encoder for graph structures (nodes and edges).

Encodes a graph by assigning random hypervectors to nodes and bundling bound node pairs for edges. Graph = bundle of edge HVs.

Parameters:
  • node_embeddings (Array)

  • num_nodes (int)

  • dimensions (int)

  • vsa_model_name (str)

node_embeddings: Array
num_nodes: int
dimensions: int
vsa_model_name: str = 'map'
static create(num_nodes, dimensions=10000, vsa_model='map', key=None)[source]

Create a graph encoder.

Parameters:
  • num_nodes (int) – Maximum number of nodes

  • dimensions (int) – Hypervector dimensionality

  • vsa_model (str | VSAModel) – VSA model for real-valued graphs

  • key (Array | None) – JAX random key

Returns:

Initialized GraphEncoder

Return type:

GraphEncoder

encode_edges(edges)[source]

Encode graph as bundle of bound edge pairs.

Parameters:

edges (Array) – Array of shape (num_edges, 2) with node indices in [0, num_nodes). Out-of-bounds indices are clamped to valid range.

Returns:

Graph hypervector of shape (dimensions,)

Return type:

Array

__init__(node_embeddings, num_nodes, dimensions, vsa_model_name='map')
Parameters:
  • node_embeddings (Array)

  • num_nodes (int)

  • dimensions (int)

  • vsa_model_name (str)

Return type:

None