Skip to content

TensorFlow Implementation of SPSA seems to calculate gradient incorrectly #1087

@cassidylaidlaw

Description

@cassidylaidlaw

In the SPSAAdam class in cleverhans/attacks/spsa.py, the gradient is calculated with the following code:

  delta = self._delta
  delta_x = self._get_delta(x, delta)
  delta_x = tf.concat([delta_x, -delta_x], axis=0)
  loss_vals = tf.reshape(
      loss_fn(x + delta_x),
      [2 * self._num_samples] + [1] * (len(x_shape) - 1))
  avg_grad = reduce_mean(loss_vals * delta_x, axis=0) / delta

I believe the last line should instead be

  avg_grad = reduce_mean(loss_vals * tf.sign(delta_x), axis=0) / delta

or equivalently

  avg_grad = reduce_mean(loss_vals / delta_x, axis=0)

to match the description in the paper. Without this, the gradient is multiplied by delta, which is by default 0.01. Thus, the gradient is underestimated by a factor of 100. I confirmed this while working on the PyTorch implementation, which I changed to use sign(delta_x); it then estimated the gradients correctly.

This issue is also present in cleverhans/future/tf2/attacks/spsa.py. It looks like it was originally implemented by @juesato.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions