secmlt.optimization package#
Submodules#
secmlt.optimization.constraints module#
Constraints for tensors and the corresponding batch-wise projections.
- class secmlt.optimization.constraints.ClipConstraint(lb: float = 0.0, ub: float = 1.0)[source]#
Bases:
ConstraintBox constraint, usually for the input space.
- class secmlt.optimization.constraints.InputSpaceConstraint(preprocessing: DataProcessing)[source]#
Bases:
Constraint,ABCInput space constraint.
Reverts the preprocessing, applies the constraint, and re-applies the preprocessing.
- class secmlt.optimization.constraints.L0Constraint(radius: float | Tensor = 0.0)[source]#
Bases:
LpConstraintL0 constraint.
- class secmlt.optimization.constraints.L1Constraint(radius: float | Tensor = 0.0)[source]#
Bases:
LpConstraintL1 constraint.
- project(x: Tensor) Tensor[source]#
Compute Euclidean projection onto the L1 ball for a batch.
Source: https://gist.github.com/tonyduan/1329998205d88c566588e57e3e2c0c55
min ||x - u||_2 s.t. ||u||_1 <= eps
Inspired by the corresponding numpy version by Adrien Gaidon.
- Parameters:
x (torch.Tensor) – Input tensor.
- Returns:
Projected tensor.
- Return type:
torch.Tensor
Notes
The complexity of this algorithm is in O(dlogd) as it involves sorting x.
References
- [1] Efficient Projections onto the l1-Ball for Learning in High Dimensions
John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra. International Conference on Machine Learning (ICML 2008)
- class secmlt.optimization.constraints.L2Constraint(radius: float | Tensor = 0.0)[source]#
Bases:
LpConstraintL2 constraint.
- class secmlt.optimization.constraints.LInfConstraint(radius: float | Tensor = 0.0)[source]#
Bases:
LpConstraintLinf constraint.
- class secmlt.optimization.constraints.LpConstraint(radius: float | Tensor = 0.0, p: str = 'linf')[source]#
Bases:
Constraint,ABCAbstract class for Lp constraint.
- abstract project(x: Tensor) Tensor[source]#
Project onto the Lp constraint.
- Parameters:
x (torch.Tensor) – Input tensor.
- Returns:
Tensor projected onto the Lp constraint.
- Return type:
torch.Tensor
- property radius: Tensor#
Get radius of the constraint.
- class secmlt.optimization.constraints.MaskConstraint(mask: Tensor)[source]#
Bases:
ConstraintConstraint for keeping components only on the given mask.
- class secmlt.optimization.constraints.QuantizationConstraint(preprocessing: DataProcessing = None, levels: list[float] | torch.Tensor | int = 255)[source]#
Bases:
InputSpaceConstraintConstraint for ensuring quantized outputs into specified levels.
secmlt.optimization.gradient_processing module#
Processing functions for gradients.
- class secmlt.optimization.gradient_processing.GradientProcessing[source]#
Bases:
ABCGradient processing base class.
- class secmlt.optimization.gradient_processing.LinearProjectionGradientProcessing(perturbation_model: str = 'l2')[source]#
Bases:
GradientProcessingLinear projection of the gradient onto Lp balls.
secmlt.optimization.initializer module#
Initializers for the attacks.
- class secmlt.optimization.initializer.Initializer[source]#
Bases:
objectInitialization for the perturbation delta.
- class secmlt.optimization.initializer.RandomLpInitializer(radius: Tensor, perturbation_model: LpPerturbationModels)[source]#
Bases:
InitializerRandom perturbation initialization in Lp ball.
secmlt.optimization.losses module#
Difference of Logits Loss.
- class secmlt.optimization.losses.LogitDifferenceLoss(weight: Tensor | None = None, reduction: str = 'mean')[source]#
Bases:
_WeightedLossImplements the Difference of Logits Loss.
- forward(input: Tensor, target: Tensor) Tensor[source]#
Compute the difference between input and target logits.
The loss is defined as:
\[\mathcal{L}(x, y) = -(z_y - \max_{j \ne y} z_j)\]where:
\(z\) are the model’s output logits for input \(x\),
\(y\) is the true class index,
\(z_y\) is the logit corresponding to the true class,
\(\max_{j \ne y} z_j\) is the highest logit among incorrect classes.
This loss encourages the logit of the true class to be greater than that of any other class, and is particularly useful in adversarial attack settings like FMN.
secmlt.optimization.optimizer_factory module#
Optimizer creation tools.
- class secmlt.optimization.optimizer_factory.OptimizerFactory[source]#
Bases:
objectCreator class for optimizers.
- OPTIMIZERS: ClassVar[dict[str, torch.optim.Optimizer]] = {'adam': <class 'torch.optim.adam.Adam'>, 'sgd': <class 'torch.optim.sgd.SGD'>}#
- static create_adam(lr: float) partial[Adam][source]#
Create the Adam optimizer.
- Parameters:
lr (float) – Learning rate.
- Returns:
Adam optimizer.
- Return type:
functools.partial[Adam]
- static create_from_name(optimizer_name: str, lr: float, **kwargs) partial[Adam] | partial[SGD][source]#
Create an optimizer.
- Parameters:
optimizer_name (str) – One of the available optimizer names. Available: adam, sgd.
lr (float) – Learning rate.
- Returns:
The created optimizer.
- Return type:
functools.partial[Adam] | functools.partial[SGD]
- Raises:
ValueError – Raises ValueError when the requested optimizer is not in the list of implemented optimizers.
secmlt.optimization.random_perturb module#
Random pertubations in Lp balls.
- class secmlt.optimization.random_perturb.RandomPerturb(p: str, epsilon: float)[source]#
Bases:
objectRandom perturbation creator.
- class secmlt.optimization.random_perturb.RandomPerturbBase(epsilon: float)[source]#
Bases:
ABCClass implementing the random perturbations in Lp balls.
- class secmlt.optimization.random_perturb.RandomPerturbL0(epsilon: float)[source]#
Bases:
RandomPerturbBaseRandom Perturbations for L0 norm.
- class secmlt.optimization.random_perturb.RandomPerturbL1(epsilon: float)[source]#
Bases:
RandomPerturbBaseRandom Perturbations for L1 norm.
- class secmlt.optimization.random_perturb.RandomPerturbL2(epsilon: float)[source]#
Bases:
RandomPerturbBaseRandom Perturbations for L2 norm.
- class secmlt.optimization.random_perturb.RandomPerturbLinf(epsilon: float)[source]#
Bases:
RandomPerturbBaseRandom Perturbations for Linf norm.
secmlt.optimization.scheduler_factory module#
Learning Rate Schedulers creation tools.
- class secmlt.optimization.scheduler_factory.LRSchedulerFactory[source]#
Bases:
objectCreator class for learning rate schedulers.
- SCHEDULERS: ClassVar[dict[str, _LRScheduler]] = {'cosine_annealing': <class 'torch.optim.lr_scheduler.CosineAnnealingLR'>, 'no_scheduler': <class 'secmlt.optimization.scheduler_factory.NoScheduler'>}#
- static create_cosine_annealing() partial[_LRScheduler][source]#
Create the Cosine Annealing scheduler.
- Returns:
Cosine Annealing scheduler.
- Return type:
functools.partial[LRScheduler]
- static create_from_name(scheduler_name: str, **kwargs) partial[_LRScheduler][source]#
Create a learning rate scheduler.
- Parameters:
scheduler_name (str) – One of the available scheduler names. Available: cosine.
- Returns:
The created scheduler.
- Return type:
functools.partial[LRScheduler]
- Raises:
ValueError – Raises ValueError when the requested scheduler is not in the list of implemented schedulers.
Module contents#
Optimization functionalities.