-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the decision function after training a SVM model? #105
Comments
I believe that It would be great if you could verify this – and potentially correct me. EDIT: if you are only interested in the decision values |
I think As we know, in the dual problem of two-class SVM, the constraints on d = 3; N = 10;
X = rand(d, N)
y = rand([-1, 1], N)
K = X' * X
model = svmtrain(K, y, kernel=Kernel.Precomputed)
ind_SVs = model.SVs.indices
α = model.coefs ./ y[ind_SVs]
b = model.rho
α, b, α[:]>=zeros(N), sum(α) It turned out that As for one-class SVM, we have constraint d = 3; N = 10;
X = rand(d, N)
K = X' * X
model = svmtrain(K, svmtype=OneClassSVM, kernel=Kernel.Precomputed)
ind_SVs = model.SVs.indices
α = model.coefs
b = model.rho
α, b, sum(α) The result of |
Thanks, but I do need the exact value of the Lagrangian multiplers |
I think your code is computing d = 3; N = 10;
X = rand(d, N)
y = rand([-1, 1], N)
model = svmtrain(X, y)
sv_y = model.SVs.y
α = model.coefs ./ sv_y
@show sum(sv_y .* α) I am getting zero (or something close to zero). Regarding the sign of the Lagrange multipliers, they should still be valid as long as all of them are of the same sign, right? The weight vector reads and assuming So if all the signs are the same, you get either It all comes down to which label LIBSVM considers to be positive, and I think the answer is that the label of the first training example is chosen as "positive" (even if it happens to be So this should give you the expected result: d = 3; N = 10;
X = rand(d, N)
y = rand([-1, 1], N)
model = svmtrain(X, y)
sv_y = model.SVs.y
α = first(y) * model.coefs ./ sv_y # if y[1] == -1, flip the signs
@show sum(sv_y .* α) # should be zero
@show all(α .≥ 0) # should be true |
Oh, I'm sorry for the wrong code in the first example. Thanks! But how about the one-class SVM case? Why is the The actual |
For one-class SVM, LIBSVM imposes the constraint |
Thank you very much! Everything becomes clear. 🤝 |
I kindly ask you to verify our conclusions and let us know here if they are correct. We can then close the issue. |
Sure. So far, there is no evidence from limited instances that the conclusions are wrong. I will continue to gather more information to confirm this. To confirm the above conclusions theoretically, I think it's best if someone can analyze the code rigorously. |
One more question: Does anyone know how to retrieve the optimal values of the slack variables Thanks! |
I don't know if that's possible. You might compute them yourself by obtaining the decision values |
Generally, the decision function of a SVM model should be like$f(x) = sgn(\sum_i\alpha_iy_iK(x, x_i) + b)$ . Then, how to get the $\alpha_i$ and $b$ in the function after the model is trained?
For example,
Are they
model.coefs
andmodel.rho
?The text was updated successfully, but these errors were encountered: