|
5 | 5 |
|
6 | 6 | from torchjd.aggregation import Aggregator |
7 | 7 |
|
8 | | -from ._inputs import matrices, scaled_matrices, weak_stationary_matrices, zero_matrices |
| 8 | +from ._inputs import ( |
| 9 | + matrices, |
| 10 | + scaled_matrices, |
| 11 | + strong_stationary_matrices, |
| 12 | + weak_stationary_matrices, |
| 13 | + zero_matrices, |
| 14 | +) |
9 | 15 |
|
10 | 16 |
|
11 | 17 | class ExpectedStructureProperty: |
@@ -34,7 +40,7 @@ class NonConflictingProperty: |
34 | 40 | """ |
35 | 41 |
|
36 | 42 | @classmethod |
37 | | - @mark.parametrize("matrix", weak_stationary_matrices + matrices) |
| 43 | + @mark.parametrize("matrix", strong_stationary_matrices + weak_stationary_matrices + matrices) |
38 | 44 | def test_non_conflicting_property( |
39 | 45 | cls, |
40 | 46 | aggregator: Aggregator, |
@@ -80,3 +86,77 @@ def _assert_permutation_invariance_property(aggregator: Aggregator, matrix: Tens |
80 | 86 | def _permute_randomly(matrix: Tensor) -> Tensor: |
81 | 87 | row_permutation = torch.randperm(matrix.size(dim=0)) |
82 | 88 | return matrix[row_permutation] |
| 89 | + |
| 90 | + |
| 91 | +class StationarityProperty: |
| 92 | + """ |
| 93 | + This class tests empirically that a given `Aggregator` satisfies the stationarity property. |
| 94 | + """ |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def _assert_stationarity_property( |
| 98 | + aggregator: Aggregator, |
| 99 | + stationary_matrix: Tensor, |
| 100 | + ) -> None: |
| 101 | + vector = aggregator(stationary_matrix) |
| 102 | + norm = vector.norm().item() |
| 103 | + assert norm < 8e-02 |
| 104 | + |
| 105 | + @staticmethod |
| 106 | + def _assert_non_stationarity_property( |
| 107 | + aggregator: Aggregator, |
| 108 | + non_stationary_matrix: Tensor, |
| 109 | + ) -> None: |
| 110 | + vector = aggregator(non_stationary_matrix) |
| 111 | + norm = vector.norm().item() |
| 112 | + assert norm > 1e-03 |
| 113 | + |
| 114 | + |
| 115 | +class StrongStationarityProperty(StationarityProperty): |
| 116 | + |
| 117 | + @classmethod |
| 118 | + @mark.parametrize("stationary_matrix", strong_stationary_matrices) |
| 119 | + def test_stationarity_property( |
| 120 | + cls, |
| 121 | + aggregator: Aggregator, |
| 122 | + stationary_matrix: Tensor, |
| 123 | + ): |
| 124 | + super(StrongStationarityProperty, cls)._assert_stationarity_property( |
| 125 | + aggregator, stationary_matrix |
| 126 | + ) |
| 127 | + |
| 128 | + @classmethod |
| 129 | + @mark.parametrize("non_stationary_matrix", weak_stationary_matrices + matrices) |
| 130 | + def test_non_stationarity_property( |
| 131 | + cls, |
| 132 | + aggregator: Aggregator, |
| 133 | + non_stationary_matrix: Tensor, |
| 134 | + ): |
| 135 | + super(StrongStationarityProperty, cls)._assert_non_stationarity_property( |
| 136 | + aggregator, non_stationary_matrix |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +class WeakStationarityProperty(StationarityProperty): |
| 141 | + |
| 142 | + @classmethod |
| 143 | + @mark.parametrize("stationary_matrix", strong_stationary_matrices + weak_stationary_matrices) |
| 144 | + def test_stationarity_property( |
| 145 | + cls, |
| 146 | + aggregator: Aggregator, |
| 147 | + stationary_matrix: Tensor, |
| 148 | + ): |
| 149 | + super(WeakStationarityProperty, cls)._assert_stationarity_property( |
| 150 | + aggregator, stationary_matrix |
| 151 | + ) |
| 152 | + |
| 153 | + @classmethod |
| 154 | + @mark.parametrize("non_stationary_matrix", matrices) |
| 155 | + def test_non_stationarity_property( |
| 156 | + cls, |
| 157 | + aggregator: Aggregator, |
| 158 | + non_stationary_matrix: Tensor, |
| 159 | + ): |
| 160 | + super(WeakStationarityProperty, cls)._assert_non_stationarity_property( |
| 161 | + aggregator, non_stationary_matrix |
| 162 | + ) |
0 commit comments