Answers.Body: <p>A way to avoid computing the SVM loss by hand is to use a differentiable programming framework, such as <a href="https://github.com/google/jax" rel="nofollow noreferrer">JAX</a>. These frameworks will automatically calculate gradients using automatic differentiation.</p>
  <p>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.</p>
  <p>In JAX this would look like:</p>
  <pre><code>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
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-14T02:47:06.440Z
Answers.LastActivityDate: 2022-11-29T18:18:03.630Z
Answers.LastEditDate: 2022-11-29T18:18:03.630Z
Answers.UserDisplayName: Chris Cundy
Body: <p>I have an SVM currently and want to perform a gradient based attack on it similar to FGSM discussed in <a href="https://arxiv.org/pdf/1412.6572.pdf" rel="nofollow noreferrer">Explaining And Harnessing
  Adversarial Examples</a>.</p>
  
  <p><a href="https://i.sstatic.net/m4xA5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/m4xA5.png" alt="SimplePic"></a>  </p>
  
  <p>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).</p>
  
  <p>Is there a way to avoid the maths (I am working in python if that helps?)</p>
  
Tags: <objective-functions><gradient-descent><papers><support-vector-machine><adversarial-ml>
Title: How do you perform a gradient based adversarial attack on an SVM based model?
UserDisplayName: FeedMeInformation
ThreadText: # How do you perform a gradient based adversarial attack on an SVM based model?
  
  (Asked by: username_0 on 2020-03-11)
  
  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](https://arxiv.org/pdf/1412.6572.pdf).
  
  [![SimplePic](https://i.sstatic.net/m4xA5.png)](https://i.sstatic.net/m4xA5.png)
  
  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?)
  
  **Tags:** objective-functions, gradient-descent, papers, support-vector-machine, adversarial-ml
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-14. Score: 2
  
  A way to avoid computing the SVM loss by hand is to use a differentiable programming framework, such as [JAX](https://github.com/google/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
  
  ```
  
  
  ---
  
  