-
Notifications
You must be signed in to change notification settings - Fork 17
feat(scalarization): Add GeometricMean
#716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+94
−7
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
26f2e06
add GLS support
ppraneth ec4e3aa
add GLS support with edge cases
ppraneth ac9f97a
minor fix
ppraneth f61eb64
Merge branch 'main' into scalarization-2
ppraneth 631609d
Update CHANGELOG.md
ppraneth 5966b0d
Update src/torchjd/scalarization/_gls.py
ppraneth c21a57b
Rename GLS to GMean and update tests
ppraneth 07861a3
Rename GMean to GeometricMean, update tests and docs
ppraneth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| :hide-toc: | ||
|
|
||
| GeometricMean | ||
| ============= | ||
|
|
||
| .. autoclass:: torchjd.scalarization.GeometricMean | ||
| :members: __call__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ Abstract base class | |
| :maxdepth: 1 | ||
|
|
||
| constant.rst | ||
| geometric_mean.rst | ||
| mean.rst | ||
| random.rst | ||
| sum.rst | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import torch | ||
| from torch import Tensor | ||
|
|
||
| from ._scalarizer_base import Scalarizer | ||
|
|
||
|
|
||
| class GeometricMean(Scalarizer): | ||
| """ | ||
| :class:`~torchjd.scalarization.Scalarizer` that returns the geometric mean of the input tensor | ||
| of values, as studied in `MultiNet++: Multi-Stream Feature Aggregation and Geometric Loss | ||
| Strategy for Multi-Task 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>`_. | ||
|
|
||
| This method is also known as GLS (Geometric Loss Strategy). | ||
| """ | ||
|
|
||
| def forward(self, values: Tensor, /) -> Tensor: | ||
| if (values < 1e-12).any(): | ||
| raise ValueError( | ||
| "GeometricMean is only defined for strictly positive values. Found a value " | ||
| "below 1e-12 in the input." | ||
| ) | ||
| return torch.exp(torch.log(values).mean()) | ||
|
ValerianRey marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import torch | ||
| from pytest import mark, raises | ||
| from torch import Tensor | ||
| from utils.tensors import rand_, tensor_ | ||
|
|
||
| from torchjd.scalarization import GeometricMean | ||
|
|
||
| from ._asserts import ( | ||
| assert_grad_flow, | ||
| assert_permutation_invariant, | ||
| assert_returns_scalar, | ||
| ) | ||
| from ._inputs import shapes | ||
|
|
||
| positive_inputs: list[Tensor] = [rand_(shape) + 1 for shape in shapes] | ||
|
|
||
|
|
||
| def test_value() -> None: | ||
| losses = tensor_([1.0, 2.0, 4.0]) | ||
| torch.testing.assert_close(GeometricMean()(losses), tensor_(2.0)) | ||
|
|
||
|
|
||
| @mark.parametrize("losses", positive_inputs) | ||
| def test_expected_structure(losses: Tensor) -> None: | ||
| assert_returns_scalar(GeometricMean(), losses) | ||
|
|
||
|
|
||
| @mark.parametrize("losses", positive_inputs) | ||
| def test_grad_flow(losses: Tensor) -> None: | ||
| assert_grad_flow(GeometricMean(), losses) | ||
|
|
||
|
|
||
| @mark.parametrize("losses", positive_inputs) | ||
| def test_permutation_invariant(losses: Tensor) -> None: | ||
| assert_permutation_invariant(GeometricMean(), losses) | ||
|
|
||
|
|
||
| @mark.parametrize( | ||
| "invalid", | ||
| [ | ||
| tensor_([1.0, 0.0]), | ||
| tensor_([1.0, -1.0]), | ||
| tensor_([1.0, 1e-13]), | ||
| ], | ||
| ) | ||
| def test_raises_on_non_positive_input(invalid: Tensor) -> None: | ||
| with raises(ValueError): | ||
| GeometricMean()(invalid) | ||
|
|
||
|
|
||
| def test_representations() -> None: | ||
| s = GeometricMean() | ||
| assert repr(s) == "GeometricMean()" | ||
| assert str(s) == "GeometricMean" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.