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