Skip to content

Commit 2f796b7

Browse files
committed
Improve documentation for MCRTensor and CGRTensor
Replicate function signatures in inherited classes (MCRTensor and CGRTensor) from their parent class (BaseMCRTensor) to let sphinx print the documentation of each parent function.
1 parent b7ac97c commit 2f796b7

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

torchhd/tensors/cgr.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,71 @@ class CGRTensor(BaseMCRTensor):
3535
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.
3636
"""
3737

38+
@classmethod
39+
def empty(
40+
cls,
41+
num_vectors: int,
42+
dimensions: int,
43+
*,
44+
block_size: int,
45+
generator=None,
46+
dtype=torch.int64,
47+
device=None,
48+
requires_grad=False,
49+
) -> "CGRTensor":
50+
return super().empty(
51+
num_vectors,
52+
dimensions,
53+
block_size=block_size,
54+
generator=generator,
55+
dtype=dtype,
56+
device=device,
57+
requires_grad=requires_grad,
58+
)
59+
60+
@classmethod
61+
def identity(
62+
cls,
63+
num_vectors: int,
64+
dimensions: int,
65+
*,
66+
block_size: int,
67+
dtype=torch.int64,
68+
device=None,
69+
requires_grad=False,
70+
) -> "CGRTensor":
71+
return super().identity(
72+
num_vectors,
73+
dimensions,
74+
block_size=block_size,
75+
dtype=dtype,
76+
device=device,
77+
requires_grad=requires_grad,
78+
)
79+
80+
@classmethod
81+
def random(
82+
cls,
83+
num_vectors: int,
84+
dimensions: int,
85+
*,
86+
block_size: int,
87+
generator=None,
88+
dtype=torch.int64,
89+
device=None,
90+
requires_grad=False,
91+
) -> "CGRTensor":
92+
return super().random(
93+
num_vectors,
94+
dimensions,
95+
block_size=block_size,
96+
generator=generator,
97+
dtype=dtype,
98+
device=device,
99+
requires_grad=requires_grad,
100+
)
101+
102+
38103
def bundle(self, other: "CGRTensor") -> "CGRTensor":
39104
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.
40105
@@ -83,7 +148,29 @@ def multibundle(self) -> "CGRTensor":
83148
"""Bundle multiple hypervectors"""
84149
# The use of torch.mode() makes untying deterministic as it always
85150
# returns the lowest index among the ties. For example, if there is an
86-
# equal number amount of 0s and 1s in a bundle, 0 is returned.
151+
# equal amount of 0s and 1s in a bundle, 0 is returned.
87152
val, _ = torch.mode(self, dim=-2)
88153
return val
89154

155+
def bind(self, other: "CGRTensor") -> "CGRTensor":
156+
return super().bind(other)
157+
158+
def multibind(self) -> "CGRTensor":
159+
"""Bind multiple hypervectors"""
160+
return super().multibind()
161+
162+
def inverse(self) -> "CGRTensor":
163+
return super().inverse()
164+
165+
def permute(self, shifts: int = 1) -> "CGRTensor":
166+
return super().permute(shifts=shifts)
167+
168+
def normalize(self) -> "CGRTensor":
169+
return super().normalize()
170+
171+
def dot_similarity(self, others: "CGRTensor", *, dtype=None) -> Tensor:
172+
return super().dot_similarity(others, dtype=dtype)
173+
174+
def cosine_similarity(self, others: "CGRTensor", *, dtype=None) -> Tensor:
175+
return super().cosine_similarity(others, dtype=dtype)
176+

torchhd/tensors/mcr.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,70 @@ class MCRTensor(BaseMCRTensor):
3636
Proposed in `Modular Composite Representation <https://link.springer.com/article/10.1007/s12559-013-9243-y>`_, this model works with modular integer vectors.
3737
"""
3838

39+
@classmethod
40+
def empty(
41+
cls,
42+
num_vectors: int,
43+
dimensions: int,
44+
*,
45+
block_size: int,
46+
generator=None,
47+
dtype=torch.int64,
48+
device=None,
49+
requires_grad=False,
50+
) -> "MCRTensor":
51+
return super().empty(
52+
num_vectors,
53+
dimensions,
54+
block_size=block_size,
55+
generator=generator,
56+
dtype=dtype,
57+
device=device,
58+
requires_grad=requires_grad,
59+
)
60+
61+
@classmethod
62+
def identity(
63+
cls,
64+
num_vectors: int,
65+
dimensions: int,
66+
*,
67+
block_size: int,
68+
dtype=torch.int64,
69+
device=None,
70+
requires_grad=False,
71+
) -> "MCRTensor":
72+
return super().identity(
73+
num_vectors,
74+
dimensions,
75+
block_size=block_size,
76+
dtype=dtype,
77+
device=device,
78+
requires_grad=requires_grad,
79+
)
80+
81+
@classmethod
82+
def random(
83+
cls,
84+
num_vectors: int,
85+
dimensions: int,
86+
*,
87+
block_size: int,
88+
generator=None,
89+
dtype=torch.int64,
90+
device=None,
91+
requires_grad=False,
92+
) -> "MCRTensor":
93+
return super().random(
94+
num_vectors,
95+
dimensions,
96+
block_size=block_size,
97+
generator=generator,
98+
dtype=dtype,
99+
device=device,
100+
requires_grad=requires_grad,
101+
)
102+
39103
def bundle(self, other: "MCRTensor") -> "MCRTensor":
40104
r"""Bundle the hypervector with normalized complex vector addition.
41105
@@ -100,3 +164,25 @@ def multibundle(self) -> "MCRTensor":
100164

101165
return torch.remainder(result, self.block_size).type(self.dtype)
102166

167+
def bind(self, other: "MCRTensor") -> "MCRTensor":
168+
return super().bind(other)
169+
170+
def multibind(self) -> "MCRTensor":
171+
"""Bind multiple hypervectors"""
172+
return super().multibind()
173+
174+
def inverse(self) -> "MCRTensor":
175+
return super().inverse()
176+
177+
def permute(self, shifts: int = 1) -> "MCRTensor":
178+
return super().permute(shifts=shifts)
179+
180+
def normalize(self) -> "MCRTensor":
181+
return super().normalize()
182+
183+
def dot_similarity(self, others: "MCRTensor", *, dtype=None) -> Tensor:
184+
return super().dot_similarity(others, dtype=dtype)
185+
186+
def cosine_similarity(self, others: "MCRTensor", *, dtype=None) -> Tensor:
187+
return super().cosine_similarity(others, dtype=dtype)
188+

0 commit comments

Comments
 (0)