skwdro.base.cost_decoder module#
API
This module exposes mainly the cost_from_str() function, which translates strings with the following grammar to a cost class inheriting Cost.
1// Entry point
2spec: engine DASH type DASH FLOAT DASH FLOAT kappa? ;
3
4DASH: '-' ;
5
6FLOAT: .* ; // Python-parseable positive floating point number
7
8// NC for simple p-powered k-norm cost
9// NLC for same with a penalization of label switches with weight kappa
10type: 'NC' | 'NLC' ;
11
12kappa: DASH FLOAT ; // Python-parseable positive floating point number
- class skwdro.base.losses_torch.LogisticLoss(sampler: LabeledSampler, *, d: int = 0, l2reg: float | None = None, fit_intercept: bool = False)[source]
Bases:
LossLogisic regression with classes \(\{-1, 1\}\)
- Parameters:
- samplerOptional[LabeledSampler]
sampler for the adversarial samples
- dint
dimension of the data (
xi.size(-1))- fit_interceptbool
model has an affine dimension
- Attributes:
- Lnn.SoftMarginLoss
torch solution to implement the soft margin in
]-1, 1[- classifnn.Tanh
activation function to project tensor in
]-1, 1[- linearnn.Linear
linear combination containing the relevant parameters
- classmethod default_sampler(xi, xi_labels, epsilon, seed: int | None) BaseSampler[source]
Overload this method if you wish to define a default sampler associated to your loss.
- property intercept: Tensor
- predict(X: Tensor) Tensor[source]
Predict the label of the argument tensor
- Parameters:
- self
self
- Xpt.Tensor
X
- Returns:
- pt.Tensor
- property theta: Tensor
- class skwdro.base.losses_torch.Loss(sampler: BaseSampler | None, has_labels: bool, *, l2reg: float | None = None, xi: Tensor | None = None, xi_labels: Tensor | None = None, sigma: float | None = None)[source]
Bases:
Module,ABCBase class for loss functions
- abstractmethod classmethod default_sampler(xi, xi_labels, epsilon, seed: int | None) BaseSampler | None[source]
Overload this method if you wish to define a default sampler associated to your loss.
- forward(*args)[source]
Alias for
value(), for consistency with usual torch api.
- has_labels: bool
- abstract property intercept
- regularize(loss: Tensor) Tensor[source]
Returns the regularized loss, used in the value function. Adds a new term \(\frac{1}{2}\|\theta\|_2^2\)
- property sampler: BaseSampler
- abstract property theta
- value(xi: Tensor, xi_labels: Tensor) Tensor[source]
- value(xi: Tensor, xi_labels: None) Tensor
Perform forward pass. Overload the method to implement your own.
- value_old(theta, xi)[source]
DEPRECATED, DO NOT USE
- skwdro.base.losses_torch.NewsVendorLoss
alias of
NewsVendorLoss_torch
- class skwdro.base.losses_torch.NewsVendorLoss_torch(sampler: NoLabelsSampler, *, k: float = 5, u: float = 7, l2reg: float | None = None, name: str | None = 'NewsVendor loss')[source]
Bases:
LossLoss associated with the newsvendor problem:
\[k\theta - \mathbb{E}[u\min(\theta, \xi)]\]- Parameters:
- samplerOptional[NoLabelsSampler]
optional sampler to use for the demand
- kint
journal price
- uint
benefit per journal sold
- name
name of the loss, optional
- Attributes:
- samplerNoLabelsSampler
- knn.Parameter
- unn.Parameter
- thetann.Parameter
number of journal stocked
- nameOptional[str]
- classmethod default_sampler(xi, xi_labels, epsilon, seed: int | None) NewsVendorNormalSampler[source]
Overload this method if you wish to define a default sampler associated to your loss.
- property intercept: None
- property theta: Tensor
- value(xi: Tensor, xi_labels: Tensor | None = None) Tensor[source]
Forward pass of the loss on the data
- Parameters:
- xipt.Tensor
empirical observations of demand
- xi_labelsNoneType
placeholder, do not touch
- value_old(theta, xi)[source]
DEPRECATED, DO NOT USE
- class skwdro.base.losses_torch.QuadraticLoss(sampler: LabeledSampler, *, d: int = 0, l2reg: float | None = None, fit_intercept: bool = False)[source]
Bases:
Loss- classmethod default_sampler(xi, xi_labels, epsilon, seed: int | None) BaseSampler[source]
Overload this method if you wish to define a default sampler associated to your loss.
- property intercept: Tensor
- regression(X) Tensor[source]
- property theta: Tensor
- class skwdro.base.losses_torch.SimpleWeber(d: int)[source]
Bases:
Module- forward(xi: Tensor, xi_labels: Tensor) Tensor[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- reduction = 'none'
- class skwdro.base.losses_torch.WrappedPrimalLoss(loss: Module | Callable[[...], Tensor], transform: Module | None, sampler: BaseSampler, has_labels: bool, reduce_spatial_dims: bool = True, *, l2reg: float | None = None)[source]
Bases:
Loss- classmethod default_sampler(xi, xi_labels, epsilon, seed: int | None) BaseSampler[source]
Overload this method if you wish to define a default sampler associated to your loss.
- has_labels: bool
- property intercept
- loss_oop_interface: bool = True
- reduce_spatial_dims: bool = True
- property theta