Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

BinHD: A Binary Learning Framework for Hyperdimensional Computing

BinHD is a Python implementation based on "A Binary Learning Framework for Hyperdimensional Computing" paper.

Usage

Prerequisites

First, install PyTorch using their installation instructions. Then, use the following command to install Torchhd:

pip install torch-hd

Then, use the following command to install BinHD:

pip install binhd

Requirements: PyTorch, Torchhd and ucimlrepo to load datasets from UCI repository.

Quick Start

Iris Example

To quickly get started with BinHD, here's an example using the Iris dataset. Full training code is available in the examples/iris.py file.

import torch
import torch.nn as nn
import torchhd
from torchhd import embeddings
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from binhd.embeddings import ScatterCode
from binhd.datasets.iris import Iris
from binhd.classifiers import BinHD

# Use the GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using {} device".format(device))

iris = Iris()
dimension = 1000
num_levels = 100

min_val, max_val = iris.get_min_max_values()
print(min_val, max_val)

class RecordEncoder(nn.Module):
    def __init__(self, out_features, size, levels, low, high):
        super(RecordEncoder, self).__init__() 
        self.position = embeddings.Random(size, out_features, vsa="BSC", dtype=torch.uint8)
        self.value = ScatterCode(levels, out_features, low = low, high = high)
    
    def forward(self, x):
        sample_hv = torchhd.bind(self.position.weight, self.value(x))
        sample_hv = torchhd.multiset(sample_hv)
        return sample_hv

X = iris.features
y = list(iris.labels)

record_encode = RecordEncoder(dimension, iris.num_features, num_levels, min_val, max_val)
record_encode = record_encode.to(device)

with torch.no_grad():
    samples = torch.tensor(X.values).to(device)
    labels = torch.tensor(y).squeeze().to(device)

    X_hv = record_encode(samples)

X_train, X_test, y_train, y_test = train_test_split(X_hv, labels, test_size=0.3, random_state = 0)  

model = BinHD(dimension, iris.num_classes)

with torch.no_grad():
    model.fit(X_train,y_train)
    predictions = model.predict(X_test)  
    acc = accuracy_score(predictions, y_test)
    print("BinHD: Accuracy = ", acc)

    model.fit_adapt(X_train,y_train)
    predictions = model.predict(X_test)  
    acc = accuracy_score(predictions, y_test)
    print("BinHD - Adapt: Accuracy = ", acc)
    

Supported HDC/VSA models

Currently, the library supports the following HDC/VSA models:

Metadata

Release files for binhd 1.0.0a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binhd 1.0.0a0
File Size Uploaded
binhd-1.0.0a0.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for binhd 1.0.0a0
File Interpreter ABI Platform
binhd-1.0.0a0-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / binhd-1.0.0a0.tar.gz

Download URL binhd-1.0.0a0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
33a132f5729ac5bf5ded7ca9bf09e8e068132f5c66941a3db2a53432eab71532
BLAKE2b-256 checksum
How to use checksums
175a278c3479ef329cc5c37c7fa21aca08a5bca2f76c89f89476e77809a4affc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 24, 2025.

Transparency log

Release files / binhd-1.0.0a0-py3-none-any.whl

Download URL binhd-1.0.0a0-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61b4cc54fdfc78924a96ff9882a03a77a7e77db50f60789e60ebbaf80cfa01c6
BLAKE2b-256 checksum
How to use checksums
c94d47fbe0ec11ea9078d7e5b1c125bef5f7ecb24b66a79246d50e6526c693ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 24, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0a0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page