Skip to main content

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

Project description

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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binhd-1.0.0a0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

binhd-1.0.0a0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file binhd-1.0.0a0.tar.gz.

File metadata

  • Download URL: binhd-1.0.0a0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for binhd-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 33a132f5729ac5bf5ded7ca9bf09e8e068132f5c66941a3db2a53432eab71532
MD5 09d07de7955a1fb327b253594cc7a03f
BLAKE2b-256 175a278c3479ef329cc5c37c7fa21aca08a5bca2f76c89f89476e77809a4affc

See more details on using hashes here.

Provenance

The following attestation bundles were made for binhd-1.0.0a0.tar.gz:

Publisher: python-publish.yml on leandro-santiago/binhd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binhd-1.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: binhd-1.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for binhd-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 61b4cc54fdfc78924a96ff9882a03a77a7e77db50f60789e60ebbaf80cfa01c6
MD5 aeb41c2d40104299e9560bf1e9e87a8e
BLAKE2b-256 c94d47fbe0ec11ea9078d7e5b1c125bef5f7ecb24b66a79246d50e6526c693ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for binhd-1.0.0a0-py3-none-any.whl:

Publisher: python-publish.yml on leandro-santiago/binhd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page