Skip to content

Commit e4d1e99

Browse files
committed
Add CGRTensor
Add a new VSA class named Cyclic Group Representation (CGR). This class is similar to MCR, but differs in bundling.
1 parent c22f2a3 commit e4d1e99

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

torchhd/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from torchhd.tensors.vtb import VTBTensor
4040
from torchhd.tensors.basemcr import BaseMCRTensor
4141
from torchhd.tensors.mcr import MCRTensor
42+
from torchhd.tensors.cgr import CGRTensor
4243

4344
from torchhd.functional import (
4445
ensure_vsa_tensor,
@@ -94,6 +95,7 @@
9495
"VTBTensor",
9596
"BaseMCRTensor",
9697
"MCRTensor",
98+
"CGRTensor",
9799
"functional",
98100
"embeddings",
99101
"structures",

torchhd/functional.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from torchhd.tensors.bsbc import BSBCTensor
3737
from torchhd.tensors.vtb import VTBTensor
3838
from torchhd.tensors.mcr import MCRTensor
39+
from torchhd.tensors.cgr import CGRTensor
3940
from torchhd.types import VSAOptions
4041

4142

@@ -93,6 +94,8 @@ def get_vsa_tensor_class(vsa: VSAOptions) -> Type[VSATensor]:
9394
return VTBTensor
9495
elif vsa == "MCR":
9596
return MCRTensor
97+
elif vsa == "CGR":
98+
return CGRTensor
9699

97100
raise ValueError(f"Provided VSA model is not supported, specified: {vsa}")
98101

torchhd/tensors/cgr.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# MIT License
3+
#
4+
# Copyright (c) 2023 Mike Heddes, Igor Nunes, Pere Vergés, Denis Kleyko, and Danny Abraham
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
import torch
25+
from torch import Tensor
26+
import torch.nn.functional as F
27+
from typing import Set
28+
29+
from torchhd.tensors.basemcr import BaseMCRTensor
30+
31+
32+
class CGRTensor(BaseMCRTensor):
33+
r"""Cyclic Group Representation (CGR)
34+
35+
First introduced in `Modular Composite Representation <https://link.springer.com/article/10.1007/s12559-013-9243-y>`_ and then better elaborated in `Understanding hyperdimensional computing for parallel single-pass learning <https://proceedings.neurips.cc/paper_files/paper/2022/file/080be5eb7e887319ff30c792c2cbc28c-Paper-Conference.pdf>`_, this model works with modular integer vectors. It works similar to the MCR class, but uses a bundling based on element-wise mode instead of addition of complex numbers.
36+
"""
37+
38+
def bundle(self, other: "CGRTensor") -> "CGRTensor":
39+
r"""Bundle the hypervector with majority voting. Ties might be broken at random. However, the expected result is that the tie representing the lowest value wins.
40+
41+
This produces a hypervector maximally similar to both.
42+
43+
The bundling operation is used to aggregate information into a single hypervector.
44+
45+
Args:
46+
other (CGR): other input hypervector
47+
48+
Shapes:
49+
- Self: :math:`(*)`
50+
- Other: :math:`(*)`
51+
- Output: :math:`(*)`
52+
53+
Examples::
54+
55+
>>> a, b = torchhd.CGRTensor.random(2, 10, block_size=64)
56+
>>> a
57+
CGRTensor([32, 26, 22, 22, 34, 30, 2, 4, 40, 43])
58+
>>> b
59+
CGRTensor([32, 26, 39, 54, 27, 60, 60, 4, 40, 5])
60+
>>> a.bundle(b)
61+
CGRTensor([32, 26, 39, 22, 27, 60, 2, 4, 40, 5])
62+
63+
"""
64+
assert self.block_size == other.block_size
65+
66+
t = torch.stack((self, other), dim=-2)
67+
val, _ = torch.mode(t, dim=-2)
68+
return val
69+
70+
def multibundle(self) -> "CGRTensor":
71+
"""Bundle multiple hypervectors"""
72+
73+
val, _ = torch.mode(self, dim=-2)
74+
return val
75+

torchhd/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
#
2424
from typing import Literal
2525

26-
VSAOptions = Literal["BSC", "MAP", "HRR", "FHRR", "BSBC", "VTB", "MCR"]
26+
VSAOptions = Literal["BSC", "MAP", "HRR", "FHRR", "BSBC", "VTB", "MCR", "CGR"]

0 commit comments

Comments
 (0)