Skip to content

Commit 8ac3b0e

Browse files
authored
feat(scalarization): Add scalarization package (#701)
* Add torchjd.scalarization with Scalarizer, Constant, Sum, Mean and Random. * Add changelog entry. * Move _vector_str.py out of the aggregation package * Add tests and documentation for scalarizers ---- Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
1 parent 385e7f0 commit 8ac3b0e

27 files changed

Lines changed: 434 additions & 15 deletions

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ changelog does not include internal changes that do not affect the user.
1010

1111
### Added
1212

13+
- Added a new `torchjd.scalarization` package providing the abstract `Scalarizer` base class and
14+
the concrete implementations `Constant`, `Mean`, `Random`, and `Sum`. These baselines simply
15+
combine losses into a scalar that can be optimized with a standard backward pass, making them
16+
useful for comparison with JD-based methods.
1317
- Added `FairGrad` and `FairGradWeighting` from [Fair Resource Allocation in Multi-Task
1418
Learning](https://arxiv.org/pdf/2402.15638).
1519

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:hide-toc:
2+
3+
Constant
4+
========
5+
6+
.. autoclass:: torchjd.scalarization.Constant
7+
:members: __call__
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
scalarization
2+
=============
3+
4+
.. automodule:: torchjd.scalarization
5+
:no-members:
6+
7+
Abstract base class
8+
-------------------
9+
10+
.. autoclass:: torchjd.scalarization.Scalarizer
11+
:members: __call__
12+
13+
14+
.. toctree::
15+
:hidden:
16+
:maxdepth: 1
17+
18+
constant.rst
19+
mean.rst
20+
random.rst
21+
sum.rst
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:hide-toc:
2+
3+
Mean
4+
====
5+
6+
.. autoclass:: torchjd.scalarization.Mean
7+
:members: __call__
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:hide-toc:
2+
3+
Random
4+
======
5+
6+
.. autoclass:: torchjd.scalarization.Random
7+
:members: __call__
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:hide-toc:
2+
3+
Sum
4+
===
5+
6+
.. autoclass:: torchjd.scalarization.Sum
7+
:members: __call__

‎docs/source/index.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Jacobian descent is the aggregator, which maps the Jacobian to an optimization s
3131
:doc:`Aggregation <docs/aggregation/index>`, we provide an overview of the various aggregators
3232
available in TorchJD, and their corresponding weightings.
3333

34+
For comparison against simple baselines, the :doc:`Scalarization <docs/scalarization/index>`
35+
package provides scalarizers that combine a tensor of losses into a single scalar loss, allowing
36+
standard gradient descent to be used.
37+
3438
A straightforward application of Jacobian descent is multi-task learning, in which the vector of
3539
per-task losses has to be minimized. To start using TorchJD for multi-task learning, follow our
3640
:doc:`MTL example <examples/mtl>`.
@@ -70,4 +74,5 @@ TorchJD is open-source, under MIT License. The source code is available on
7074
docs/autogram/index.rst
7175
docs/autojac/index.rst
7276
docs/aggregation/index.rst
77+
docs/scalarization/index.rst
7378
docs/linalg/index.rst
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ def vector_to_str(vector: Tensor) -> str:
99

1010
weights_str = ", ".join([f"{value:.2f}".rstrip("0") for value in vector])
1111
return weights_str
12+
13+
14+
def pref_vector_to_str_suffix(pref_vector: Tensor | None) -> str:
15+
"""Returns a suffix string containing the representation of the optional preference vector."""
16+
17+
if pref_vector is None:
18+
return ""
19+
return f"([{vector_to_str(pref_vector)}])"

‎src/torchjd/aggregation/_aligned_mtl.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import torch
88
from torch import Tensor
99

10+
from torchjd._vector_str import pref_vector_to_str_suffix
1011
from torchjd.linalg import PSDMatrix
1112

1213
from ._aggregator_bases import GramianWeightedAggregator
1314
from ._mean import MeanWeighting
14-
from ._utils.pref_vector import pref_vector_to_str_suffix, pref_vector_to_weighting
15+
from ._utils.pref_vector import pref_vector_to_weighting
1516
from ._weighting_bases import _GramianWeighting
1617

1718
SUPPORTED_SCALE_MODE: TypeAlias = Literal["min", "median", "rmse"]

‎src/torchjd/aggregation/_config.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import torch
66
from torch import Tensor
77

8+
from torchjd._vector_str import pref_vector_to_str_suffix
89
from torchjd.linalg import Matrix
910

1011
from ._aggregator_bases import Aggregator
1112
from ._mixins import _NonDifferentiable
1213
from ._sum import SumWeighting
13-
from ._utils.pref_vector import pref_vector_to_str_suffix, pref_vector_to_weighting
14+
from ._utils.pref_vector import pref_vector_to_weighting
1415

1516

1617
# Non-differentiable: the pseudoinverse and the normalization are not differentiable in this context.

0 commit comments

Comments
 (0)