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?)

  • $\begingroup$ 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). $\endgroup$ – DuttaA Mar 11 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)

hinge_loss_grad = grad(hinge_loss)
# hinge_loss_grad takes an x, y, theta and returns gradient of hinge loss wrt x
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.