Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/torchjd/aggregation/_modo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from torchjd.aggregation._mixins import _NonDifferentiable
from torchjd.linalg import Matrix

from ._utils.simplex import _projection2simplex
from ._utils.simplex import project_onto_simplex
from ._weighting_bases import _MatrixWeighting


Expand Down Expand Up @@ -167,7 +167,7 @@ def forward(self, matrix: Matrix, /) -> Tensor:
lambd = cast(Tensor, self._lambda)

grad = matrix @ lambd + self._rho * lambd
lambd = _projection2simplex(lambd - self._gamma * grad)
lambd = project_onto_simplex(lambd - self._gamma * grad)

self._lambda = lambd
return lambd
Expand Down
4 changes: 2 additions & 2 deletions src/torchjd/aggregation/_sdmgrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from torchjd.aggregation._mixins import _NonDifferentiable
from torchjd.linalg import Matrix

from ._utils.simplex import _projection2simplex
from ._utils.simplex import project_onto_simplex
from ._weighting_bases import _MatrixWeighting


Expand Down Expand Up @@ -185,7 +185,7 @@ def forward(self, matrix: Matrix, /) -> Tensor:
for _ in range(self._n_iter):
grad = a @ (w + self._lambda * w_tilde)
velocity = grad if velocity is None else self._momentum * velocity + grad
w = _projection2simplex(w - self._lr * velocity)
w = project_onto_simplex(w - self._lr * velocity)

self._w = w
return (w + self._lambda * w_tilde) / (1.0 + self._lambda)
Expand Down
2 changes: 1 addition & 1 deletion src/torchjd/aggregation/_utils/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from torch import Tensor


def _projection2simplex(y: Tensor) -> Tensor:
def project_onto_simplex(y: Tensor) -> Tensor:
"""Euclidean projection of ``y`` onto the probability simplex."""

m = len(y)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/aggregation/_utils/test_simplex.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from torch.testing import assert_close
from utils.tensors import tensor_

from torchjd.aggregation._utils.simplex import _projection2simplex
from torchjd.aggregation._utils.simplex import project_onto_simplex


def test_projection2simplex_known_values() -> None:
def test_project_onto_simplex_known_values() -> None:
"""The simplex projection matches hand-computed Euclidean projections."""

# Already-positive input: the deficit (1 - sum) is spread equally, no clamping.
assert_close(
_projection2simplex(tensor_([0.5, 0.1, 0.1])),
project_onto_simplex(tensor_([0.5, 0.1, 0.1])),
tensor_([0.6, 0.2, 0.2]),
)
# Input with a negative entry: it gets clamped to zero.
assert_close(
_projection2simplex(tensor_([1.0, 0.0, -0.5])),
project_onto_simplex(tensor_([1.0, 0.0, -0.5])),
tensor_([1.0, 0.0, 0.0]),
)
Loading