Speech & Audio AI Lab

Interactive labs for speech recognition, speech synthesis, and audio in general: in-browser DSP experiments, algorithms, datasets, and a learning path, without storing any recording.

43experiments

Support Vector Machine

Support Vector Machine (SVM)

IntermediateMachine Learning

Educational content in the form of a scientific draft that needs expert review before being adopted as an academic reference. No experimental result that was not actually run is shown here.

Definition

A classifier that searches for a separating hyperplane with the largest margin between two classes. It can be extended to nonlinear cases with kernels and to multiple classes with binary combinations.

Mathematical Idea

It defines the hyperplane $w^\top x+b=0$ and the margin $2/\|w\|$. We minimize $\tfrac12\|w\|^2+C\sum_i\max(0,1-y_i(w^\top x_i+b))$, where $C$ balances margin against errors. The support vectors are the points on or inside the margin, and they alone determine the hyperplane. With the kernel $K(x,x')$ we obtain nonlinear boundaries without computing the projection explicitly.

How It Works

  1. We extract and normalize the features.
  2. We choose the kernel and the parameter $C$.
  3. We solve the convex optimization problem.
  4. We identify the support vectors and $w,b$.
  5. We classify by the sign of $w^\top x+b$.

Stages Diagram

  1. Features
  2. Normalization
  3. Kernel
  4. Margin optimization
  5. sign decision

Equations

$$\min_{w,b}\ \tfrac12\|w\|^2+C\sum_i\max\big(0,\,1-y_i(w^\top x_i+b)\big)$$

Hinge loss with the margin constraint.

$$\text{margin}=\frac{2}{\|w\|}$$

The margin width.

$$f(x)=\operatorname{sign}\Big(\sum_i \alpha_i y_i K(x_i,x)+b\Big)$$

The dual formulation with a kernel.

Advantages

  • A large margin gives good generalization.
  • Effective with high feature dimensions.
  • Multiple kernels for nonlinear problems.

Limitations

  • Does not scale easily to millions of examples.
  • Sensitive to parameters and normalization.
  • Does not give probabilities directly.

Use Cases

  • Classifying sounds and commands.
  • Speaker recognition (educational).
  • Emotion classification with handcrafted features.

Applied Example

If $w=(1,1)$ and $b=-3$: the point $(2,2)$ gives $w^\top x+b=1$, so it lies on the margin, and the margin width is $2/\sqrt2\approx1.414$.

Code Example

PythonImplemented and Tested
import numpy as np
rng = np.random.default_rng(0)
X = np.vstack([rng.normal([-2, -2], 0.7, (40, 2)), rng.normal([2, 2], 0.7, (40, 2))])
y = np.array([-1]*40 + [1]*40)
Xb = np.hstack([X, np.ones((80, 1))])                 # constant column for the bias b
w, lam = np.zeros(3), 0.1
for t in range(1, 3001):                              # Pegasos algorithm
    i = rng.integers(len(X))
    eta = 1/(lam*t)
    if y[i]*(Xb[i] @ w) < 1:                          # margin violation
        w = (1 - eta*lam)*w + eta*y[i]*Xb[i]
    else:
        w = (1 - eta*lam)*w
print("accuracy:", (np.sign(Xb @ w) == y).mean())
print("margin width 2/||w||:", round(2/np.linalg.norm(w[:2]), 2))

References

  1. Cortes, Vapnik. Support-vector networks. Machine Learning (1995). DOI
  2. McFee, Raffel, Liang, Ellis, McVicar, Battenberg, et al.. librosa: Audio and Music Signal Analysis in Python. Proceedings of the 14th Python in Science Conference (2015). DOI

Audio processing in the experiments takes place inside your browser. Audio, texts, and conversations are not saved on the server. Privacy Policy