Skip to content

Commit

Permalink
Fix docs build
Browse files Browse the repository at this point in the history
Signed-off-by: Beat Buesser <[email protected]>
  • Loading branch information
Beat Buesser committed Jun 15, 2020
1 parent b8a8c8c commit 0ac8660
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from typing import Optional, TYPE_CHECKING

import numpy as np
import torch

from art.config import ART_NUMPY_DTYPE
from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent_numpy import (
Expand Down Expand Up @@ -121,6 +120,8 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
:type mask: `np.ndarray`
:return: An array holding the adversarial examples.
"""
import torch

# Check whether random eps is enabled
self._random_eps()

Expand Down Expand Up @@ -232,6 +233,8 @@ def _compute_perturbation(self, x: "torch.Tensor", y: "torch.Tensor", mask: "tor
perturbed.
:return: Perturbations.
"""
import torch

# Pick a small scalar to avoid division by 0
tol = 10e-8

Expand Down Expand Up @@ -266,6 +269,8 @@ def _apply_perturbation(self, x: "torch.Tensor", perturbation: "torch.Tensor", e
:param eps_step: Attack step size (input variation) at each iteration.
:return: Adversarial examples.
"""
import torch

x = x + eps_step * perturbation

if self.estimator.clip_values is not None:
Expand Down Expand Up @@ -302,6 +307,8 @@ def _compute_torch(
original input.
:return: Adversarial examples.
"""
import torch

if random_init:
n = x.shape[0]
m = np.prod(x.shape[1:])
Expand Down Expand Up @@ -344,6 +351,8 @@ def _projection(self, values: "torch.Tensor", eps: float, norm_p: int) -> "torch
:param norm_p: L_p norm to use for clipping supporting 1, 2 and `np.Inf`.
:return: Values of `values` after projection.
"""
import torch

# Pick a small scalar to avoid division by 0
tol = 10e-8
values_tmp = values.reshape(values.shape[0], -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from typing import Optional, TYPE_CHECKING

import numpy as np
import tensorflow as tf

from art.config import ART_NUMPY_DTYPE
from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent_numpy import (
Expand Down Expand Up @@ -120,6 +119,8 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
:type mask: `np.ndarray`
:return: An array holding the adversarial examples.
"""
import tensorflow as tf

# Check whether random eps is enabled
self._random_eps()

Expand Down Expand Up @@ -222,6 +223,8 @@ def _compute_perturbation(self, x: "tf.Tensor", y: "tf.Tensor", mask: "tf.Tensor
perturbed.
:return: Perturbations.
"""
import tensorflow as tf

# Pick a small scalar to avoid division by 0
tol = 10e-8

Expand Down Expand Up @@ -256,6 +259,8 @@ def _apply_perturbation(self, x: "tf.Tensor", perturbation: "tf.Tensor", eps_ste
:param eps_step: Attack step size (input variation) at each iteration.
:return: Adversarial examples.
"""
import tensorflow as tf

x = x + eps_step * perturbation

if self.estimator.clip_values is not None:
Expand Down Expand Up @@ -292,6 +297,8 @@ def _compute_tf(
original input.
:return: Adversarial examples.
"""
import tensorflow as tf

if random_init:
n = x.shape[0]
m = np.prod(x.shape[1:])
Expand Down Expand Up @@ -334,6 +341,8 @@ def _projection(values: "tf.Tensor", eps: float, norm_p: int) -> "tf.Tensor":
:param norm_p: L_p norm to use for clipping supporting 1, 2 and `np.Inf`.
:return: Values of `values` after projection.
"""
import tensorflow as tf

# Pick a small scalar to avoid division by 0
tol = 10e-8
values_tmp = tf.reshape(values, (values.shape[0], -1))
Expand Down
5 changes: 2 additions & 3 deletions art/defences/trainer/adversarial_trainer_FBF_Pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import time

import numpy as np
import torch
import torch.nn as nn

from art.config import ART_NUMPY_DTYPE
from art.defences.trainer.adversarial_trainer_FBF import AdversarialTrainerFBF
Expand Down Expand Up @@ -200,6 +198,7 @@ def _batch_process(self, x_batch, y_batch, lr):
:type lr: `float`
:return: `(float, float, float)`
"""
import torch

n = x_batch.shape[0]
m = np.prod(x_batch.shape[1:])
Expand Down Expand Up @@ -239,7 +238,7 @@ def _batch_process(self, x_batch, y_batch, lr):
loss.backward()

# clip the gradients
nn.utils.clip_grad_norm_(self._classifier._model.parameters(), 0.5)
torch.nn.utils.clip_grad_norm_(self._classifier._model.parameters(), 0.5)
self._classifier._optimizer.step()

train_loss = loss.item() * o_batch.size(0)
Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"sphinx_rtd_theme",
"sphinx-autodoc-annotation",
"sphinx-autodoc-typehints",
"matplotlib",
"numpy",
"scipy",
"six==1.13.0",
"scikit-learn==0.22.1",
"Pillow>=6.0.0",
]


Expand Down
4 changes: 4 additions & 0 deletions tests/defences/test_adversarial_trainer_FBF.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ def test_adversarial_trainer_FBF_Pytorch_fit_and_predict(get_adv_trainer, fix_ge

np.testing.assert_array_almost_equal(accuracy, 0.32, decimal=0.001)
np.testing.assert_array_almost_equal(accuracy_new, 0.14, decimal=0.001)


if __name__ == "__main__":
pytest.cmdline.main("-q -s {} --mlFramework=pytorch --durations=0".format(__file__).split(" "))

0 comments on commit 0ac8660

Please sign in to comment.