-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path_property_testers.py
More file actions
82 lines (64 loc) · 2.97 KB
/
_property_testers.py
File metadata and controls
82 lines (64 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import torch
from pytest import mark
from torch import Tensor
from torch.testing import assert_close
from torchjd.aggregation import Aggregator
from ._inputs import matrices, scaled_matrices, weak_stationary_matrices, zero_matrices
class ExpectedStructureProperty:
"""
This class tests that the vector returned by the `__call__` method of an `Aggregator` has the
expected structure: it should return a vector whose dimension should be the number of columns of
the input matrix, and that should only contain finite values (no `nan`, `inf` or `-inf`). Note
that this property implies that the `__call__` method does not raise any exception.
"""
@classmethod
@mark.parametrize("matrix", scaled_matrices + zero_matrices)
def test_expected_structure_property(cls, aggregator: Aggregator, matrix: Tensor):
cls._assert_expected_structure_property(aggregator, matrix)
@staticmethod
def _assert_expected_structure_property(aggregator: Aggregator, matrix: Tensor) -> None:
vector = aggregator(matrix) # Will fail if the call raises an exception
assert vector.shape == matrix.shape[1:]
assert vector.isfinite().all()
class NonConflictingProperty:
"""
This class tests empirically that a given `Aggregator` satisfies the non-conflicting property.
"""
@classmethod
@mark.parametrize("matrix", weak_stationary_matrices + matrices)
def test_non_conflicting_property(
cls,
aggregator: Aggregator,
matrix: Tensor,
):
cls._assert_non_conflicting_property(aggregator, matrix)
@staticmethod
def _assert_non_conflicting_property(
aggregator: Aggregator,
matrix: Tensor,
) -> None:
vector = aggregator(matrix)
output_direction = matrix @ vector
positive_directions = output_direction[output_direction >= 0]
assert_close(positive_directions.norm(), output_direction.norm(), atol=4e-04, rtol=0)
class PermutationInvarianceProperty:
"""
This class tests empirically that for a given `Aggregator`, randomly permuting rows of the input
matrix doesn't change the aggregation.
"""
N_PERMUTATIONS = 5
@classmethod
@mark.parametrize("matrix", matrices)
def test_permutation_invariance_property(cls, aggregator: Aggregator, matrix: Tensor):
cls._assert_permutation_invariance_property(aggregator, matrix)
@staticmethod
def _assert_permutation_invariance_property(aggregator: Aggregator, matrix: Tensor) -> None:
vector = aggregator(matrix)
for _ in range(PermutationInvarianceProperty.N_PERMUTATIONS):
permuted_matrix = PermutationInvarianceProperty._permute_randomly(matrix)
permuted_vector = aggregator(permuted_matrix)
assert_close(vector, permuted_vector, atol=5e-04, rtol=1e-05)
@staticmethod
def _permute_randomly(matrix: Tensor) -> Tensor:
row_permutation = torch.randperm(matrix.size(dim=0))
return matrix[row_permutation]