Skip to content

Commit 2de4301

Browse files
authored
refactor(scalarization): Extract shape check in scalarizers (#801)
1 parent 120633f commit 2de4301

4 files changed

Lines changed: 26 additions & 30 deletions

File tree

‎src/torchjd/scalarization/_cosmos.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from torch.nn.functional import cosine_similarity
33

44
from ._scalarizer_base import Scalarizer
5+
from ._utils import check_same_shape_as_values
56

67

78
class COSMOS(Scalarizer):
@@ -52,12 +53,7 @@ def __init__(self, lambda_: float, weights: Tensor) -> None:
5253
self.weights = weights
5354

5455
def forward(self, values: Tensor, /) -> Tensor:
55-
if self.weights.shape != values.shape:
56-
raise ValueError(
57-
f"Parameter `weights` should have the same shape as `values`. Found "
58-
f"`weights.shape = {tuple(self.weights.shape)}` and `values.shape = "
59-
f"{tuple(values.shape)}`."
60-
)
56+
check_same_shape_as_values(self.weights, values, "weights")
6157

6258
weighted_sum = (self.weights * values).sum()
6359
cosine = cosine_similarity(self.weights.flatten(), values.flatten(), dim=0)

‎src/torchjd/scalarization/_pbi.py‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from torch import Tensor
33

44
from ._scalarizer_base import Scalarizer
5+
from ._utils import check_same_shape_as_values
56

67
_EPSILON = 1e-12
78

@@ -60,18 +61,8 @@ def __init__(self, theta: float, weights: Tensor, reference: Tensor | None = Non
6061
self.reference = reference
6162

6263
def forward(self, values: Tensor, /) -> Tensor:
63-
if self.weights.shape != values.shape:
64-
raise ValueError(
65-
f"Parameter `weights` should have the same shape as `values`. Found "
66-
f"`weights.shape = {tuple(self.weights.shape)}` and `values.shape = "
67-
f"{tuple(values.shape)}`."
68-
)
69-
if self.reference is not None and self.reference.shape != values.shape:
70-
raise ValueError(
71-
f"Parameter `reference` should have the same shape as `values`. Found "
72-
f"`reference.shape = {tuple(self.reference.shape)}` and `values.shape = "
73-
f"{tuple(values.shape)}`."
74-
)
64+
check_same_shape_as_values(self.weights, values, "weights")
65+
check_same_shape_as_values(self.reference, values, "reference")
7566

7667
shifted = values if self.reference is None else values - self.reference
7768
f = shifted.flatten()

‎src/torchjd/scalarization/_stch.py‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from torch import Tensor
33

44
from ._scalarizer_base import Scalarizer
5+
from ._utils import check_same_shape_as_values
56

67

78
class STCH(Scalarizer):
@@ -52,18 +53,8 @@ def __init__(
5253
self.reference = reference
5354

5455
def forward(self, values: Tensor, /) -> Tensor:
55-
if self.weights is not None and self.weights.shape != values.shape:
56-
raise ValueError(
57-
f"Parameter `weights` should have the same shape as `values`. Found "
58-
f"`weights.shape = {tuple(self.weights.shape)}` and `values.shape = "
59-
f"{tuple(values.shape)}`."
60-
)
61-
if self.reference is not None and self.reference.shape != values.shape:
62-
raise ValueError(
63-
f"Parameter `reference` should have the same shape as `values`. Found "
64-
f"`reference.shape = {tuple(self.reference.shape)}` and `values.shape = "
65-
f"{tuple(values.shape)}`."
66-
)
56+
check_same_shape_as_values(self.weights, values, "weights")
57+
check_same_shape_as_values(self.reference, values, "reference")
6758

6859
if self.weights is None:
6960
weights = torch.full_like(values, 1.0 / values.numel())
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from torch import Tensor
2+
3+
4+
def check_same_shape_as_values(tensor: Tensor | None, values: Tensor, name: str) -> None:
5+
"""
6+
Checks that ``tensor`` has the same shape as ``values``. Nothing is checked if ``tensor`` is
7+
``None``.
8+
9+
:param tensor: The tensor to check.
10+
:param values: The tensor of values to scalarize.
11+
:param name: The name of the parameter corresponding to ``tensor``, used in the error message.
12+
"""
13+
14+
if tensor is not None and tensor.shape != values.shape:
15+
raise ValueError(
16+
f"Parameter `{name}` should have the same shape as `values`. Found "
17+
f"`{name}.shape = {tuple(tensor.shape)}` and `values.shape = {tuple(values.shape)}`."
18+
)

0 commit comments

Comments
 (0)