Skip to content

Commit c70aa3e

Browse files
authored
Add aliasing for basic function (#52)
1 parent 0a22073 commit c70aa3e

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ import torch, torchhd
4545
d = 10000 # number of dimensions
4646

4747
# create the hypervectors for each symbol
48-
country = torchhd.functional.random_hv(1, d)
49-
capital = torchhd.functional.random_hv(1, d)
50-
currency = torchhd.functional.random_hv(1, d)
48+
country = torchhd.random_hv(1, d)
49+
capital = torchhd.random_hv(1, d)
50+
currency = torchhd.random_hv(1, d)
5151

52-
usa = torchhd.functional.random_hv(1, d) # United States
53-
mex = torchhd.functional.random_hv(1, d) # Mexico
52+
usa = torchhd.random_hv(1, d) # United States
53+
mex = torchhd.random_hv(1, d) # Mexico
5454

55-
wdc = torchhd.functional.random_hv(1, d) # Washington D.C.
56-
mxc = torchhd.functional.random_hv(1, d) # Mexico City
55+
wdc = torchhd.random_hv(1, d) # Washington D.C.
56+
mxc = torchhd.random_hv(1, d) # Mexico City
5757

58-
usd = torchhd.functional.random_hv(1, d) # US Dollar
59-
mxn = torchhd.functional.random_hv(1, d) # Mexican Peso
58+
usd = torchhd.random_hv(1, d) # US Dollar
59+
mxn = torchhd.random_hv(1, d) # Mexican Peso
6060

6161
# create country representations
6262
keys = torch.cat([country, capital, currency], dim=0)
@@ -67,10 +67,10 @@ US = torchhd.functional.hash_table(keys, us_values)
6767
mx_values = torch.cat([mex, mxc, mxn])
6868
MX = torchhd.functional.hash_table(keys, mx_values)
6969

70-
MX_US = torchhd.functional.bind(US, MX)
70+
MX_US = torchhd.bind(US, MX)
7171

7272
# query for the dollar of mexico
73-
usd_of_mex = torchhd.functional.bind(MX_US, usd)
73+
usd_of_mex = torchhd.bind(MX_US, usd)
7474

7575
memory = torch.cat([keys, us_values, mx_values], dim=0)
7676
torchhd.functional.cosine_similarity(usd_of_mex, memory)

torchhd/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33
import torchhd.structures as structures
44
import torchhd.datasets as datasets
55

6+
from torchhd.functional import (
7+
identity_hv,
8+
random_hv,
9+
level_hv,
10+
circular_hv,
11+
bind,
12+
bundle,
13+
permute,
14+
)
15+
616
__all__ = [
717
"functional",
818
"embeddings",
919
"structures",
1020
"datasets",
21+
"identity_hv",
22+
"random_hv",
23+
"level_hv",
24+
"circular_hv",
25+
"bind",
26+
"bundle",
27+
"permute",
1128
]

torchhd/functional.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def identity_hv(
4444
"""Creates a set of identity hypervector.
4545
4646
When bound with a random-hypervector :math:`x`, the result is :math:`x`.
47+
48+
Aliased as ``torchhd.identity_hv``.
4749
4850
Args:
4951
num_embeddings (int): the number of hypervectors to generate.
@@ -93,6 +95,8 @@ def random_hv(
9395
9496
The resulting hypervectors are sampled uniformly at random from the ``embedding_dim``-dimensional hyperspace.
9597
98+
Aliased as ``torchhd.random_hv``.
99+
96100
Args:
97101
num_embeddings (int): the number of hypervectors to generate.
98102
embedding_dim (int): the dimensionality of the hypervectors.
@@ -153,6 +157,8 @@ def level_hv(
153157
Implements level-hypervectors as an interpolation between random-hypervectors as described in `An Extension to Basis-Hypervectors for Learning from Circular Data in Hyperdimensional Computing <https://arxiv.org/abs/2205.07920>`_.
154158
The first and last hypervector in the generated set are quasi-orthogonal.
155159
160+
Aliased as ``torchhd.level_hv``.
161+
156162
Args:
157163
num_embeddings (int): the number of hypervectors to generate.
158164
embedding_dim (int): the dimensionality of the hypervectors.
@@ -249,6 +255,8 @@ def circular_hv(
249255
Implements circular-hypervectors based on level-hypervectors as described in `An Extension to Basis-Hypervectors for Learning from Circular Data in Hyperdimensional Computing <https://arxiv.org/abs/2205.07920>`_.
250256
Any hypervector is quasi-orthogonal to the hypervector opposite site of the circle.
251257
258+
Aliased as ``torchhd.circular_hv``.
259+
252260
Args:
253261
num_embeddings (int): the number of hypervectors to generate.
254262
embedding_dim (int): the dimensionality of the hypervectors.
@@ -366,6 +374,8 @@ def bind(input: Tensor, other: Tensor, *, out=None) -> Tensor:
366374
367375
\otimes: \mathcal{H} \times \mathcal{H} \to \mathcal{H}
368376
377+
Aliased as ``torchhd.bind``.
378+
369379
Args:
370380
input (Tensor): input hypervector
371381
other (Tensor): other input hypervector
@@ -406,6 +416,8 @@ def bundle(input: Tensor, other: Tensor, *, out=None) -> Tensor:
406416
407417
\oplus: \mathcal{H} \times \mathcal{H} \to \mathcal{H}
408418
419+
Aliased as ``torchhd.bundle``.
420+
409421
Args:
410422
input (Tensor): input hypervector
411423
other (Tensor): other input hypervector
@@ -446,6 +458,8 @@ def permute(input: Tensor, *, shifts=1, dims=-1) -> Tensor:
446458
447459
\Pi: \mathcal{H} \to \mathcal{H}
448460
461+
Aliased as ``torchhd.permute``.
462+
449463
Args:
450464
input (Tensor): input hypervector
451465
shifts (int or tuple of ints, optional): The number of places by which the elements of the tensor are shifted. If shifts is a tuple, dims must be a tuple of the same size, and each dimension will be rolled by the corresponding value.

0 commit comments

Comments
 (0)