|
| 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 pytest |
| 25 | +import torch |
| 26 | +import torch.nn.functional as F |
| 27 | +import torchhd |
| 28 | +from torchhd import models |
| 29 | +from torchhd import MAPTensor |
| 30 | + |
| 31 | +from .utils import ( |
| 32 | + torch_dtypes, |
| 33 | + vsa_tensors, |
| 34 | + supported_dtype, |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +class TestCentroid: |
| 39 | + @pytest.mark.parametrize("dtype", torch_dtypes) |
| 40 | + def test_initialization(self, dtype): |
| 41 | + if dtype not in MAPTensor.supported_dtypes: |
| 42 | + return |
| 43 | + |
| 44 | + model = models.Centroid(1245, 12, dtype=dtype) |
| 45 | + assert torch.allclose(model.weight, torch.zeros(12, 1245, dtype=dtype)) |
| 46 | + assert model.weight.dtype == dtype |
| 47 | + |
| 48 | + device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 49 | + |
| 50 | + model = models.Centroid(1245, 12, dtype=dtype, device=device) |
| 51 | + assert torch.allclose(model.weight, torch.zeros(12, 1245, dtype=dtype)) |
| 52 | + assert model.weight.dtype == dtype |
| 53 | + assert model.weight.device == device |
| 54 | + |
| 55 | + def test_add(self): |
| 56 | + samples = torch.randn(4, 12) |
| 57 | + targets = torch.tensor([0, 1, 2, 2]) |
| 58 | + |
| 59 | + model = models.Centroid(12, 3) |
| 60 | + model.add(samples, targets) |
| 61 | + |
| 62 | + c = samples[:-1].clone() |
| 63 | + c[-1] += samples[-1] |
| 64 | + |
| 65 | + assert torch.allclose(model(samples), torchhd.cos(samples, c)) |
| 66 | + assert torch.allclose(model(samples, dot=True), torchhd.dot(samples, c)) |
| 67 | + |
| 68 | + model.normalize() |
| 69 | + print(model(samples, dot=True)) |
| 70 | + print(torchhd.cos(samples, c)) |
| 71 | + assert torch.allclose( |
| 72 | + model(samples, dot=True), torchhd.dot(samples, F.normalize(c)) |
| 73 | + ) |
| 74 | + |
| 75 | + def test_add_online(self): |
| 76 | + samples = torch.randn(10, 12) |
| 77 | + targets = torch.randint(0, 3, (10,)) |
| 78 | + |
| 79 | + model = models.Centroid(12, 3) |
| 80 | + model.add_online(samples, targets) |
| 81 | + |
| 82 | + logits = model(samples) |
| 83 | + assert logits.shape == (10, 3) |
| 84 | + |
| 85 | + |
| 86 | +class TestIntRVFL: |
| 87 | + @pytest.mark.parametrize("dtype", torch_dtypes) |
| 88 | + def test_initialization(self, dtype): |
| 89 | + if dtype not in MAPTensor.supported_dtypes: |
| 90 | + return |
| 91 | + |
| 92 | + model = models.IntRVFL(5, 1245, 12, dtype=dtype) |
| 93 | + assert torch.allclose(model.weight, torch.zeros(12, 1245, dtype=dtype)) |
| 94 | + assert model.weight.dtype == dtype |
| 95 | + |
| 96 | + device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 97 | + |
| 98 | + model = models.IntRVFL(5, 1245, 12, dtype=dtype, device=device) |
| 99 | + assert torch.allclose(model.weight, torch.zeros(12, 1245, dtype=dtype)) |
| 100 | + assert model.weight.dtype == dtype |
| 101 | + assert model.weight.device == device |
| 102 | + |
| 103 | + def test_fit_ridge_regression(self): |
| 104 | + samples = torch.randn(10, 12) |
| 105 | + targets = torch.randint(0, 3, (10,)) |
| 106 | + |
| 107 | + model = models.IntRVFL(12, 1245, 3) |
| 108 | + model.fit_ridge_regression(samples, targets) |
| 109 | + |
| 110 | + logits = model(samples) |
| 111 | + assert logits.shape == (10, 3) |
0 commit comments