skwdro.base.losses_torch module#
API
Here you can fin the API for all available losses in the library.
They all subclass the base Loss class, and so should your custom implementations if you wish to build one from scratch.
In order to make your own loss, you must overload Loss.value() and probably Loss.default_sampler()
- 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
- l2reg: Tensor | None
- 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