Skip to content

Commit 49a35d0

Browse files
mikeheddesgithub-actions[bot]pereverges
authored
Add new classifiers module with commonly used HD/VSA classifiers (#166)
* WIP add classify module * [github-action] formatting fixes * Add neuralHD * Add neuralhd to __all__ * Add LeHDC implementation * [github-action] formatting fixes * Adding models QuantHD, CompHD, SparseHD and RefineHD * [github-action] formatting fixes * Add classifiers documentation * [github-action] formatting fixes * Add IntRVFL * Fix bugs * [github-action] formatting fixes * Refactoring --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: pere verges <[email protected]>
1 parent 234edb9 commit 49a35d0

File tree

7 files changed

+1102
-1
lines changed

7 files changed

+1102
-1
lines changed

docs/_templates/class_classifier.rst

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. role:: hidden
2+
:class: hidden-section
3+
.. currentmodule:: {{ module }}
4+
5+
6+
{{ name | underline}}
7+
8+
.. autoclass:: {{ name }}
9+
:members: fit, predict, accuracy
10+
:special-members: __call__

docs/classifiers.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _models:
2+
3+
torchhd.classifiers
4+
=======================
5+
6+
.. currentmodule:: torchhd.classifiers
7+
8+
.. autosummary::
9+
:nosignatures:
10+
:toctree: generated/
11+
:template: class_classifier.rst
12+
13+
Classifier
14+
Vanilla
15+
AdaptHD
16+
OnlineHD
17+
NeuralHD
18+
DistHD
19+
CompHD
20+
SparseHD
21+
QuantHD
22+
LeHDC
23+
IntRVFL

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Torchhd is a Python library dedicated to *Hyperdimensional Computing* also known
1919
embeddings
2020
structures
2121
models
22+
classifiers
2223
memory
2324
datasets
2425
utils

examples/classifiers.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import torch
2+
import torchhd
3+
from torchhd.datasets.isolet import ISOLET
4+
5+
classifiers = [
6+
"Vanilla",
7+
"AdaptHD",
8+
"OnlineHD",
9+
"NeuralHD",
10+
"DistHD",
11+
"CompHD",
12+
"SparseHD",
13+
"QuantHD",
14+
"LeHDC",
15+
"IntRVFL",
16+
]
17+
18+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19+
print("Using {} device".format(device))
20+
21+
DIMENSIONS = 1024 # number of hypervector dimensions
22+
BATCH_SIZE = 12 # for GPUs with enough memory we can process multiple images at ones
23+
24+
train_ds = ISOLET("../data", train=True, download=True)
25+
train_ld = torch.utils.data.DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True)
26+
27+
test_ds = ISOLET("../data", train=False, download=True)
28+
test_ld = torch.utils.data.DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=False)
29+
30+
num_features = train_ds[0][0].size(-1)
31+
num_classes = len(train_ds.classes)
32+
33+
std, mean = torch.std_mean(train_ds.data, dim=0, keepdim=False)
34+
35+
36+
def transform(sample):
37+
return (sample - mean) / std
38+
39+
40+
train_ds.transform = transform
41+
test_ds.transform = transform
42+
43+
params = {
44+
"Vanilla": {},
45+
"AdaptHD": {
46+
"epochs": 10,
47+
},
48+
"OnlineHD": {
49+
"epochs": 10,
50+
},
51+
"NeuralHD": {
52+
"epochs": 10,
53+
"regen_freq": 5,
54+
},
55+
"DistHD": {
56+
"epochs": 10,
57+
"regen_freq": 5,
58+
},
59+
"CompHD": {},
60+
"SparseHD": {
61+
"epochs": 10,
62+
},
63+
"QuantHD": {
64+
"epochs": 10,
65+
},
66+
"LeHDC": {
67+
"epochs": 10,
68+
},
69+
"IntRVFL": {},
70+
}
71+
72+
for classifier in classifiers:
73+
print()
74+
print(classifier)
75+
76+
model_cls = getattr(torchhd.classifiers, classifier)
77+
model: torchhd.classifiers.Classifier = model_cls(
78+
num_features, DIMENSIONS, num_classes, device=device, **params[classifier]
79+
)
80+
81+
model.fit(train_ld)
82+
accuracy = model.accuracy(test_ld)
83+
print(f"Testing accuracy of {(accuracy * 100):.3f}%")

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"openpyxl",
3030
],
3131
packages=find_packages(exclude=["docs", "torchhd.tests", "examples"]),
32-
python_requires=">=3.6, <4",
32+
python_requires=">=3.8, <4",
3333
project_urls={
3434
"Source": "https://github.com/hyperdimensional-computing/torchhd",
3535
"Documentation": "https://torchhd.readthedocs.io",

torchhd/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import torchhd.embeddings as embeddings
2626
import torchhd.structures as structures
2727
import torchhd.models as models
28+
import torchhd.classifiers as classifiers
2829
import torchhd.memory as memory
2930
import torchhd.datasets as datasets
3031
import torchhd.utils as utils
@@ -92,6 +93,7 @@
9293
"embeddings",
9394
"structures",
9495
"models",
96+
"classifiers",
9597
"memory",
9698
"datasets",
9799
"utils",

0 commit comments

Comments
 (0)