|
| 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 | + |
0 commit comments