Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF2] spsa #1158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cleverhans/future/tf2/attacks/spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def spsa(model_fn, x, y, eps, nb_iter, clip_min=None, clip_max=None, targeted=Fa
is on `spsa_samples` different inputs.
:param is_debug: If True, print the adversarial loss after each update.
"""
if x.get_shape().as_list()[0] != 1:

if tf.is_tensor(x) is False:
x = tf.convert_to_tensor(x)

if tf.shape(x)[0] != 1:
raise ValueError("For SPSA, input tensor x must have batch_size of 1.")

optimizer = SPSAAdam(lr=learning_rate, delta=delta, num_samples=spsa_samples,
Expand All @@ -44,7 +48,7 @@ def loss_fn(x, label):
"""
Margin logit loss, with correct sign for targeted vs untargeted loss.
"""
logits = model_fn(x)
logits = model_fn(x, training=False)
loss_multiplier = 1 if targeted else -1
return loss_multiplier * margin_logit_loss(logits, label, nb_classes=logits.get_shape()[-1])

Expand Down Expand Up @@ -108,7 +112,7 @@ def body(i, grad_array):
loss_vals = tf.reshape(
loss_fn(x + delta_x),
[2 * self._num_samples] + [1] * (len(x_shape) - 1))
avg_grad = tf.reduce_mean(loss_vals * delta_x, axis=0) / delta
avg_grad = tf.reduce_mean(loss_vals / delta_x, axis=0)
avg_grad = tf.expand_dims(avg_grad, axis=0)
new_grad_array = grad_array.write(i, avg_grad)
return i + 1, new_grad_array
Expand Down