def get_gradient_sign_tf(x, predictions):
"""
TensorFlow implementation of calculting signed gradient with respect to x.
:param x: the input placeholder
:param predictions: the model's output tensor
:return: a tensor for the adversarial example
"""
# Compute loss
y = tf.to_float(tf.equal(predictions, tf.reduce_max(predictions, 1, keep_dims=True)))
y = y / tf.reduce_sum(y, 1, keep_dims=True)
loss = utils_tf.tf_model_loss(y, predictions, mean=False)
# Define gradient of loss wrt input
grad, = tf.gradients(loss, x)
# Take sign of gradient
signed_grad = tf.sign(grad)
signed_grad = tf.stop_gradient(signed_grad)
return signed_grad
评论列表
文章目录