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$
    – user9947
    Mar 11, 2020 at 17:48

1 Answer 1


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
import jax.numpy as jnp
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 jnp.maximum(0, 1 - y_hat * y)

hinge_loss_grad = jax.grad(hinge_loss)
# hinge_loss_grad takes an x, y, theta and returns gradient of hinge loss wrt x

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .