# How do you perform a gradient based adversarial attack on an SVM based model?

I have an SVM currently and want to perform a gradient based attack on it similar to FGSM discussed in Explaining And Harnessing Adversarial Examples.

I am struggling to actually calculate the gradient of the SVM cost function with respect to the input (I am assuming it needs to be w.r.t input).

Is there a way to avoid the maths (I am working in python if that helps?)

• Just check some standard implementations of this model. As far as math goes, if you want to know the model fully, you cannot sidestep it. Also judging by the author's name it will probably be difficult to understand the maths (if you don't have prior knowledge about some of the works already done in this respect).
– user9947
Mar 11 '20 at 17:48

A way to avoid computing the SVM loss by hand is to use a differentiable programming framework, such as JAX. These frameworks will automatically calculate gradients using automatic differentiation.

If you can write down the SVM loss using numpy operations then you can use the framework's tools to get a function which evaluates the gradient with respect to any argument.

In JAX this would look like:

import jax.numpy as np
def hinge_loss(x, y, theta):
# x is an nxd matrix, y is an nx1 matrix
y_hat = model(x, theta) # returns nx1 matrix, model parameters theta
return np.maximum(0, 1 - y_hat * y)