pyhdc: Top-Level Module

Version and availability

pyhdc.__version__: str

Current version string, e.g. "2.0.0".

pyhdc.__author__: str

Primary author identifier, "GNPower".

pyhdc.TORCH_AVAILABLE: bool

True if import torch succeeded at PyHDC import time. False if PyTorch is not installed.

import pyhdc
if pyhdc.TORCH_AVAILABLE:
    enc = pyhdc.MAP_C(backend="torch", device="cuda")

Convenience functions

These functions are thin wrappers that delegate to the first hypervector’s encoding. They are provided for concise one-off calls.

pyhdc.generate(encoding, size=None, use_generator=None)[source]

Generate one or more hypervectors using encoding.

Parameters:
  • encoding – An instantiated Encoding object.

  • sizeNone or omitted for a single (D,) vector; an int for a single vector of that dimension; a tuple (D, N) for a dimension-first batch of N vectors (each column a hypervector).

  • use_generator – Override the encoding’s generator setting. True forces the custom generator; False forces NumPy’s default.

Returns:

A Hypervector.

enc = pyhdc.MAP_C(dimension=10_000)
hv  = pyhdc.generate(enc)                          # (10000,)
batch = pyhdc.generate(enc, size=(10_000, 100))    # (10000, 100)
pyhdc.zeros(encoding, size=None)[source]

Return a zero-valued hypervector (or batch) for encoding.

Parameters:
  • encoding – An instantiated Encoding object.

  • size – Same as for generate().

Returns:

A Hypervector filled with zeros.

zero = pyhdc.zeros(enc)
pyhdc.bundle(*hypervectors)[source]

Bundle two or more hypervectors using the encoding of the first argument.

Parameters:

hypervectors – Two or more Hypervector objects produced by the same encoding.

Returns:

A Hypervector.

result = pyhdc.bundle(hv1, hv2, hv3)
pyhdc.bind(*hypervectors)[source]

Bind two or more hypervectors using the encoding of the first argument.

Parameters:

hypervectors – Two or more Hypervector objects.

Returns:

A Hypervector.

result = pyhdc.bind(key, value)
pyhdc.stack(hypervectors)[source]

Combine hypervectors and/or batches into one dimension-first (D, N) batch by concatenating along the batch axis. A 1-D (D,) vector is treated as a single column (D, 1). Backend-agnostic (NumPy or PyTorch).

Parameters:

hypervectors – A list of Hypervector objects (vectors or (D, N) batches) sharing a backend.

Returns:

A single Hypervector of shape (D, total_columns).

proto    = enc.generate()                    # (10000,)
codebook = enc.generate(size=(10_000, 50))   # (10000, 50)
combined = pyhdc.stack([proto, codebook])    # (10000, 51); proto is column 0

Global backend and device defaults

Set a process-wide default backend/device; encodings created without an explicit backend / device argument inherit it.

pyhdc.prefer_torch(device=None)[source]

Make PyTorch the default backend (optionally pinning device). Raises ImportError if PyTorch is not installed.

pyhdc.prefer_cuda(index=None)[source]

Make PyTorch on CUDA the default ("cuda" or "cuda:{index}"). Raises if PyTorch or CUDA is unavailable.

pyhdc.prefer_numpy()[source]

Reset the default backend to NumPy.

pyhdc.prefer_cpu()[source]

Pin the default device to CPU (relevant when the backend is PyTorch).

pyhdc.get_default_backend()[source]

Return the current default backend, "numpy" or "torch".

pyhdc.get_default_device()[source]

Return the current default device string, or None.

pyhdc.prefer_torch()                  # or pyhdc.prefer_cuda()
enc = pyhdc.MAP_C(dimension=10_000)   # inherits the torch backend
pyhdc.prefer_numpy()                  # reset to numpy

Encoding classes

All encoding classes are imported at the top level:

See Encoding Classes for full documentation of each class.

Exception classes

All exception classes are imported at the top level:

See Exceptions for details.

Generator base classes

  • HDCGenerator: abstract base for all generators

  • DefaultGenerator: NumPy-backed default

See Generation Module for the full family listing.