From 1cc2720ff4ec61ee3722ca575043709cc4761bf5 Mon Sep 17 00:00:00 2001 From: Ian Goodfellow Date: Tue, 6 Feb 2018 09:27:05 -0800 Subject: [PATCH 1/2] using `generate` with float64 causes trouble --- tests_tf/test_attacks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests_tf/test_attacks.py b/tests_tf/test_attacks.py index d51269767..5d598cef3 100644 --- a/tests_tf/test_attacks.py +++ b/tests_tf/test_attacks.py @@ -173,6 +173,11 @@ def test_generate_np_gives_adversarial_example_l1(self): def test_generate_np_gives_adversarial_example_l2(self): self.help_generate_np_gives_adversarial_example(2) + def test_generate_respects_dtype(self): + x = tf.placeholder(dtype=tf.float64, shape=(100, 2)) + x_adv = self.attack.generate(x) + self.assertEqual(x_adv.dtype, tf.float64) + def test_targeted_generate_np_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) From a89f8c8354a2d5e6d550498f9d027fd1791683a0 Mon Sep 17 00:00:00 2001 From: Ian Goodfellow Date: Tue, 6 Feb 2018 09:41:52 -0800 Subject: [PATCH 2/2] make `generate` support variable dtypes --- cleverhans/attacks.py | 2 +- tests_tf/test_attacks.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cleverhans/attacks.py b/cleverhans/attacks.py index d10a56677..1254cf2c6 100644 --- a/cleverhans/attacks.py +++ b/cleverhans/attacks.py @@ -529,7 +529,7 @@ def generate(self, x, **kwargs): # Normalize current gradient and add it to the accumulated gradient red_ind = list(xrange(1, len(grad.get_shape()))) - avoid_zero_div = 1e-12 + avoid_zero_div = tf.cast(1e-12, grad.dtype) grad = grad / tf.maximum(avoid_zero_div, tf.reduce_mean(tf.abs(grad), red_ind, diff --git a/tests_tf/test_attacks.py b/tests_tf/test_attacks.py index 5d598cef3..3edc42b8e 100644 --- a/tests_tf/test_attacks.py +++ b/tests_tf/test_attacks.py @@ -28,9 +28,10 @@ class SimpleModel(Model): """ def get_logits(self, x): - W1 = tf.constant([[1.5, .3], [-2, 0.3]], dtype=tf.float32) + W1 = tf.constant([[1.5, .3], [-2, 0.3]], dtype=x.dtype) h1 = tf.nn.sigmoid(tf.matmul(x, W1)) - W2 = tf.constant([[-2.4, 1.2], [0.5, -2.3]], dtype=tf.float32) + W2 = tf.constant([[-2.4, 1.2], [0.5, -2.3]], dtype=x.dtype) + res = tf.matmul(h1, W2) return res