Skip to content

Inconsistent Pegasos Kernel SVM test cases #592

Description

@Rishi-Jain-27

The test cases for the Pegasos Kernel SVM Implementation problem use inconsistent alpha conventions.
The problem statement describes the deterministic Pegasos update using the decision function:

f(x_i) = sum(alpha[j] * y[j] * K(x_j, x_i)) + b

Under this convention, alpha[j] is an unsigned coefficient, and the label sign is applied separately through y[j].
So when the margin is violated, the update should be:

alpha[i] = (1 - eta * lambda_val) * alpha[i] + eta
bias = bias + eta * y_i

and when the margin is satisfied:

alpha[i] = (1 - eta * lambda_val) * alpha[i]

However, one of the hidden/torch tests expects signed alpha values:

[100.0, 0.0, -100.0, -100.0]

This implies the implementation should update alpha using:

alpha[i] = (1 - eta * lambda_val) * alpha[i] + eta * y_i

...which conflicts with the stated decision function, which already multiplies by y[j]. Using both signed alphas and alpha[j] * y[j] double-counts the label sign.

Reproduction:
Using the prompt’s stated algorithm/convention:

alphas, b = pegasos_kernel_svm(
    np.array([[1, 2], [2, 3], [3, 1], [4, 1]]),
    np.array([1, 1, -1, -1]),
    kernel='linear',
    lambda_val=0.01,
    iterations=100
)
print(([round(a, 4) for a in alphas], round(b, 4)))

Expected output according to the problem statement:

([2.0, 2.0, 6.0, 1.0], 36.2027)

But the hidden/torch test expects:

([100.0, 0.0, -100.0, -100.0], -937.4755)

These two outputs correspond to different interpretations of what alpha represents.

Suggested fix:
The tests should use the unsigned-alpha convention in the problem statement:

decision_value += alpha[j] * y[j] * K(x_j, x_i)

with update:

alpha[i] = (1 - eta * lambda_val) * alpha[i] + eta

and with regularization decay in the non-violating case:

alpha[i] = (1 - eta * lambda_val) * alpha[i]

The corrected test cases could be:

[
  {
    "test": "alphas, b = pegasos_kernel_svm(np.array([[1, 2], [2, 3], [3, 1], [4, 1]]), np.array([1, 1, -1, -1]), kernel='linear', lambda_val=0.01, iterations=100)\nprint(([round(a, 4) for a in alphas], round(b, 4)))",
    "expected_output": "([2.0, 2.0, 6.0, 1.0], 36.2027)"
  },
  {
    "test": "alphas, b = pegasos_kernel_svm(np.array([[1, 2], [2, 3], [3, 1], [4, 1]]), np.array([1, 1, -1, -1]), kernel='rbf', lambda_val=0.01, iterations=100, sigma=0.5)\nprint(([round(a, 4) for a in alphas], round(b, 4)))",
    "expected_output": "([1.0, 1.0, 1.0, 1.0], 0.0)"
  }
]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions