Skip to content

Commit 890a502

Browse files
Simplified renaming of various core functions and classes (#124)
* Rename VSA_Model to VSATensor and respectively for all subclasses * [github-action] formatting fixes * Redesign vsa tensor specification * [github-action] formatting fixes * Rename SDM * [github-action] formatting fixes * Override + - * / operators for VSATensor type * [github-action] formatting fixes * Remove operator overloading * Fix vsa propagation in embedding classes * [github-action] formatting fixes * Update examples * Remove base permute implementation --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent d98d9b8 commit 890a502

36 files changed

+1471
-1376
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ import torch, torchhd
5656
d = 10000 # number of dimensions
5757

5858
# create the hypervectors for each symbol
59-
keys = torchhd.random_hv(3, d)
59+
keys = torchhd.random(3, d)
6060
country, capital, currency = keys
6161

62-
usa, mex = torchhd.random_hv(2, d) # United States and Mexico
63-
wdc, mxc = torchhd.random_hv(2, d) # Washington D.C. and Mexico City
64-
usd, mxn = torchhd.random_hv(2, d) # US Dollar and Mexican Peso
62+
usa, mex = torchhd.random(2, d) # United States and Mexico
63+
wdc, mxc = torchhd.random(2, d) # Washington D.C. and Mexico City
64+
usd, mxn = torchhd.random(2, d) # US Dollar and Mexican Peso
6565

6666
# create country representations
6767
us_values = torch.stack([usa, wdc, usd])
@@ -77,7 +77,7 @@ mx_us = torchhd.bind(torchhd.inverse(us), mx)
7777
usd_of_mex = torchhd.bind(mx_us, usd)
7878

7979
memory = torch.cat([keys, us_values, mx_values], dim=0)
80-
torchhd.cos_similarity(usd_of_mex, memory)
80+
torchhd.cosine_similarity(usd_of_mex, memory)
8181
# tensor([-0.0062, 0.0123, -0.0057, -0.0019, -0.0084, -0.0078, 0.0102, 0.0057, 0.3292])
8282
# The hypervector for the Mexican Peso is the most similar.
8383
```
@@ -87,10 +87,10 @@ This example is from the paper [What We Mean When We Say "What's the Dollar of M
8787
## Supported HDC/VSA models
8888
Currently, the library supports the following HDC/VSA models:
8989

90-
- [Multiply-Add-Permute (MAP)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.MAP.html)
91-
- [Binary Spatter Codes (BSC)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.BSC.html)
92-
- [Holographic Reduced Representations (HRR)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.HRR.html)
93-
- [Fourier Holographic Reduced Representations (FHRR)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.FHRR.html)
90+
- [Multiply-Add-Permute (MAP)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.MAPTensor.html)
91+
- [Binary Spatter Codes (BSC)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.BSCTensor.html)
92+
- [Holographic Reduced Representations (HRR)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.HRRTensor.html)
93+
- [Fourier Holographic Reduced Representations (FHRR)](https://torchhd.readthedocs.io/en/stable/generated/torchhd.FHRRTensor.html)
9494

9595
We welcome anyone to help with contributing more models to the library!
9696

docs/getting_started.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ The first step to encode these records is to define the basis-hypervectors for e
3333

3434
.. code-block:: python
3535
36-
from torchhd import functional
36+
import torchhd
3737
3838
d = 10000 # dimensions
39-
fruits = functional.random_hv(3, d)
40-
weights = functional.level_hv(10, d)
41-
seasons = functional.circular_hv(4, d)
42-
var = functional.random_hv(3, d)
39+
fruits = torchhd.random(3, d)
40+
weights = torchhd.level(10, d)
41+
seasons = torchhd.circular(4, d)
42+
var = torchhd.random(3, d)
4343
4444
which creates hypervectors for the 3 fruit types, 10 weight levels, 4 seasons and the 3 variables. The figure below illustrates the distance between the pairs of hypervectors in each set:
4545

@@ -55,7 +55,7 @@ Similar behavior can be achieved using the classes in the :ref:`embeddings` modu
5555
5656
weight = torch.tensor([149.0])
5757
# explicit mapping of the fruit weight to an index
58-
w_i = functional.value_to_index(weight, 0, 200, 10)
58+
w_i = torchhd.functional.value_to_index(weight, 0, 200, 10)
5959
weights[w_i] # select representation of 149
6060
6161
whereas the :ref:`embeddings<embeddings>` have this common behavior built-in:
@@ -75,10 +75,10 @@ Once the basis-hypervectors are defined, we can use the MAP operations from :ref
7575

7676
.. code-block:: python
7777
78-
f = functional.bind(var[0], fruits[0]) # fruit = apple
79-
w = functional.bind(var[1], weights[w_i]) # weight = 149
80-
s = functional.bind(var[2], seasons[3]) # season = fall
81-
r1 = functional.bundle(functional.bundle(f, w), s)
78+
f = torchhd.bind(var[0], fruits[0]) # fruit = apple
79+
w = torchhd.bind(var[1], weights[w_i]) # weight = 149
80+
s = torchhd.bind(var[2], seasons[3]) # season = fall
81+
r1 = torchhd.bundle(torchhd.bundle(f, w), s)
8282
8383
which is equivalent to using the following shortened syntax:
8484

@@ -95,7 +95,7 @@ Alternatively, we can use one of the commonly used encodings provided in the :re
9595
9696
# combine values in one tensor of shape (3, d)
9797
values = torch.stack([fruits[0], weights[w_i], seasons[3]])
98-
r1 = functional.hash_table(var, values)
98+
r1 = torchhd.hash_table(var, values)
9999
100100
The :ref:`structures` module contains the same encoding patterns in addition to binary trees and finite state automata, but provides them as data structures. This module provides class-based implementations of HDC data structures. Using the hash table class, record :math:`r_1` can be represented as follows:
101101

docs/memory.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ torchhd.memory
99
:toctree: generated/
1010
:template: class.rst
1111

12-
SDM
12+
SparseDistributed

docs/torchhd.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Basis-hypervector sets
1414
:toctree: generated/
1515
:template: function.rst
1616

17-
empty_hv
18-
identity_hv
19-
random_hv
20-
level_hv
21-
thermometer_hv
22-
circular_hv
17+
empty
18+
identity
19+
random
20+
level
21+
thermometer
22+
circular
2323

2424

2525
Operations
@@ -50,7 +50,7 @@ Similarities
5050
:toctree: generated/
5151
:template: function.rst
5252

53-
cos_similarity
53+
cosine_similarity
5454
dot_similarity
5555
hamming_similarity
5656

@@ -79,11 +79,11 @@ VSA Models
7979
:toctree: generated/
8080
:template: class.rst
8181

82-
VSA_Model
83-
BSC
84-
MAP
85-
HRR
86-
FHRR
82+
VSATensor
83+
BSCTensor
84+
MAPTensor
85+
HRRTensor
86+
FHRRTensor
8787

8888

8989
Utilities
@@ -93,7 +93,7 @@ Utilities
9393
:toctree: generated/
9494
:template: function.rst
9595

96-
as_vsa_model
96+
ensure_vsa_tensor
9797
map_range
9898
value_to_index
9999
index_to_value

examples/hd_hashing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, levels: int, dimensions: int, device=None):
1414
self.dimensions = dimensions
1515
self.device = device
1616

17-
self.hvs = torchhd.circular_hv(levels, dimensions, device=device)
17+
self.hvs = torchhd.circular(levels, dimensions, device=device)
1818
self.servers = []
1919
self.server_hvs = []
2020
self.weight_by_server = {}

torchhd/__init__.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
import torchhd.datasets as datasets
77
import torchhd.utils as utils
88

9-
from torchhd.base import VSA_Model
10-
from torchhd.bsc import BSC
11-
from torchhd.map import MAP
12-
from torchhd.hrr import HRR
13-
from torchhd.fhrr import FHRR
9+
from torchhd.tensors.base import VSATensor
10+
from torchhd.tensors.bsc import BSCTensor
11+
from torchhd.tensors.map import MAPTensor
12+
from torchhd.tensors.hrr import HRRTensor
13+
from torchhd.tensors.fhrr import FHRRTensor
1414

1515
from torchhd.functional import (
16-
as_vsa_model,
17-
empty_hv,
18-
identity_hv,
19-
random_hv,
20-
level_hv,
21-
thermometer_hv,
22-
circular_hv,
16+
ensure_vsa_tensor,
17+
empty,
18+
identity,
19+
random,
20+
level,
21+
thermometer,
22+
circular,
2323
bind,
2424
bundle,
2525
permute,
@@ -31,8 +31,10 @@
3131
multirandsel,
3232
soft_quantize,
3333
hard_quantize,
34-
cos_similarity,
34+
cosine_similarity,
35+
cos,
3536
dot_similarity,
37+
dot,
3638
hamming_similarity,
3739
multiset,
3840
multibundle,
@@ -53,25 +55,25 @@
5355

5456
__all__ = [
5557
"__version__",
56-
"VSA_Model",
57-
"BSC",
58-
"MAP",
59-
"HRR",
60-
"FHRR",
58+
"VSATensor",
59+
"BSCTensor",
60+
"MAPTensor",
61+
"HRRTensor",
62+
"FHRRTensor",
6163
"functional",
6264
"embeddings",
6365
"structures",
6466
"models",
6567
"memory",
6668
"datasets",
6769
"utils",
68-
"as_vsa_model",
69-
"empty_hv",
70-
"identity_hv",
71-
"random_hv",
72-
"level_hv",
73-
"thermometer_hv",
74-
"circular_hv",
70+
"ensure_vsa_tensor",
71+
"empty",
72+
"identity",
73+
"random",
74+
"level",
75+
"thermometer",
76+
"circular",
7577
"bind",
7678
"bundle",
7779
"permute",
@@ -83,8 +85,10 @@
8385
"multirandsel",
8486
"soft_quantize",
8587
"hard_quantize",
86-
"cos_similarity",
88+
"cosine_similarity",
89+
"cos",
8790
"dot_similarity",
91+
"dot",
8892
"hamming_similarity",
8993
"multiset",
9094
"multibundle",

0 commit comments

Comments
 (0)