Skip to content

Commit 2364055

Browse files
authored
refactor(aggregation): Rename simplex projection helper (#814)
1 parent ec23448 commit 2364055

4 files changed

Lines changed: 9 additions & 9 deletions

File tree

‎src/torchjd/aggregation/_modo.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from torchjd.aggregation._mixins import _NonDifferentiable
1212
from torchjd.linalg import Matrix
1313

14-
from ._utils.simplex import _projection2simplex
14+
from ._utils.simplex import project_onto_simplex
1515
from ._weighting_bases import _MatrixWeighting
1616

1717

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

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

172172
self._lambda = lambd
173173
return lambd

‎src/torchjd/aggregation/_sdmgrad.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from torchjd.aggregation._mixins import _NonDifferentiable
1212
from torchjd.linalg import Matrix
1313

14-
from ._utils.simplex import _projection2simplex
14+
from ._utils.simplex import project_onto_simplex
1515
from ._weighting_bases import _MatrixWeighting
1616

1717

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

190190
self._w = w
191191
return (w + self._lambda * w_tilde) / (1.0 + self._lambda)

‎src/torchjd/aggregation/_utils/simplex.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from torch import Tensor
33

44

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

88
m = len(y)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from torch.testing import assert_close
22
from utils.tensors import tensor_
33

4-
from torchjd.aggregation._utils.simplex import _projection2simplex
4+
from torchjd.aggregation._utils.simplex import project_onto_simplex
55

66

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

1010
# Already-positive input: the deficit (1 - sum) is spread equally, no clamping.
1111
assert_close(
12-
_projection2simplex(tensor_([0.5, 0.1, 0.1])),
12+
project_onto_simplex(tensor_([0.5, 0.1, 0.1])),
1313
tensor_([0.6, 0.2, 0.2]),
1414
)
1515
# Input with a negative entry: it gets clamped to zero.
1616
assert_close(
17-
_projection2simplex(tensor_([1.0, 0.0, -0.5])),
17+
project_onto_simplex(tensor_([1.0, 0.0, -0.5])),
1818
tensor_([1.0, 0.0, 0.0]),
1919
)

0 commit comments

Comments
 (0)