Configuration dataclass for HRR systems.
@dataclass
class HRRConfig(MemoryConfig):
"""Configuration for HRR system.
Attributes
----------
dimension : int
Dimensionality of vectors (default: 1024)
normalize : bool
Whether to normalize vectors after operations (default: True)
cleanup_threshold : float
Similarity threshold for cleanup memory (default: 0.3)
storage_method : str
Method for storing vectors: "real" or "complex" (default: "real")
seed : Optional[int]
Random seed for reproducibility (default: None)
"""Example:
config = HRRConfig(
dimension=2048,
normalize=True,
cleanup_threshold=0.25,
storage_method="complex",
seed=42
)Main HRR implementation class.
class HRR(CognitiveMemory):
"""Holographic Reduced Representation system.
Parameters
----------
config : HRRConfig
Configuration object
Attributes
----------
dimension : int
Vector dimensionality
normalize : bool
Whether normalization is enabled
storage_method : str
Storage method in use
"""def bind(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
"""Bind two vectors using circular convolution.
Parameters
----------
a : np.ndarray
First vector (shape: [dimension,])
b : np.ndarray
Second vector (shape: [dimension,])
Returns
-------
np.ndarray
Bound vector (shape: [dimension,])
Examples
--------
>>> hrr = create_hrr(dimension=1024)
>>> role = hrr.generate_vector()
>>> filler = hrr.generate_vector()
>>> binding = hrr.bind(role, filler)
"""def unbind(self, c: np.ndarray, a: np.ndarray) -> np.ndarray:
"""Unbind vectors using circular correlation.
Parameters
----------
c : np.ndarray
Composite vector (shape: [dimension,])
a : np.ndarray
Known vector to unbind with (shape: [dimension,])
Returns
-------
np.ndarray
Retrieved vector (shape: [dimension,])
Examples
--------
>>> retrieved = hrr.unbind(binding, role)
>>> similarity = hrr.similarity(retrieved, filler)
"""def bundle(self, vectors: List[np.ndarray],
weights: Optional[List[float]] = None) -> np.ndarray:
"""Bundle multiple vectors by superposition.
Parameters
----------
vectors : List[np.ndarray]
List of vectors to bundle
weights : Optional[List[float]]
Optional weights for each vector
Returns
-------
np.ndarray
Bundled vector (shape: [dimension,])
Examples
--------
>>> bundle = hrr.bundle([vec1, vec2, vec3])
>>> weighted = hrr.bundle([vec1, vec2], weights=[0.7, 0.3])
"""def similarity(self, a: np.ndarray, b: np.ndarray) -> float:
"""Compute cosine similarity between vectors.
Parameters
----------
a : np.ndarray
First vector
b : np.ndarray
Second vector
Returns
-------
float
Cosine similarity in range [-1, 1]
"""def generate_vector(self, method: str = "random") -> np.ndarray:
"""Generate a new vector.
Parameters
----------
method : str
Generation method: "random" or "unitary"
Returns
-------
np.ndarray
Generated vector (shape: [dimension,])
"""def store(self, key: np.ndarray, value: np.ndarray) -> None:
"""Store key-value pair in memory.
Parameters
----------
key : np.ndarray
Key vector
value : np.ndarray
Value vector
"""def recall(self, key: np.ndarray) -> Optional[np.ndarray]:
"""Recall value associated with key.
Parameters
----------
key : np.ndarray
Query key
Returns
-------
Optional[np.ndarray]
Retrieved value or None
"""Efficient circular convolution implementation.
class CircularConvolution:
"""Circular convolution operations for HRR."""@staticmethod
def convolve(a: np.ndarray, b: np.ndarray,
method: str = "auto") -> np.ndarray:
"""Perform circular convolution.
Parameters
----------
a : np.ndarray
First vector
b : np.ndarray
Second vector
method : str
Method: "direct", "fft", or "auto"
Returns
-------
np.ndarray
Convolution result
"""@staticmethod
def correlate(a: np.ndarray, b: np.ndarray,
method: str = "auto") -> np.ndarray:
"""Perform circular correlation.
Parameters
----------
a : np.ndarray
First vector
b : np.ndarray
Second vector
method : str
Method: "direct", "fft", or "auto"
Returns
-------
np.ndarray
Correlation result
"""Additional vector operations for HRR.
class VectorOperations:
"""Utility operations for HRR vectors."""@staticmethod
def normalize(vector: np.ndarray) -> np.ndarray:
"""Normalize vector to unit length.
Parameters
----------
vector : np.ndarray
Input vector
Returns
-------
np.ndarray
Normalized vector
"""@staticmethod
def make_unitary(vector: np.ndarray) -> np.ndarray:
"""Convert vector to unitary form.
Parameters
----------
vector : np.ndarray
Input vector
Returns
-------
np.ndarray
Unitary vector
"""@staticmethod
def involution(vector: np.ndarray) -> np.ndarray:
"""Compute involution (pseudo-inverse) of vector.
Parameters
----------
vector : np.ndarray
Input vector
Returns
-------
np.ndarray
Involution of vector
"""Configuration for cleanup memory.
@dataclass
class CleanupMemoryConfig:
"""Configuration for cleanup memory.
Attributes
----------
threshold : float
Similarity threshold for cleanup (default: 0.3)
method : str
Similarity method: "cosine", "dot", "euclidean" (default: "cosine")
"""Item memory for cleaning up noisy vectors.
class CleanupMemory:
"""Memory for mapping noisy vectors to clean items.
Parameters
----------
config : CleanupMemoryConfig
Configuration object
dimension : int
Vector dimensionality
"""def add_item(self, name: str, vector: np.ndarray) -> None:
"""Add an item to cleanup memory.
Parameters
----------
name : str
Item identifier
vector : np.ndarray
Item vector
Examples
--------
>>> cleanup = CleanupMemory(config, dimension=1024)
>>> cleanup.add_item("cat", cat_vector)
>>> cleanup.add_item("dog", dog_vector)
"""def cleanup(self, vector: np.ndarray) -> Tuple[str, np.ndarray, float]:
"""Clean up noisy vector to nearest item.
Parameters
----------
vector : np.ndarray
Noisy input vector
Returns
-------
Tuple[str, np.ndarray, float]
(item_name, clean_vector, confidence)
Examples
--------
>>> name, clean_vec, conf = cleanup.cleanup(noisy_vector)
>>> print(f"Cleaned to {name} with confidence {conf:.3f}")
"""def find_closest(self, vector: np.ndarray,
k: int = 1) -> List[Tuple[str, float]]:
"""Find k closest items to vector.
Parameters
----------
vector : np.ndarray
Query vector
k : int
Number of items to return
Returns
-------
List[Tuple[str, float]]
List of (item_name, similarity) pairs
"""def has_item(self, name: str) -> bool:
"""Check if item exists in memory.
Parameters
----------
name : str
Item name
Returns
-------
bool
True if item exists
"""def get_vector(self, name: str) -> np.ndarray:
"""Get vector for named item.
Parameters
----------
name : str
Item name
Returns
-------
np.ndarray
Item vector
Raises
------
KeyError
If item not found
"""Encode role-filler structures.
class RoleFillerEncoder:
"""Encoder for role-filler structures.
Parameters
----------
hrr : HRR
HRR system to use for encoding
"""def encode_pair(self, role: np.ndarray,
filler: np.ndarray) -> np.ndarray:
"""Encode a single role-filler pair.
Parameters
----------
role : np.ndarray
Role vector
filler : np.ndarray
Filler vector
Returns
-------
np.ndarray
Bound role-filler pair
"""def encode_structure(self,
role_filler_pairs: Dict[str, np.ndarray]) -> np.ndarray:
"""Encode complete role-filler structure.
Parameters
----------
role_filler_pairs : Dict[str, np.ndarray]
Dictionary mapping role names to filler vectors
Returns
-------
np.ndarray
Encoded structure
Examples
--------
>>> structure = encoder.encode_structure({
... "agent": john_vector,
... "action": loves_vector,
... "patient": mary_vector
... })
"""def decode_filler(self, structure: np.ndarray,
role: np.ndarray) -> np.ndarray:
"""Extract filler for given role.
Parameters
----------
structure : np.ndarray
Encoded structure
role : np.ndarray
Query role
Returns
-------
np.ndarray
Retrieved filler
"""Encode sequences using HRR.
class SequenceEncoder:
"""Encoder for sequential data.
Parameters
----------
hrr : HRR
HRR system to use
"""def encode_sequence(self, items: List[np.ndarray],
method: str = "position") -> np.ndarray:
"""Encode ordered sequence of items.
Parameters
----------
items : List[np.ndarray]
List of item vectors
method : str
Encoding method: "position" or "chaining"
Returns
-------
np.ndarray
Encoded sequence
Examples
--------
>>> seq = encoder.encode_sequence([a, b, c], method="position")
"""def decode_position(self, sequence: np.ndarray,
position: int) -> np.ndarray:
"""Decode item at specific position.
Parameters
----------
sequence : np.ndarray
Encoded sequence
position : int
Position to decode (0-indexed)
Returns
-------
np.ndarray
Retrieved item
"""def generate_position_vectors(self,
n_positions: int) -> List[np.ndarray]:
"""Generate position vectors for encoding.
Parameters
----------
n_positions : int
Number of positions needed
Returns
-------
List[np.ndarray]
List of position vectors
"""Encode hierarchical structures.
class HierarchicalEncoder:
"""Encoder for tree and hierarchical structures.
Parameters
----------
hrr : HRR
HRR system to use
"""def encode_tree(self, tree: Dict[str, Any]) -> np.ndarray:
"""Encode tree structure recursively.
Parameters
----------
tree : Dict[str, Any]
Tree structure as nested dictionary
Returns
-------
np.ndarray
Encoded tree
Examples
--------
>>> tree = {
... "value": "root",
... "left": {"value": "a"},
... "right": {"value": "b"}
... }
>>> encoded = encoder.encode_tree(tree)
"""def decode_subtree(self, encoding: np.ndarray,
path: List[str]) -> np.ndarray:
"""Extract subtree at given path.
Parameters
----------
encoding : np.ndarray
Encoded tree
path : List[str]
Path to subtree (e.g., ["left", "right"])
Returns
-------
np.ndarray
Subtree encoding
"""def generate_random_vector(dimension: int,
method: str = "gaussian") -> np.ndarray:
"""Generate random vector.
Parameters
----------
dimension : int
Vector dimension
method : str
Generation method: "gaussian", "uniform", "sparse"
Returns
-------
np.ndarray
Random vector
"""def generate_unitary_vector(dimension: int) -> np.ndarray:
"""Generate unitary (self-inverse) vector.
Parameters
----------
dimension : int
Vector dimension
Returns
-------
np.ndarray
Unitary vector
"""def generate_orthogonal_set(dimension: int,
n_vectors: int) -> np.ndarray:
"""Generate set of orthogonal vectors.
Parameters
----------
dimension : int
Vector dimension
n_vectors : int
Number of vectors
Returns
-------
np.ndarray
Array of shape (n_vectors, dimension)
"""def analyze_binding_capacity(hrr: HRR,
n_pairs: int) -> Dict[str, float]:
"""Analyze binding capacity of HRR system.
Parameters
----------
hrr : HRR
HRR system to analyze
n_pairs : int
Number of role-filler pairs to test
Returns
-------
Dict[str, float]
Analysis results including mean/min/max similarity
"""def measure_crosstalk(hrr: HRR,
vectors: List[np.ndarray]) -> float:
"""Measure crosstalk between vectors.
Parameters
----------
hrr : HRR
HRR system
vectors : List[np.ndarray]
Vectors to test
Returns
-------
float
Average crosstalk level
"""def measure_associative_capacity(hrr: HRR,
n_items: int) -> Dict[str, Any]:
"""Test associative memory capacity.
Parameters
----------
hrr : HRR
HRR system
n_items : int
Number of associations to test
Returns
-------
Dict[str, Any]
Test results
"""def to_complex(vector: np.ndarray) -> np.ndarray:
"""Convert real vector to complex representation.
Parameters
----------
vector : np.ndarray
Real-valued vector
Returns
-------
np.ndarray
Complex-valued vector
"""def from_complex(vector: np.ndarray) -> np.ndarray:
"""Convert complex vector to real representation.
Parameters
----------
vector : np.ndarray
Complex-valued vector
Returns
-------
np.ndarray
Real-valued vector
"""def plot_similarity_matrix(vectors: Dict[str, np.ndarray],
figsize: Tuple[int, int] = (10, 8)) -> plt.Figure:
"""Plot similarity matrix heatmap.
Parameters
----------
vectors : Dict[str, np.ndarray]
Dictionary of named vectors
figsize : Tuple[int, int]
Figure size
Returns
-------
plt.Figure
Matplotlib figure
"""def plot_binding_accuracy(hrr: HRR,
test_results: Dict[str, Any],
figsize: Tuple[int, int] = (10, 6)) -> plt.Figure:
"""Plot binding accuracy results.
Parameters
----------
hrr : HRR
HRR system
test_results : Dict[str, Any]
Results from capacity analysis
figsize : Tuple[int, int]
Figure size
Returns
-------
plt.Figure
Matplotlib figure
"""def visualize_cleanup_space(cleanup_memory: CleanupMemory,
method: str = "pca") -> plt.Figure:
"""Visualize cleanup memory space.
Parameters
----------
cleanup_memory : CleanupMemory
Cleanup memory to visualize
method : str
Dimensionality reduction: "pca" or "tsne"
Returns
-------
plt.Figure
Matplotlib figure
"""def create_hrr(dimension: int = 1024, **kwargs) -> HRR:
"""Create HRR system with specified parameters.
Parameters
----------
dimension : int
Vector dimension (default: 1024)
**kwargs
Additional arguments for HRRConfig
Returns
-------
HRR
Configured HRR system
Examples
--------
>>> hrr = create_hrr(dimension=2048, normalize=True)
>>> hrr = create_hrr(storage_method="complex")
"""class HRRError(Exception):
"""Base exception for HRR operations."""class DimensionMismatchError(HRRError):
"""Raised when vector dimensions don't match."""class CleanupError(HRRError):
"""Raised when cleanup operation fails."""# Type aliases for clarity
VectorArray = np.ndarray # Shape: (dimension,)
VectorSet = np.ndarray # Shape: (n_vectors, dimension)
SimilarityMatrix = np.ndarray # Shape: (n, n)# Default values
DEFAULT_DIMENSION = 1024
DEFAULT_THRESHOLD = 0.3
DEFAULT_SEED = None
# Method options
STORAGE_METHODS = ["real", "complex"]
SIMILARITY_METHODS = ["cosine", "dot", "euclidean"]
ENCODING_METHODS = ["position", "chaining"]
VECTOR_METHODS = ["gaussian", "uniform", "sparse"]