Ch.34: Soft Margin SVM and the Hinge Loss
Inspired by: YouTube
Last post ended on a warning: hard margin SVM's constraint has to hold for every point, and if the classes overlap even slightly, no satisfies it. There's no fallback, the optimization problem simply has no solution. Soft margin SVM is the fix, it lets points violate the constraint, at a cost the objective has to pay for.
1. Give Every Point Some Slack
For each training point, introduce a variable (the Greek letter xi, pronounced "chi" or often just "slack ") and relax the hard constraint to:
Read as how far point is allowed to fall short of the margin requirement:
- : the point satisfies the original hard-margin constraint, sitting on the correct side of its supporting plane, exactly like before.
- : the point is inside the margin, correctly classified but too close to the boundary, it invades the safety strip that hard margin insisted on keeping empty.
- : the point has crossed all the way to the wrong side of the decision boundary , it's actually misclassified.
Every point gets its own , and most of them stay at , slack is only "spent" on the points that actually need it to keep the problem solvable.
2. Paying for Slack in the Objective
Slack can't be free. Set to a huge number for every point and the constraint is trivially satisfiable no matter how bad are, that's not a margin classifier anymore, it's nothing. The fix is to add a penalty for total slack directly into the thing being minimized:
Two competing terms now sit in one objective:
- still wants a small , which from ch33 means a wide margin.
- wants total slack small, meaning few violations and margins that hug the data tightly.
is the hyperparameter that decides how much the second term matters relative to the first, it's the knob that turns hard margin (, no violation tolerated) into a fully relaxed classifier (, margin can grow as wide as it likes regardless of how many points end up inside it).
- Small : violations are cheap, the optimizer happily lets grow to buy a wider margin. More bias, less variance, closer to underfitting if pushed too far.
- Large : violations are expensive, the optimizer shrinks even if that means a narrower margin that hugs the training points closely. Less bias, more variance, closer to overfitting if pushed too far.
This is exactly the same bias-variance dial seen with logistic regression's regularization strength, just written as instead of , and inverted: scikit-learn's SVC(C=...) uses large for less regularization, the opposite convention from .
3. Turning Slack Into a Loss Function
The constraint combined with pins down the smallest can possibly be for a given point: either (if the constraint is already met) or exactly (if it isn't). That's precisely what computes:
Substituting this back removes and the constraints entirely, collapsing soft margin SVM into a single unconstrained objective over :
The term inside the sum, , is the hinge loss for a single point. It's zero once a point clears the margin by at least , and grows linearly the moment it doesn't, which is exactly the behavior was already tracking, this is just that same quantity written as a per-point loss function instead of a constraint.
4. Hinge Loss vs. Logistic Loss
Both hinge loss and logistic regression's log loss punish a point more the further it sits on the wrong side of the boundary, and both go to zero as the point moves confidently onto the correct side. The difference is where "confident enough" kicks in:
- Log loss never actually reaches zero, it keeps decreasing (however slightly) for any finite score, always rewarding more confidence.
- Hinge loss hits exactly zero once and stays there, a point that already clears the margin contributes nothing further to the objective, no matter how much more confidently correct it becomes.
That flat zero region is what makes SVM a margin classifier rather than a probability classifier: it only cares about pushing points out of the margin, not about squeezing every last bit of confidence out of points that are already safely classified. Once that's true for a point, hinge loss stops paying attention to it entirely.
5. Where This Leaves SVM
Soft margin SVM, in either form, the constrained quadratic program with slack variables or the unconstrained hinge-loss objective, is still a linear classifier: is a straight line (or hyperplane) no matter how is tuned. Overlapping classes that soft margin merely tolerates are one problem; classes that no straight line could ever separate, even loosely, are a different one entirely. That's what the kernel trick exists to solve, and it's the next post.
