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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33a132f5729ac5bf5ded7ca9bf09e8e068132f5c66941a3db2a53432eab71532
|
|
| MD5 |
09d07de7955a1fb327b253594cc7a03f
|
|
| BLAKE2b-256 |
175a278c3479ef329cc5c37c7fa21aca08a5bca2f76c89f89476e77809a4affc
|
Provenance
The following attestation bundles were made for binhd-1.0.0a0.tar.gz:
Publisher:
python-publish.yml on leandro-santiago/binhd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binhd-1.0.0a0.tar.gz -
Subject digest:
33a132f5729ac5bf5ded7ca9bf09e8e068132f5c66941a3db2a53432eab71532 - Sigstore transparency entry: 165240892
- Sigstore integration time:
-
Permalink:
leandro-santiago/binhd@22f13ae4a1ce34fad20387ac023f044848eea983 -
Branch / Tag:
refs/tags/v1.0.0-alpha - Owner: https://github.com/leandro-santiago
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@22f13ae4a1ce34fad20387ac023f044848eea983 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61b4cc54fdfc78924a96ff9882a03a77a7e77db50f60789e60ebbaf80cfa01c6
|
|
| MD5 |
aeb41c2d40104299e9560bf1e9e87a8e
|
|
| BLAKE2b-256 |
c94d47fbe0ec11ea9078d7e5b1c125bef5f7ecb24b66a79246d50e6526c693ff
|
Provenance
The following attestation bundles were made for binhd-1.0.0a0-py3-none-any.whl:
Publisher:
python-publish.yml on leandro-santiago/binhd
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
binhd-1.0.0a0-py3-none-any.whl -
Subject digest:
61b4cc54fdfc78924a96ff9882a03a77a7e77db50f60789e60ebbaf80cfa01c6 - Sigstore transparency entry: 165240897
- Sigstore integration time:
-
Permalink:
leandro-santiago/binhd@22f13ae4a1ce34fad20387ac023f044848eea983 -
Branch / Tag:
refs/tags/v1.0.0-alpha - Owner: https://github.com/leandro-santiago
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@22f13ae4a1ce34fad20387ac023f044848eea983 -
Trigger Event:
release
-
Statement type: