Degree-2 Feature Map vs Polynomial Kernel Pegasos: Reproduce Primal SVM Weights from Dual Coefficients

Quick Overview

Build an explicit degree-2 polynomial feature map, train a bias-free linear SVM on it with Pegasos, and write a kernelized Pegasos whose dual coefficients give back the same weights. It tests the kernel trick, the unrolled Pegasos update, and exact control of the random visiting order.

Degree-2 Feature Map vs Polynomial Kernel Pegasos: Reproduce Primal SVM Weights from Dual Coefficients

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement an explicit degree-2 polynomial feature map for 2D inputs, train a bias-free linear SVM on it with Pegasos, and train a kernelized Pegasos whose dual coefficients reproduce the same weights. Labels are `±1`. Implement these functions: - `poly2_feature_map(X, gamma=1.0, coef0=1.0)` returns the columns $[\gamma x_1^2,\ \gamma x_2^2,\ \sqrt{2}\,\gamma x_1 x_2,\ \sqrt{2\gamma c}\, x_1,\ \sqrt{2\gamma c}\, x_2,\ c]$, where $c$ is `coef0`. Input not shaped `(n, 2)`, or a negative `gamma` or `coef0`, raises `ValueError`. - `poly_kernel(X, Y, gamma=1.0, coef0=1.0, degree=2)` returns `(gamma * X @ Y.T + coef0) ** degree`. - `primal_objective(w, Phi, y, lam)` returns the float $\frac{\lambda}{2}\|w\|^2 + \frac{1}{n}\sum_i \max(0,\ 1 - y_i\, \phi_i \cdot w)$. - `pegasos_train(Phi, y, lam, epochs, seed)` starts from `w = 0`, creates `rng = np.random.default_rng(seed)` and visits `rng.permutation(n)` each epoch. At step `t` (global across epochs, starting from 1) it sets $\eta = 1/(\lambda t)$, computes the margin with the current `w`, scales `w` by $(1 - \eta\lambda)$, then adds $\eta\, y_i \phi_i$ if the margin is below 1. - `kernel_pegasos_train(X, y, lam, epochs, seed, gamma=1.0, coef0=1.0)` uses the same RNG and visiting order on the degree-2 kernel and returns `dual_coef` such that `Phi.T @ dual_coef` equals the primal `w`. - `kernel_decision_function(X_train, dual_coef, X_new, gamma=1.0, coef0=1.0)` returns `poly_kernel(X_new, X_train) @ dual_coef`. Example: ```python poly2_feature_map([[1.0, 2.0]]) # [[1, 4, 2√2, √2, 2√2, 1]] poly_kernel([[1, 2]], [[3, -1]], 0.5, 1.0) # [[2.25]] pegasos_train([[1, 2]], [-1], 0.5, 1, 0) # [-2., -4.] pegasos_train([[1, 2]], [-1], 0.5, 2, 0) # [-1., -2.] ``` ```hint Unroll the update Write $w_t$ in terms of $w_{t-1}$ with $\eta_t = 1/(\lambda t)$ substituted, then multiply both sides by $\lambda t$. Look at what the weights become as a sum over past updates. ``` ```hint Watch the first step At $t = 1$ the shrink factor is zero and the weights are still zero. Make sure the kernel version handles that step without dividing by zero. ``` ### Constraints & Assumptions - Inputs to the feature map are 2D, shape `(n, 2)`. The map has 6 output features. - Labels are `-1` or `1`. - `gamma` and `coef0` are non-negative. - The SVM has no separate bias term. - Both trainers must consume the random generator identically so they visit samples in the same order. ### What a Strong Answer Covers - A feature map whose inner products equal the degree-2 polynomial kernel exactly, with the input checks. - A primal Pegasos loop that follows the specified order: step size, margin on the current weights, shrink, conditional update. - The unrolled form of the Pegasos weights and how it gives the dual coefficients. - A kernel trainer that reproduces the same visiting order and margin decisions, including the first step. - Time and memory cost of the primal and kernel versions. ### Follow-up Questions - Why prefer the kernel form over the explicit map at higher degree or dimension? - How would you add a bias term without breaking the equivalence? - What does the optional Pegasos projection step do?

Overview: Build an explicit degree-2 polynomial feature map, train a bias-free linear SVM on it with Pegasos, and write a kernelized Pegasos whose dual coefficients give back the same weights. It tests the kernel trick, the unrolled Pegasos update, and exact control of the random visiting order.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement an explicit degree-2 polynomial feature map for 2D inputs, train a bias-free linear SVM on it with Pegasos, and train a kernelized Pegasos whose dual coefficients reproduce the same weights. Labels are ±1.

Implement these functions:

  • poly2_feature_map(X, gamma=1.0, coef0=1.0) returns the columns [γx12, γx22, 2 γx1x2, 2γc x1, 2γc x2, c][\gamma x_1^2,\ \gamma x_2^2,\ \sqrt{2}\,\gamma x_1 x_2,\ \sqrt{2\gamma c}\, x_1,\ \sqrt{2\gamma c}\, x_2,\ c] , where cc is coef0 . Input not shaped (n, 2) , or a negative gamma or coef0 , raises ValueError .
  • poly_kernel(X, Y, gamma=1.0, coef0=1.0, degree=2) returns (gamma * X @ Y.T + coef0) ** degree .
  • primal_objective(w, Phi, y, lam) returns the float λ2∥w∥2+1n∑imax⁡(0, 1−yi ϕi⋅w)\frac{\lambda}{2}\|w\|^2 + \frac{1}{n}\sum_i \max(0,\ 1 - y_i\, \phi_i \cdot w) .
  • pegasos_train(Phi, y, lam, epochs, seed) starts from w = 0 , creates rng = np.random.default_rng(seed) and visits rng.permutation(n) each epoch. At step t (global across epochs, starting from 1) it sets η=1/(λt)\eta = 1/(\lambda t) , computes the margin with the current w , scales w by (1−ηλ)(1 - \eta\lambda) , then adds η yiϕi\eta\, y_i \phi_i if the margin is below 1.
  • kernel_pegasos_train(X, y, lam, epochs, seed, gamma=1.0, coef0=1.0) uses the same RNG and visiting order on the degree-2 kernel and returns dual_coef such that Phi.T @ dual_coef equals the primal w .
  • kernel_decision_function(X_train, dual_coef, X_new, gamma=1.0, coef0=1.0) returns poly_kernel(X_new, X_train) @ dual_coef .

Example:

poly2_feature_map([[1.0, 2.0]])            # [[1, 4, 2√2, √2, 2√2, 1]]
poly_kernel([[1, 2]], [[3, -1]], 0.5, 1.0) # [[2.25]]
pegasos_train([[1, 2]], [-1], 0.5, 1, 0)   # [-2., -4.]
pegasos_train([[1, 2]], [-1], 0.5, 2, 0)   # [-1., -2.]

Constraints & Assumptions

  • Inputs to the feature map are 2D, shape (n, 2) . The map has 6 output features.
  • Labels are -1 or 1 .
  • gamma and coef0 are non-negative.
  • The SVM has no separate bias term.
  • Both trainers must consume the random generator identically so they visit samples in the same order.

What a Strong Answer Covers Guidance

  • A feature map whose inner products equal the degree-2 polynomial kernel exactly, with the input checks.
  • A primal Pegasos loop that follows the specified order: step size, margin on the current weights, shrink, conditional update.
  • The unrolled form of the Pegasos weights and how it gives the dual coefficients.
  • A kernel trainer that reproduces the same visiting order and margin decisions, including the first step.
  • Time and memory cost of the primal and kernel versions.

Follow-up Questions Guidance

  • Why prefer the kernel form over the explicit map at higher degree or dimension?
  • How would you add a bias term without breaking the equivalence?
  • What does the optional Pegasos projection step do?