Skip to content

Commit 7a94240

Browse files
authored
fix(aggregation): Keep significant zeros in CAGrad string (#817)
1 parent ab4b296 commit 7a94240

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎src/torchjd/aggregation/_cagrad.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,5 @@ def __repr__(self) -> str:
135135
return f"{self.__class__.__name__}(c={self.c}, norm_eps={self.norm_eps})"
136136

137137
def __str__(self) -> str:
138-
c_str = str(self.c).rstrip("0")
138+
c_str = str(self.c).removesuffix(".0")
139139
return f"CAGrad{c_str}"

‎tests/unit/aggregation/test_cagrad.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ def test_representations() -> None:
6060
assert str(A) == "CAGrad0.5"
6161

6262

63+
@mark.parametrize(
64+
["c", "expected"],
65+
[
66+
(0, "CAGrad0"),
67+
(0.0, "CAGrad0"),
68+
(0.5, "CAGrad0.5"),
69+
(1, "CAGrad1"),
70+
(1.0, "CAGrad1"),
71+
(10, "CAGrad10"),
72+
(100.0, "CAGrad100"),
73+
],
74+
)
75+
def test_str_keeps_significant_zeros(c: float, expected: str) -> None:
76+
assert str(CAGrad(c=c)) == expected
77+
78+
6379
def test_c_setter_updates_value() -> None:
6480
A = CAGrad(c=0.5)
6581
A.c = 1.25

0 commit comments

Comments
 (0)