skwdro.base.costs_torch module#
API
Here you can fin the API for all available cost functionals in the library.
They are used to define the ground cost for transporting probability mass from sample \(\xi\) in the dataset to \(\zeta\) an “adversarial” sample generated by SkWDRO.
They all subclass the BaseCost class, and so should your custom implementations if you wish to build one from scratch.
Most users will want by default to turn to norm-powers if their problem lies in a Hilbert space.
They may then wish to turn to the helper functions cost_from_str().
- skwdro.base.costs_torch.Cost
alias of
TorchCost
- class skwdro.base.costs_torch.NormCost(p: float = 1.0, power: float = 1.0, name: str | None = None)[source]
Bases:
TorchCostp-norm to some power, with torch arguments
- solve_max_series_exp(xi: Tensor, xi_labels: Tensor, rhs: Tensor, rhs_labels: Tensor) Tuple[Tensor, Tensor][source]
- solve_max_series_exp(xi: Tensor, xi_labels: Tensor | None, rhs: Tensor, rhs_labels: Tensor | None) Tuple[Tensor, Tensor | None]
Override this method to provide an explicit solution to the expansion of the inner supremum one would wish to solve if they were solving the usual WDRO approach:
\[\zeta^\texttt{imp_samp}:=\text{arg}\min_{\zeta} \left\langle\nabla_\xi L_\theta(\xi)\mid\zeta-\xi\right\rangle - \lambda c(\xi, \zeta).\]Important
This is an unconstrained first-order approximation of the supremum, which can be ill-posed or untractable, but is usually cheap enough for efficient importance sampling. One may attempt to implement higher-order approximations and add constraints if cheap enough solutions are available, for reasonably small models, if desired.
- class skwdro.base.costs_torch.NormLabelCost(p: float = 2.0, power: float = 1.0, kappa: float = 10000.0, name: str | None = None)[source]
Bases:
NormCostp-norm of the ground metric to change data + label
Norm used to add cost to switching labels:
\[\begin{split}d_\kappa\left(\left[\begin{array}{c}\bm{X}\\y\end{array}\right], \left[\begin{array}{c}\bm{X'}\\y'\end{array}\right]\right) := \|\bm{X}-\bm{X'}\|+\kappa |y-y'|\end{split}\]- solve_max_series_exp(xi: Tensor, xi_labels: Tensor, rhs: Tensor, rhs_labels: Tensor) Tuple[Tensor, Tensor][source]
- solve_max_series_exp(xi: Tensor, xi_labels: Tensor | None, rhs: Tensor, rhs_labels: Tensor | None) Tuple[Tensor, Tensor | None]
Override this method to provide an explicit solution to the expansion of the inner supremum one would wish to solve if they were solving the usual WDRO approach:
\[\zeta^\texttt{imp_samp}:=\text{arg}\min_{\zeta} \left\langle\nabla_\xi L_\theta(\xi)\mid\zeta-\xi\right\rangle - \lambda c(\xi, \zeta).\]Important
This is an unconstrained first-order approximation of the supremum, which can be ill-posed or untractable, but is usually cheap enough for efficient importance sampling. One may attempt to implement higher-order approximations and add constraints if cheap enough solutions are available, for reasonably small models, if desired.
- class skwdro.base.costs_torch.TorchCost(name: str = '', engine: str = '')[source]
Bases:
Module,ABCBase class for transport functions
- forward(xi: Tensor, zeta: Tensor, xi_labels: Tensor | None = None, zeta_labels: Tensor | None = None) Tensor[source]
This function is called by default when using the __call__ dunder of pytorch modules: it sends directly to the
value()method.
- sampler(xi: Tensor, xi_labels: Tensor | None, epsilon: Tensor | None) Tuple[Distribution, Distribution | None][source]
- abstractmethod solve_max_series_exp(xi: Tensor, xi_labels: Tensor | None, rhs: Tensor, rhs_labels: Tensor | None) Tuple[Tensor, Tensor | None][source]
Override this method to provide an explicit solution to the expansion of the inner supremum one would wish to solve if they were solving the usual WDRO approach:
\[\zeta^\texttt{imp_samp}:=\text{arg}\min_{\zeta} \left\langle\nabla_\xi L_\theta(\xi)\mid\zeta-\xi\right\rangle - \lambda c(\xi, \zeta).\]Important
This is an unconstrained first-order approximation of the supremum, which can be ill-posed or untractable, but is usually cheap enough for efficient importance sampling. One may attempt to implement higher-order approximations and add constraints if cheap enough solutions are available, for reasonably small models, if desired.