Machine Learning Algorithms

Ch.34: Soft Margin SVM and the Hinge Loss

By Ayush Arora6 min read

Inspired by: YouTube

Last post ended on a warning: hard margin SVM's constraint yi(wxi+b)1y_i(w \cdot x_i + b) \geq 1 has to hold for every point, and if the classes overlap even slightly, no w,bw, b 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 ξi0\xi_i \geq 0 (the Greek letter xi, pronounced "chi" or often just "slack ii") and relax the hard constraint to:

yi(wxi+b)1ξiy_i(w \cdot x_i + b) \geq 1 - \xi_i

Read ξi\xi_i as how far point ii is allowed to fall short of the margin requirement:

Every point gets its own ξi\xi_i, and most of them stay at 00, 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 ξi\xi_i to a huge number for every point and the constraint is trivially satisfiable no matter how bad w,bw, b 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:

minw,b,ξ 12w2+Ci=1nξisubject toyi(wxi+b)1ξi,  ξi0  i\min_{w, b, \xi} \ \frac{1}{2}\|w\|^2 + C\sum_{i=1}^n \xi_i \quad \text{subject to} \quad y_i(w \cdot x_i + b) \geq 1 - \xi_i, \ \ \xi_i \geq 0 \ \ \forall i

Two competing terms now sit in one objective:

CC is the hyperparameter that decides how much the second term matters relative to the first, it's the knob that turns hard margin (CC \to \infty, no violation tolerated) into a fully relaxed classifier (C0C \to 0, margin can grow as wide as it likes regardless of how many points end up inside it).

Two side-by-side scatter plots of the same overlapping two-class dataset, red x's for one class and green plus signs for the other, with a blue decision boundary and dashed black margin lines. Points that fall inside the margin or on the wrong side are circled in orange, representing xi greater than 0. Left panel at C=0.05 shows a wide margin with 23 circled points and the label small C wide margin more violations tolerated. Right panel at C=20 shows a narrower margin with 18 circled points and the label large C narrower margin violations punished harder.

This is exactly the same bias-variance dial seen with logistic regression's regularization strength, just written as CC instead of λ\lambda, and inverted: scikit-learn's SVC(C=...) uses large CC for less regularization, the opposite convention from λ\lambda.

3. Turning Slack Into a Loss Function

The constraint yi(wxi+b)1ξiy_i(w \cdot x_i + b) \geq 1 - \xi_i combined with ξi0\xi_i \geq 0 pins down the smallest ξi\xi_i can possibly be for a given point: either 00 (if the constraint is already met) or exactly 1yi(wxi+b)1 - y_i(w \cdot x_i + b) (if it isn't). That's precisely what max(0,)\max(0, \cdot) computes:

ξi=max(0, 1yi(wxi+b))\xi_i = \max\big(0, \ 1 - y_i(w \cdot x_i + b)\big)

Substituting this back removes ξi\xi_i and the constraints entirely, collapsing soft margin SVM into a single unconstrained objective over w,bw, b:

minw,b 12w2+Ci=1nmax(0, 1yi(wxi+b))\min_{w, b} \ \frac{1}{2}\|w\|^2 + C\sum_{i=1}^n \max\big(0, \ 1 - y_i(w \cdot x_i + b)\big)

The term inside the sum, max(0,1yi(wxi+b))\max(0, 1 - y_i(w \cdot x_i + b)), is the hinge loss for a single point. It's zero once a point clears the margin by at least 11, and grows linearly the moment it doesn't, which is exactly the behavior ξi\xi_i 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:

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: wx+bw \cdot x + b is a straight line (or hyperplane) no matter how CC 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.