Compute the gradient of the SVM loss function
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Support Vector Machines (SVM) are powerful supervised learning models used for classification and regression tasks. One of the critical steps in training an SVM involves optimizing the loss function. The goal is to find the hyperplane that best separates the different classes of data. The process of optimization is considerably accelerated through the use of gradients. In particular, computing the gradient of the SVM loss function is an essential technique.
SVM Loss Function
The typical SVM loss function is characterized by the hinge loss and a regularization term. For a binary classification problem, it can be represented as:
where: • represents the weight vector. • is the bias term. • is the regularization parameter. • is the number of training samples. • is the true label for the sample, which takes values of either +1 or -1. • is the feature vector for the sample.
The hinge loss part, , penalizes misclassifications.
Gradient of the SVM Loss Function
The gradient descent method is used to minimize the loss function, and computing the gradient is essential for updating the model parameters (weights and biases). The loss function here is not differentiable everywhere, particularly due to the hinge loss. However, sub-gradient techniques are employed for optimization.
Gradient with Respect to
The gradient of the loss function with respect to the weight vector is given by:
where is the indicator function, which returns 1 when the argument is true and 0 otherwise.
Gradient with Respect to
The gradient with respect to the bias term is:
The gradients are used in an iterative process to adjust the weights and biases such that the SVM's decision boundary is fine-tuned to minimize the loss.
Gradient Descent Algorithm
The complete gradient descent update rules for the SVM training process can be summarized as follows:
- Initialize: and (usually with zeros or small random values).
- Iterate: • Compute gradients: and . • Update weights: . • Update bias: .
- Convergence Check: Continue until convergence criteria are met, such as a maximum number of iterations or the magnitudes of gradients fall below a threshold.
The parameter is the learning rate, which controls the step size in each iteration.
Regularization and Loss Function
Regularization is essential to prevent overfitting by penalizing large weights. The regularization term () ensures the model enjoys the property of maximum-margin separation while not becoming complex.
| Term | Description |
| Weight vector. | |
| Bias term. | |
| Regularization parameter. | |
| Number of training samples. | |
| Feature vector of the sample. | |
| Label (+1 or -1) of the sample. | |
| Total loss function. | |
| Gradient with respect to weights. | |
| Gradient with respect to bias. |
Conclusion
The gradient of the SVM loss function is crucial in optimizing the hyperplane separating different classes in the dataset. Understanding the role of different components, from hinge loss to regularization, as well as the practical steps for computing gradients, allows us to effectively train SVM models. By following a structured approach to gradient computation and parameter updates, SVMs can effectively generalize from training data to make accurate predictions on new, unseen data.
Related reading
- Computing similarity between two lists
- Computing TF-IDF on the whole dataset or only on training data?
- Concatenate custom features with CountVectorizer
- Concatenate two models with tensorflow.keras
- Computing circle intersections in O ns log n
- Computing mid in Interpolation Search?
- Concept of getter in TensorFlow
- Conditional assignment of tensor values in TensorFlow

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.