-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path_aggregator_bases.py
More file actions
82 lines (60 loc) · 2.53 KB
/
_aggregator_bases.py
File metadata and controls
82 lines (60 loc) · 2.53 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
from abc import ABC, abstractmethod
from torch import Tensor, nn
from torchjd._linalg import Matrix, PSDMatrix, compute_gramian, is_matrix
from ._weighting_bases import Weighting
class Aggregator(nn.Module, ABC):
r"""
Abstract base class for all aggregators. It has the role of aggregating matrices of dimension
:math:`m \times n` into row vectors of dimension :math:`n`.
"""
def __init__(self):
super().__init__()
@staticmethod
def _check_is_matrix(matrix: Tensor) -> None:
if not is_matrix(matrix):
raise ValueError(
"Parameter `matrix` should be a tensor of dimension 2. Found `matrix.shape = "
f"{matrix.shape}`."
)
@abstractmethod
def forward(self, matrix: Matrix) -> Tensor:
"""Computes the aggregation from the input matrix."""
# Override to make type hints and documentation more specific, note that `Matrix` type isn't
# public
def __call__(self, matrix: Tensor) -> Tensor:
"""Computes the aggregation from the input matrix and applies all registered hooks."""
Aggregator._check_is_matrix(matrix)
return super().__call__(matrix)
def __repr__(self) -> str:
return f"{self.__class__.__name__}()"
def __str__(self) -> str:
return f"{self.__class__.__name__}"
class WeightedAggregator(Aggregator):
"""
Aggregator that combines the rows of the input jacobian matrix with weights given by applying a
Weighting to it.
:param weighting: The object responsible for extracting the vector of weights from the matrix.
"""
def __init__(self, weighting: Weighting[Matrix]):
super().__init__()
self.weighting = weighting
@staticmethod
def combine(matrix: Matrix, weights: Tensor) -> Tensor:
"""
Aggregates a matrix by making a linear combination of its rows, using the provided vector of
weights.
"""
vector = weights @ matrix
return vector
def forward(self, matrix: Matrix) -> Tensor:
weights = self.weighting(matrix)
vector = self.combine(matrix, weights)
return vector
class GramianWeightedAggregator(WeightedAggregator):
"""
WeightedAggregator that computes the gramian of the input jacobian matrix before applying a
Weighting to it.
:param weighting: The object responsible for extracting the vector of weights from the gramian.
"""
def __init__(self, weighting: Weighting[PSDMatrix]):
super().__init__(weighting << compute_gramian)