From 72f338136d0c622223ef87b472c023eb56d801a3 Mon Sep 17 00:00:00 2001 From: lcy <1539275856@qq.com> Date: Thu, 9 Nov 2023 10:42:37 +0800 Subject: [PATCH] add docstring for mi_fgsm --- foolbox/attacks/mi_fgsm.py | 39 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/foolbox/attacks/mi_fgsm.py b/foolbox/attacks/mi_fgsm.py index 623f2123..65008b50 100644 --- a/foolbox/attacks/mi_fgsm.py +++ b/foolbox/attacks/mi_fgsm.py @@ -13,7 +13,15 @@ class GDMOptimizer(Optimizer): - # create GD optimizer with momentum + """Momentum-based Gradient Descent Optimizer + + Args: + x : Optimization variable for initialization of accumulation grad + stepsize : Stepsize for gradient descent + momentum : Momentum factor for accumulation grad + normalize_fn : Function to normalize the gradient + """ + def __init__( self, x: ep.Tensor, @@ -32,6 +40,16 @@ def __call__(self, gradient: ep.Tensor) -> ep.Tensor: class L1MomentumIterativeFastGradientMethod(L1BasicIterativeAttack): + """L1 Momentum Iterative Fast Gradient Sign Method (MI-FGSM) [#Dong18] + + Args: + momentum : Momentum factor for accumulation grad + rel_stepsize : Stepsize relative to epsilon + abs_stepsize : If given, it takes precedence over rel_stepsize. + steps : Number of update steps to perform. + random_start : Whether the perturbation is initialized randomly or starts at zero. + """ + def __init__( self, *, @@ -56,6 +74,16 @@ def get_optimizer(self, x: ep.Tensor, stepsize: float) -> Optimizer: class L2MomentumIterativeFastGradientMethod(L2BasicIterativeAttack): + """L2 Momentum Iterative Fast Gradient Sign Method (MI-FGSM) [#Dong18] + + Args: + momentum : Momentum factor for accumulation grad + rel_stepsize : Stepsize relative to epsilon + abs_stepsize : If given, it takes precedence over rel_stepsize. + steps : Number of update steps to perform. + random_start : Whether the perturbation is initialized randomly or starts at zero. + """ + def __init__( self, *, @@ -80,7 +108,14 @@ def get_optimizer(self, x: ep.Tensor, stepsize: float) -> Optimizer: class LinfMomentumIterativeFastGradientMethod(LinfBasicIterativeAttack): - """Momentum Iterative Fast Gradient Sign Method (MI-FGSM) [#Dong18] + """Linf Momentum Iterative Fast Gradient Sign Method (MI-FGSM) [#Dong18] + + Args: + momentum : Momentum factor for accumulation grad + rel_stepsize : Stepsize relative to epsilon + abs_stepsize : If given, it takes precedence over rel_stepsize. + steps : Number of update steps to perform. + random_start : Whether the perturbation is initialized randomly or starts at zero. References: .. [#Dong18] Dong Y, Liao F, Pang T, et al. Boosting adversarial attacks with momentum[ C]//Proceedings of the IEEE conference on computer vision and pattern recognition. 2018: 9185-9193.