-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaligned_mtl.py
More file actions
118 lines (92 loc) · 4.36 KB
/
aligned_mtl.py
File metadata and controls
118 lines (92 loc) · 4.36 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# The code of this file was partly adapted from
# https://github.com/SamsungLabs/MTL/tree/master/code/optim/aligned.
# It is therefore also subject to the following license.
#
# MIT License
#
# Copyright (c) 2022 Samsung
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import torch
from torch import Tensor
from ._pref_vector_utils import pref_vector_to_str_suffix, pref_vector_to_weighting
from .bases import _WeightedAggregator, _Weighting
from .mean import _MeanWeighting
class AlignedMTL(_WeightedAggregator):
"""
:class:`~torchjd.aggregation.bases.Aggregator` as defined in Algorithm 1 of
`Independent Component Alignment for Multi-Task Learning
<https://openaccess.thecvf.com/content/CVPR2023/papers/Senushkin_Independent_Component_Alignment_for_Multi-Task_Learning_CVPR_2023_paper.pdf>`_.
:param pref_vector: The preference vector to use.
.. admonition::
Example
Use AlignedMTL to aggregate a matrix.
>>> from torch import tensor
>>> from torchjd.aggregation import AlignedMTL
>>>
>>> A = AlignedMTL()
>>> J = tensor([[-4., 1., 1.], [6., 1., 1.]])
>>>
>>> A(J)
tensor([0.2133, 0.9673, 0.9673])
.. note::
This implementation was adapted from the `official implementation
<https://github.com/SamsungLabs/MTL/tree/master/code/optim/aligned>`_.
"""
def __init__(self, pref_vector: Tensor | None = None):
weighting = pref_vector_to_weighting(pref_vector, default=_MeanWeighting())
self._pref_vector = pref_vector
super().__init__(weighting=_AlignedMTLWrapper(weighting))
def __repr__(self) -> str:
return f"{self.__class__.__name__}(pref_vector={repr(self._pref_vector)})"
def __str__(self) -> str:
return f"AlignedMTL{pref_vector_to_str_suffix(self._pref_vector)}"
class _AlignedMTLWrapper(_Weighting):
"""
Wrapper of :class:`~torchjd.aggregation.bases._Weighting` that corrects the extracted
weights with the balance transformation defined in Algorithm 1 of `Independent Component
Alignment for Multi-Task Learning
<https://openaccess.thecvf.com/content/CVPR2023/papers/Senushkin_Independent_Component_Alignment_for_Multi-Task_Learning_CVPR_2023_paper.pdf>`_.
:param weighting: The wrapped :class:`~torchjd.aggregation.bases._Weighting`
responsible for extracting weight vectors from the input matrices.
"""
def __init__(self, weighting: _Weighting):
super().__init__()
self.weighting = weighting
def forward(self, matrix: Tensor) -> Tensor:
w = self.weighting(matrix)
G = matrix.T
B = self._compute_balance_transformation(G)
alpha = B @ w
return alpha
@staticmethod
def _compute_balance_transformation(G: Tensor) -> Tensor:
M = G.T @ G
lambda_, V = torch.linalg.eigh(M, UPLO="U") # More modern equivalent to torch.symeig
tol = torch.max(lambda_) * len(M) * torch.finfo().eps
rank = sum(lambda_ > tol)
if rank == 0:
identity = torch.eye(len(M), dtype=M.dtype, device=M.device)
return identity
order = torch.argsort(lambda_, dim=-1, descending=True)
lambda_, V = lambda_[order][:rank], V[:, order][:, :rank]
sigma_inv = torch.diag(1 / lambda_.sqrt())
lambda_R = lambda_[-1]
B = lambda_R.sqrt() * V @ sigma_inv @ V.T
return B