KevinCoble / AIToolbox

A toolbox of AI modules written in Swift: Graphs/Trees, Support Vector Machines, Neural Networks, PCA, K-Means, Genetic Algorithms
Apache License 2.0
793 stars 87 forks source link

SVM regressor predictOne() #17

Closed mente371 closed 7 years ago

mente371 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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