advertrain.dependencies.cleverhans package
Submodules
advertrain.dependencies.cleverhans.fast_gradient_method module
Taken from https://github.com/rwightman/pytorch-image-models
The Fast Gradient Method attack.
MIT License
- advertrain.dependencies.cleverhans.fast_gradient_method.fast_gradient_method(model_fn, x: Tensor, eps: float, norm: int, clip_min: float | None = None, clip_max: float | None = None, y: Tensor | None = None, targeted: bool = False, sanity_checks: bool = False) Tensor [source]
PyTorch implementation of the Fast Gradient Method (FGM).
- Parameters:
model_fn – A callable that takes an input tensor and returns the model logits.
x (torch.Tensor) – Input tensor.
eps (float) – Epsilon, the input variation parameter.
norm (int) – Order of the norm (np.inf, 1, or 2).
clip_min (float, optional) – Mininum value per input dimension.
clip_max (float, optional) – Maximum value per input dimension.
y (torch.Tensor, optional) – Labels or target labels for targeted attack.
targeted (bool) – Whether to perform a targeted attack or not.
sanity_checks (bool) – If True, include sanity checks.
- Returns:
A tensor containing the adversarial examples.
- Return type:
torch.Tensor
advertrain.dependencies.cleverhans.projected_gradient_descent module
Taken from https://github.com/rwightman/pytorch-image-models
The Projected Gradient Descent attack.
MIT License
- advertrain.dependencies.cleverhans.projected_gradient_descent.projected_gradient_descent(model_fn, x: Tensor, eps: float, eps_iter: float, nb_iter: int, norm: int, clip_min: float | None = None, clip_max: float | None = None, y: Tensor | None = None, targeted: bool = False, rand_init: bool = True, rand_minmax: float | None = None, sanity_checks: bool = True) Tensor [source]
Performs the Projected Gradient Descent attack.
- Parameters:
model_fn – A callable that takes an input tensor and returns the model logits.
x (torch.Tensor) – Input tensor.
eps (float) – Epsilon, the input variation parameter.
eps_iter (float) – Step size for each attack iteration.
nb_iter (int) – Number of attack iterations.
norm (int) – Order of the norm (np.inf, 1, or 2).
clip_min (float, optional) – Mininum value per input dimension.
clip_max (float, optional) – Maximum value per input dimension.
y (torch.Tensor, optional) – Labels or target labels for targeted attack.
targeted (bool) – Whether to perform a targeted attack or not.
rand_init (bool) – Whether to start from a randomly perturbed input.
rand_minmax (float, optional) – Range of the uniform distribution for initial random perturbation.
sanity_checks (bool) – If True, include sanity checks.
- Returns:
A tensor containing the adversarial examples.
- Return type:
torch.Tensor
advertrain.dependencies.cleverhans.utils module
Taken from https://github.com/rwightman/pytorch-image-models
MIT License
- advertrain.dependencies.cleverhans.utils.clip_eta(eta: Tensor, norm: int, eps: float) Tensor [source]
Clips the perturbation eta to be within the specified norm ball.
- Parameters:
eta (torch.Tensor) – The perturbation tensor.
norm (int) – The norm to use.
eps (float) – Epsilon, the maximum allowed norm of the perturbation.
- Returns:
The clipped perturbation.
- Return type:
torch.Tensor
- advertrain.dependencies.cleverhans.utils.optimize_linear(grad: Tensor, eps: float, norm: int = inf) Tensor [source]
Solves for the optimal input to a linear function under a norm constraint.
- Parameters:
grad (torch.Tensor) – Tensor of gradients.
eps (float) – Epsilon, the maximum allowed norm of the perturbation.
norm (int) – The norm to use.
- Returns:
The optimized perturbation.
- Return type:
torch.Tensor