Xtra-Computing / thundersvm

ThunderSVM: A Fast SVM Library on GPUs and CPUs
Apache License 2.0
1.56k stars 217 forks source link

Load trained model in C++ #186

Closed WeiChihChern closed 4 years ago

WeiChihChern commented 4 years ago

Hello,

I've trained some models and would like to test it in my c++ code. How to I load my models and perform classification? Since I don't want to call the binary and load the model each time for classification.

Further, how can I retrieve the weights like LibSVM? I cannot find .sv_coef in the trained model files. LibSVM's example:

(Link to libsvm FAQ: https://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html)
w = model.SVs' * model.sv_coef;
b = -model.rho;

if model.Label(1) == -1
  w = -w;
  b = -b;
end

And, will a thunderSVM's trained model generate a recommended threshold value for predict purpose?

Thank you.

QinbinLi commented 4 years ago

Hi, @WeiChihChern

It is not easy if you want to load model inside your c++ code. In our library, we have load_from_file function here defined in a class SvmModel. If you want to call this function, you have to link our library, define the class, load and test. I suggest you to use scikit interface to load model if possible.

For sv_coef, it is stored in the first few values of lines after SV inside the trained model file. In scikit interface, you can use dual_coef_ to get the coefficients.

QinbinLi commented 4 years ago

Also, there is no recommend threshold value for prediction. The prediction is based on the support vectors.

WeiChihChern commented 4 years ago

@GODqinbin Thank you for the reply.

I've a simple svm predict implementation which I only port the models weights and bias to my system for classification. Therefore, right now I'm trying to derive the weights.

I've some questions regarding prediction, for example linear SVM:

  1. Does the number of weights = number of features? According to w•xi+b, we the dimension of weights and features should be identical right?
  2. If so, I'm kinda confused on how can I derive weights from given support vectors and sv_coef. Would you please elaborate a bit more on how can I get the weights of my trained models?

Much appreciated

zeyiwen commented 4 years ago

Where did you see number of weights? It may mean the number of support vectors as well. Please be careful.

You need to read this section to convert sv_coef plus support vectors to get the weight vector w.

WeiChihChern commented 4 years ago

@zeyiwen In my c++ project, there's already a svm implementation where the code author ported the matlab trained svm model by copying the weights & bias into the code, and perform two-class classification by using w•xi+b.

In the code, the number of weights happen to be equal to the number of features.

According to your link, I can tell the number of weights should be equal to the number of support vectors. However, if the number of weights isn't necessary equal to the number of features, how does w•xi+b work when the dimension of w & xi don't match?

WeiChihChern commented 4 years ago

@GODqinbin & @zeyiwen any thoughts on this?

zeyiwen commented 4 years ago

I think this is not related to ThunderSVM. Let's keep the GitHub issues here for ThunderSVM. You need to read more materials about linear and kernel SVMs.

QinbinLi commented 4 years ago

@WeiChihChern

I think when you say number of weights, you mean the dimension of the weight. The dimension of the weight is equal to the number of features. @zeyiwen has shown you the formula to compute the weight vector in this link. The weight is computed by the support vectors, so the dimension is unchanged.