Skip to content
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

SVM regressor predictOne() #17

Closed
mente371 opened this issue May 25, 2017 · 3 comments
Closed

SVM regressor predictOne() #17

mente371 opened this issue May 25, 2017 · 3 comments

Comments

@mente371
Copy link

mente371 commented May 25, 2017

While implementing your fabulous SVM class (as a regressor), I always fall upon an error while running predictOne().
The error happens here :
"var sum = 0.0
for k in 0..<supportVectorCount[0] {
sum += coefficients[0][coeffStart[0]+k] * kernelValue[coeffStart[0]+k]
}
for k in 0..<supportVectorCount[1] {
sum += coefficients[0][coeffStart[1]+k] * kernelValue[coeffStart[1]+k]
}"
the vectorSupport exist, the totalSupportVector is >0. Yet the supportVectorCount is nil.
Is it because I am in the regressor mode and not the classification mode ? I did not look at the classification cases yet.
So, basically, the two "for in" instructions cited above will always fail.

I came around this by creating a mock function like this :
open func predictOneJY (_ inputs: [Double]) -> Double {
var sum = 0.0
for i in 0..<totalSupportVectors {
let kernelValue = Kernel.calcKernelValue(kernelParams, x: inputs, y: supportVector[i])
sum += coefficients[0][i] * kernelValue
}
sum -= ρ[0]
return sum
}
It returns the correct ouput AFAIK.
But I wonder if there is something that I miss, and it is, in fact, my implementation that is faulty, or if the code of predictOne() could indeed be "streamlined" or "corrected" for the regressor cases.
And thanks again for your magnificent and very useful project.

@KevinCoble
Copy link
Owner

I will look into this.

I ported the SVM code from the public domain LIBSVM (a C++ package). I used it for a classification task (I've always thought that SVM as a regression model was a bit strange...). That was the start of the AIToolbox framework - I just kept adding classifier and regression models as I used them. At one point I made generic protocols for all the regressors/classifiers, so they could be used in a validation model. That was when I made a few changes to the predict routines. I may have made an error then. I will look at the C++ code to verify the port is still valid.

Thanks again for using the framework. I see a bit of traffic and cloning being done with it, but the number of comments/suggestions/error reporting has been somewhat low. I appreciate the feedback.

@KevinCoble
Copy link
Owner

OK, the supportVectorCount is set for classification. The C++ code, being non type-safe, would return the labels for classification as doubles, and regression values as doubles, using the same routines for classification. Probability estimates were done by using regression values for classification models, since the predict function would return that double value.

I replaced the predictOne with the guts of the 'predict' function. This will avoid use of the supportVectorCount variable until it is needed for a classification type model. I also added a call to predictOne in the XCTest file to verify this.

Let me know if this works for you, and I'll close the issue

@mente371
Copy link
Author

I just tested it. It works fine, as it should. You can close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants