Skip to content

Commit 47ad975

Browse files
feat(scalarization): Add GeometricMean (#716)
--------- Signed-off-by: ppraneth <pranethparuchuri@gmail.com> Co-authored-by: Pierre Quinton <pierre.quinton@gmail.com>
1 parent 25b44f0 commit 47ad975

6 files changed

Lines changed: 94 additions & 7 deletions

File tree

‎CHANGELOG.md‎

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

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Added `GeometricMean` (also known as GLS) studied in [MultiNet++: Multi-Stream Feature
14+
Aggregation and Geometric Loss Strategy for Multi-Task
15+
Learning](https://openaccess.thecvf.com/content_CVPRW_2019/papers/WAD/Chennupati_MultiNet_Multi-Stream_Feature_Aggregation_and_Geometric_Loss_Strategy_for_Multi-Task_CVPRW_2019_paper.pdf),
16+
a `Scalarizer` that returns the geometric mean of the input tensor of values.
17+
1118
## [0.12.0] - 2026-05-28
1219

1320
### Added
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:hide-toc:
2+
3+
GeometricMean
4+
=============
5+
6+
.. autoclass:: torchjd.scalarization.GeometricMean
7+
:members: __call__

‎docs/source/docs/scalarization/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Abstract base class
1616
:maxdepth: 1
1717

1818
constant.rst
19+
geometric_mean.rst
1920
mean.rst
2021
random.rst
2122
sum.rst

‎src/torchjd/scalarization/__init__.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020
"""
2121

2222
from ._constant import Constant
23+
from ._geometric_mean import GeometricMean
2324
from ._mean import Mean
2425
from ._random import Random
2526
from ._scalarizer_base import Scalarizer
2627
from ._sum import Sum
2728

28-
__all__ = [
29-
"Constant",
30-
"Mean",
31-
"Random",
32-
"Scalarizer",
33-
"Sum",
34-
]
29+
__all__ = ["Constant", "GeometricMean", "Mean", "Random", "Scalarizer", "Sum"]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import torch
2+
from torch import Tensor
3+
4+
from ._scalarizer_base import Scalarizer
5+
6+
7+
class GeometricMean(Scalarizer):
8+
"""
9+
:class:`~torchjd.scalarization.Scalarizer` that returns the geometric mean of the input tensor
10+
of values, as studied in `MultiNet++: Multi-Stream Feature Aggregation and Geometric Loss
11+
Strategy for Multi-Task Learning
12+
<https://openaccess.thecvf.com/content_CVPRW_2019/papers/WAD/Chennupati_MultiNet_Multi-Stream_Feature_Aggregation_and_Geometric_Loss_Strategy_for_Multi-Task_CVPRW_2019_paper.pdf>`_.
13+
14+
This method is also known as GLS (Geometric Loss Strategy).
15+
"""
16+
17+
def forward(self, values: Tensor, /) -> Tensor:
18+
if (values < 1e-12).any():
19+
raise ValueError(
20+
"GeometricMean is only defined for strictly positive values. Found a value "
21+
"below 1e-12 in the input."
22+
)
23+
return torch.exp(torch.log(values).mean())
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import torch
2+
from pytest import mark, raises
3+
from torch import Tensor
4+
from utils.tensors import rand_, tensor_
5+
6+
from torchjd.scalarization import GeometricMean
7+
8+
from ._asserts import (
9+
assert_grad_flow,
10+
assert_permutation_invariant,
11+
assert_returns_scalar,
12+
)
13+
from ._inputs import shapes
14+
15+
positive_inputs: list[Tensor] = [rand_(shape) + 1 for shape in shapes]
16+
17+
18+
def test_value() -> None:
19+
losses = tensor_([1.0, 2.0, 4.0])
20+
torch.testing.assert_close(GeometricMean()(losses), tensor_(2.0))
21+
22+
23+
@mark.parametrize("losses", positive_inputs)
24+
def test_expected_structure(losses: Tensor) -> None:
25+
assert_returns_scalar(GeometricMean(), losses)
26+
27+
28+
@mark.parametrize("losses", positive_inputs)
29+
def test_grad_flow(losses: Tensor) -> None:
30+
assert_grad_flow(GeometricMean(), losses)
31+
32+
33+
@mark.parametrize("losses", positive_inputs)
34+
def test_permutation_invariant(losses: Tensor) -> None:
35+
assert_permutation_invariant(GeometricMean(), losses)
36+
37+
38+
@mark.parametrize(
39+
"invalid",
40+
[
41+
tensor_([1.0, 0.0]),
42+
tensor_([1.0, -1.0]),
43+
tensor_([1.0, 1e-13]),
44+
],
45+
)
46+
def test_raises_on_non_positive_input(invalid: Tensor) -> None:
47+
with raises(ValueError):
48+
GeometricMean()(invalid)
49+
50+
51+
def test_representations() -> None:
52+
s = GeometricMean()
53+
assert repr(s) == "GeometricMean()"
54+
assert str(s) == "GeometricMean"

0 commit comments

Comments
 (0)