Hinge Loss in SVM and Finding the Decision Boundary Using Gradient Descent

 

Hinge Loss in SVM and Finding the Decision Boundary Using Gradient Descent


The basic idea is:

SVM finds the decision boundary by minimizing a loss function consisting of two parts:

  1. Hinge loss → penalizes classification errors and margin violations.
  2. Regularization term → encourages a large margin.

Let's understand this step by step.


1. Recall the SVM Decision Function

For a Linear SVM:

f(x)=wTx+b\boxed{f(x)=w^Tx+b}

For two features:

f(x)=w1x1+w2x2+b\boxed{f(x)=w_1x_1+w_2x_2+b}

The decision boundary is where:

wTx+b=0\boxed{w^Tx+b=0}

The prediction is:

y^=sign⁡(wTx+b)\boxed{\hat y=\operatorname{sign}(w^Tx+b)}

For SVM mathematics, we normally use:

y∈{−1,+1}\boxed{y\in\{-1,+1\}}

2. What is Hinge Loss?

Hinge loss measures whether a point is:

  • correctly classified and safely outside the margin,
  • correctly classified but inside the margin,
  • or incorrectly classified.

The hinge loss is:

L(y,f(x))=max⁡(0,  1−yf(x))\boxed{ L(y,f(x))=\max(0,\;1-yf(x)) }

Since:

f(x)=wTx+bf(x)=w^Tx+b

we get:

L=max⁡(0,  1−y(wTx+b))\boxed{ L=\max(0,\;1-y(w^Tx+b)) }

3. Understanding yf(x)y f(x)

The important quantity is:

y(wTx+b)\boxed{y(w^Tx+b)}

Let:

m=y(wTx+b)m=y(w^Tx+b)

This is sometimes called the functional margin.

There are three important cases.


Case 1: m≥1m\geq1

y(wTx+b)≥1y(w^Tx+b)\geq1

The point is:

  • correctly classified ✓
  • outside the margin ✓

Therefore:

L=max⁡(0,1−m)=0L=\max(0,1-m)=0

No hinge loss.


Case 2: 0<m<10<m<1

The point is:

  • correctly classified ✓
  • but inside the margin ✗

Therefore:

L>0L>0

SVM applies a penalty.


Case 3: m<0m<0

The point is:

  • incorrectly classified ✗

Therefore:

L>1L>1

The penalty becomes even larger.


4. Simple Hinge Loss Table

yf(x)y f(x)SituationHinge Loss
22    Correct and outside margin  00
11    On margin  00
0.50.5    Correct but inside margin  0.50.5
00    On decision boundary  11
−1-1    Misclassified  22

5. Example of Calculating Hinge Loss

Suppose:

y=+1y=+1

and the SVM gives:

f(x)=0.6f(x)=0.6

Then:

yf(x)=(+1)(0.6)=0.6y f(x)=(+1)(0.6)=0.6

The hinge loss is:

L=max⁡(0,1−0.6)L=\max(0,1-0.6) L=0.4\boxed{L=0.4}

The point is correctly classified, but it lies inside the margin.


Another Example

Suppose:

y=−1y=-1

and:

f(x)=2f(x)=2

Then:

yf(x)=(−1)(2)=−2yf(x)=(-1)(2)=-2

Therefore:

L=max⁡(0,1−(−2))L=\max(0,1-(-2)) L=3\boxed{L=3}

This point is incorrectly classified.


6. Why is Hinge Loss Used?

Remember the hard-margin constraint:

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

Hard-margin SVM requires every point to satisfy this condition.

But real data may contain noise.

Instead of enforcing this constraint strictly, hinge loss gives a penalty whenever:

yi(wTxi+b)<1y_i(w^Tx_i+b)<1

Thus, hinge loss provides a way to formulate SVM as an optimization problem.


7. The Complete SVM Loss Function

To train an SVM, we use two components.

Part 1: Regularization

12∥w∥2\boxed{ \frac12\|w\|^2 }

This encourages a smaller ∥w∥\|w\|.

Since the margin is:

2∥w∥\frac{2}{\|w\|}

minimizing ∥w∥\|w\| means maximizing the margin.


Part 2: Hinge Loss

For nn training examples:

∑i=1nmax⁡(0,1−yi(wTxi+b))\boxed{ \sum_{i=1}^{n} \max(0,1-y_i(w^Tx_i+b)) }

8. Complete SVM Objective Function

A common form is:

J(w,b)=12∥w∥2+C∑i=1nmax⁡(0,1−yi(wTxi+b))\boxed{ J(w,b)= \frac12\|w\|^2+ C\sum_{i=1}^{n} \max(0,1-y_i(w^Tx_i+b)) }

where:

  • 12∥w∥2\frac12\|w\|^2 → maximizes the margin
  • hinge loss → penalizes violations
  • CC → controls the importance of classification errors

9. How Does Gradient Descent Find the Decision Boundary?

The main idea is simple.

Initially, we do not know:

w1,w2,bw_1,\quad w_2,\quad b

So we start with some initial values.

For example:

w1=0,w2=0,b=0w_1=0,\qquad w_2=0,\qquad b=0

Then repeatedly:

  1. Calculate predictions.
  2. Calculate hinge loss.
  3. Calculate the gradient.
  4. Update ww and bb.
  5. Repeat.

Eventually, the values converge toward a good decision boundary.


10. Gradient Descent Rule

The general gradient descent rule is:

New parameter=Old parameter−η∂J∂parameter\boxed{ \text{New parameter} = \text{Old parameter} - \eta \frac{\partial J}{\partial \text{parameter}} }

where:

η\eta

is the learning rate.

For SVM:

w:=w−η∂J∂w\boxed{ w:=w-\eta\frac{\partial J}{\partial w} }

and:

b:=b−η∂J∂b\boxed{ b:=b-\eta\frac{\partial J}{\partial b} }

11. Gradient of the SVM Objective

Recall:

J(w,b)=12∥w∥2+C∑imax⁡(0,1−yi(wTxi+b))J(w,b)= \frac12\|w\|^2+ C\sum_i \max(0,1-y_i(w^Tx_i+b))

We consider two cases.


Case 1: Point Correctly Classified Outside the Margin

If:

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

then:

hinge loss=0\text{hinge loss}=0

Only the regularization term contributes.

The gradient with respect to ww is:

∂J∂w=w\boxed{ \frac{\partial J}{\partial w}=w }

The gradient with respect to bb is:

∂J∂b=0\boxed{ \frac{\partial J}{\partial b}=0 }

Therefore, the update is:

w:=w−ηw\boxed{ w:=w-\eta w }

and:

b:=b\boxed{ b:=b }

Case 2: Point is Inside the Margin or Misclassified

If:

yi(wTxi+b)<1\boxed{ y_i(w^Tx_i+b)<1 }

then the hinge loss is active.

The gradient is:

∂J∂w=w−Cyixi\boxed{ \frac{\partial J}{\partial w} = w-Cy_ix_i }

and:

∂J∂b=−Cyi\boxed{ \frac{\partial J}{\partial b} = -Cy_i }

Therefore:

w:=w−η(w−Cyixi)w:=w-\eta(w-Cy_ix_i)

and:

b:=b−η(−Cyi)b:=b-\eta(-Cy_i)

Simplifying:

w:=w−ηw+ηCyixi\boxed{ w:=w-\eta w+\eta C y_ix_i } b:=b+ηCyi\boxed{ b:=b+\eta C y_i }

This update moves the decision boundary in a direction that improves classification.

12.Complete Algorithm for Linear SVM Using Gradient Descent

Step 1: Convert labels

0→−1,1→+10\rightarrow-1,\qquad1\rightarrow+1


Step 2: Initialize

w=(0,0)w=(0,0) b=0b=0


Step 3: Repeat for several epochs

For each training example (xi,yi)(x_i,y_i):

Calculate:

mi=yi(wTxi+b)m_i=y_i(w^Tx_i+b)


If:

mi≥1m_i\geq1

update:

w:=w−ηw\boxed{ w:=w-\eta w }


Otherwise:

mi<1m_i<1

update:

w:=w−η(w−Cyixi)\boxed{ w:=w-\eta(w-Cy_ix_i) }

and:

b:=b+ηCyi\boxed{ b:=b+\eta Cy_i }


Step 4: Final Classifier

After training:

f(x)=wTx+b\boxed{ f(x)=w^Tx+b }

The decision boundary is:

wTx+b=0\boxed{ w^Tx+b=0 }

​

Final Summary

Hinge Loss

L=max⁡(0,1−y(wTx+b))\boxed{ L=\max(0,1-y(w^Tx+b)) }

It penalizes:

  • incorrectly classified points
  • correctly classified points inside the margin

SVM Objective

J(w,b)=12∥w∥2+C∑imax⁡(0,1−yi(wTxi+b))\boxed{ J(w,b)= \frac12\|w\|^2+ C\sum_i\max(0,1-y_i(w^Tx_i+b)) }

Gradient/Subgradient Updates

If:

yi(wTxi+b)≥1y_i(w^Tx_i+b)\geq1 w:=w−ηw\boxed{ w:=w-\eta w }

If:

yi(wTxi+b)<1y_i(w^Tx_i+b)<1 w:=w−η(w−Cyixi)\boxed{ w:=w-\eta(w-Cy_ix_i) } b:=b+ηCyi\boxed{ b:=b+\eta Cy_i }

Final Decision Boundary

​After learning 

ww and bb:

                    w1​x1​+w2​x2​+b=0​​

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming