11from torch import Tensor
22
3- from torchjd ._linalg import normalize , regularize
3+ from torchjd ._linalg import DualConeProjector , projector_or_default
44from torchjd .linalg import PSDMatrix
55
66from ._aggregator_bases import GramianWeightedAggregator
77from ._mean import MeanWeighting
88from ._mixins import _NonDifferentiable
9- from ._utils .dual_cone import SUPPORTED_SOLVER , project_weights
109from ._utils .pref_vector import pref_vector_to_str_suffix , pref_vector_to_weighting
1110from ._weighting_bases import _GramianWeighting
1211
@@ -19,31 +18,21 @@ class DualProjWeighting(_NonDifferentiable, _GramianWeighting):
1918
2019 :param pref_vector: The preference vector to use. If not provided, defaults to
2120 :math:`\begin{bmatrix} \frac{1}{m} & \dots & \frac{1}{m} \end{bmatrix}^T \in \mathbb{R}^m`.
22- :param norm_eps: A small value to avoid division by zero when normalizing.
23- :param reg_eps: A small value to add to the diagonal of the gramian of the matrix. Due to
24- numerical errors when computing the gramian, it might not exactly be positive definite.
25- This issue can make the optimization fail. Adding ``reg_eps`` to the diagonal of the gramian
26- ensures that it is positive definite.
27- :param solver: The solver used to optimize the underlying optimization problem.
21+ :param projector: The :class:`~torchjd.linalg.DualConeProjector` used to compute the projection.
2822 """
2923
3024 def __init__ (
3125 self ,
3226 pref_vector : Tensor | None = None ,
33- norm_eps : float = 0.0001 ,
34- reg_eps : float = 0.0001 ,
35- solver : SUPPORTED_SOLVER = "proxsuite" ,
27+ projector : DualConeProjector | None = None ,
3628 ) -> None :
3729 super ().__init__ ()
3830 self .pref_vector = pref_vector
39- self .norm_eps = norm_eps
40- self .reg_eps = reg_eps
41- self .solver : SUPPORTED_SOLVER = solver
31+ self .projector = projector_or_default (projector )
4232
4333 def forward (self , gramian : PSDMatrix , / ) -> Tensor :
4434 u = self .weighting (gramian )
45- G = regularize (normalize (gramian , self .norm_eps ), self .reg_eps )
46- w = project_weights (u , G , self .solver )
35+ w = self .projector (u , gramian )
4736 return w
4837
4938 @property
@@ -56,26 +45,12 @@ def pref_vector(self, value: Tensor | None) -> None:
5645 self ._pref_vector = value
5746
5847 @property
59- def norm_eps (self ) -> float :
60- return self ._norm_eps
48+ def projector (self ) -> DualConeProjector :
49+ return self ._projector
6150
62- @norm_eps .setter
63- def norm_eps (self , value : float ) -> None :
64- if value < 0 :
65- raise ValueError (f"norm_eps must be non-negative, but got { value } ." )
66-
67- self ._norm_eps = value
68-
69- @property
70- def reg_eps (self ) -> float :
71- return self ._reg_eps
72-
73- @reg_eps .setter
74- def reg_eps (self , value : float ) -> None :
75- if value < 0 :
76- raise ValueError (f"reg_eps must be non-negative, but got { value } ." )
77-
78- self ._reg_eps = value
51+ @projector .setter
52+ def projector (self , value : DualConeProjector | None ) -> None :
53+ self ._projector = projector_or_default (value )
7954
8055
8156class DualProj (_NonDifferentiable , GramianWeightedAggregator ):
@@ -87,27 +62,18 @@ class DualProj(_NonDifferentiable, GramianWeightedAggregator):
8762
8863 :param pref_vector: The preference vector used to combine the rows. If not provided, defaults to
8964 :math:`\begin{bmatrix} \frac{1}{m} & \dots & \frac{1}{m} \end{bmatrix}^T \in \mathbb{R}^m`.
90- :param norm_eps: A small value to avoid division by zero when normalizing.
91- :param reg_eps: A small value to add to the diagonal of the gramian of the matrix. Due to
92- numerical errors when computing the gramian, it might not exactly be positive definite.
93- This issue can make the optimization fail. Adding ``reg_eps`` to the diagonal of the gramian
94- ensures that it is positive definite.
95- :param solver: The solver used to optimize the underlying optimization problem.
65+ :param projector: The :class:`~torchjd.linalg.DualConeProjector` used to compute the projection.
9666 """
9767
9868 gramian_weighting : DualProjWeighting
9969
10070 def __init__ (
10171 self ,
10272 pref_vector : Tensor | None = None ,
103- norm_eps : float = 0.0001 ,
104- reg_eps : float = 0.0001 ,
105- solver : SUPPORTED_SOLVER = "proxsuite" ,
73+ projector : DualConeProjector | None = None ,
10674 ) -> None :
107- self ._solver : SUPPORTED_SOLVER = solver
108-
10975 super ().__init__ (
110- DualProjWeighting (pref_vector , norm_eps = norm_eps , reg_eps = reg_eps , solver = solver ),
76+ DualProjWeighting (pref_vector , projector = projector ),
11177 )
11278
11379 @property
@@ -119,25 +85,17 @@ def pref_vector(self, value: Tensor | None) -> None:
11985 self .gramian_weighting .pref_vector = value
12086
12187 @property
122- def norm_eps (self ) -> float :
123- return self .gramian_weighting .norm_eps
124-
125- @norm_eps .setter
126- def norm_eps (self , value : float ) -> None :
127- self .gramian_weighting .norm_eps = value
128-
129- @property
130- def reg_eps (self ) -> float :
131- return self .gramian_weighting .reg_eps
88+ def projector (self ) -> DualConeProjector :
89+ return self .gramian_weighting .projector
13290
133- @reg_eps .setter
134- def reg_eps (self , value : float ) -> None :
135- self .gramian_weighting .reg_eps = value
91+ @projector .setter
92+ def projector (self , value : DualConeProjector | None ) -> None :
93+ self .gramian_weighting .projector = value
13694
13795 def __repr__ (self ) -> str :
13896 return (
139- f"{ self .__class__ .__name__ } (pref_vector={ repr (self .pref_vector )} , norm_eps ="
140- f"{ self . norm_eps } , reg_eps= { self . reg_eps } , solver= { repr (self ._solver )} )"
97+ f"{ self .__class__ .__name__ } (pref_vector={ repr (self .pref_vector )} , projector ="
98+ f"{ repr (self .projector )} )"
14199 )
142100
143101 def __str__ (self ) -> str :
0 commit comments