为啥 libsvm 在同一数据集上创建不同的结果

Posted

技术标签:

【中文标题】为啥 libsvm 在同一数据集上创建不同的结果【英文标题】:Why libsvm creates different results on same dataset为什么 libsvm 在同一数据集上创建不同的结果 【发布时间】:2012-07-28 09:44:30 【问题描述】:

我正在使用 libsvm,尽管我使用相同的参数训练函数两次运行相同的程序,但在模型中创建了不同的 probA 和 probB 值。

这是我的代码

public boolean Train()

    System.out.println("debug 16- mparameter :  "+ m_parameter.svm_type + " " + m_parameter.kernel_type + " " + m_parameter.gamma + " " + m_parameter.C + " " + m_parameter.cache_size + " " + m_parameter.eps + " " + m_parameter.shrinking + " " +m_parameter.probability + " " + m_parameter.nr_weight );
    System.out.println("training...");
    m_model=svm.svm_train(m_problem,m_parameter);
    System.out.print("debug 16- mparameter :  "+ m_parameter.svm_type + " " + m_parameter.kernel_type + " " + m_parameter.gamma + " " + m_parameter.C + " " + m_parameter.cache_size + " " + m_parameter.eps + " " + m_parameter.shrinking + " " +m_parameter.probability + " " + m_parameter.nr_weight );
    System.out.println(" Prob AB : " + m_model.probA[0]+  " " +m_model.probB[0]+ " "  );

    for(int i=0;i<m_problem.l;i++)
    

        for(int j=0;j<featureSetSize;j++)
        
            System.out.print(m_problem.x[i][j].index + " "+ m_problem.x[i][j].value);
           
        System.out.print(m_problem.y[i]+ "\n");
    
    return true;

我检查打印输出,一切都相同,但每次运行程序时产生的 probA 和 probB 值都不同。

这里是打印输出

debug 16- mparameter :  0 2 0.0078125 0.03125 40.0 0.001 0 1 0debug 16- mparameter :  0             2 0.0078125 0.03125 40.0 0.001 0 1 0 Prob AB : 0.625358670608583 6.877246970538699E-4 
1 4.2671854734111552 8.9323843416370113 -2.16877637130801664 1.29411764705882255 -4.1436464088397796 -3.58778625954198567 -7.9584775086505198 -1.40983606557377079 -4.74878444084278910 -0.3508771929824554511 -0.4000000000000003612 1.31868131868131751.0
1 -8.4695201037613492 5.7295373665480443 1.9324894514767944 -2.117647058823535 -7.6243093922651936 -10.07 -6.6782006920415238 -8.0655737704918029 0.01620745542949819710 -10.011 -9.012 10.01.0
1 0.479896238651102542 -10.03 10.04 2.23529411764705985 -3.75690607734806656 -3.81679389312977077 10.08 10.09 3.679092382495948510 -1.929824561403508611 -2.012 1.31868131868131751.0
1 5.1491569390402072 -3.2028469750889683 8.3797468354430364 -1.05882352941176455 3.86740331491712656 -7.1755725190839697 0.7266435986159178 -3.2459016393442629 7.8606158833063210 -1.403508771929825411 -2.012 1.4285714285714271.0
1 -0.0129701686121919122 8.2562277580071183 -0.194092827004219264 1.8823529411764715 -5.3038674033149176 -3.35877862595419877 3.2179930795847768 -10.09 6.5964343598055110 1.929824561403508611 -4.812 -10.01.0
1 9.273670557717252 -2.8825622775800713 5.76371308016877754 1.52941176470588055 -4.4751381215469616 -4.1221374045801537 6.19377162629757558 4.7213114754098349 6.40194489465153910 -0.877192982456140411 -1.800000000000000712 1.53846153846153651.0
1 3.3333333333333322 1.17437722419928823 5.3080168776371314 1.52941176470588055 -4.36464088397796 -3.74045801526717547 3.6332179930795858 -0.327868852459015879 7.99027552674230210 -0.701754385964912711 -3.599999999999999612 1.31868131868131751.0
1 2.76264591439688622 1.38790035587188683 5.1561181434599154 1.29411764705882255 -4.254143646408846 -4.4274809160305347 1.1418685121107268 -0.098360655737705479 7.017828200972445510 -1.403508771929825411 -1.199999999999999312 1.53846153846153651.0
1 9.0402075226977952 5.4804270462633463 0.177215189873418674 2.1176470588235295 -4.6961325966850836 -4.27480916030534357 2.52595155709342668 0.49180327868852569 6.49918962722852510 0.701754385964912711 -7.012 -0.32967032967033031.0
1 1.43968871595330582 10.03 -4.2109704641350214 2.1176470588235295 -4.4751381215469616 -3.96946564885496227 -7.7508650519031148 0.065573770491802469 -8.8654781199351710 -1.754385964912280811 -2.199999999999999312 1.4285714285714271.0
1 -3.82619974059662752 7.36654804270462553 -0.54852320675105484 1.05882352941176455 0.05524861878453186 -3.2061068702290077 3.0449826989619388 -2.196721311475419 9.6758508914100510 -3.33333333333333411 -3.399999999999999512 1.64835164835164961.0
1 -1.7769130998702992 8.6476868327402133 -3.4345991561181434 1.6470588235294135 -4.4751381215469616 -4.1984732824427487 2.52595155709342668 -3.18032786885245949 7.99027552674230210 -1.929824561403508611 -1.599999999999999612 1.2087912087912081.0
1 -9.4811932555123222 7.93594306049822153 -5.9324894514767934 10.05 10.06 6.183206106870237 -10.08 -7.7377049180327879 -8.11993517017828210 -2.982456140350877611 -1.800000000000000712 -0.43956043956043981.0
1 2.81452658884565572 2.2419928825622793 4.8691983122362874 2.05 -4.4751381215469616 -4.1221374045801537 -0.242214532871972928 -1.93442622950819759 6.46677471636952810 -1.754385964912280811 -1.599999999999999612 1.4285714285714271.0
1 7.3540856031128432 -5.4804270462633463 6.4050632911392434 -10.05 -10.06 10.07 2.87197231833910048 3.54098360655737749 5.17017828200972610 -2.80701754385964911 2.599999999999999612 2.30769230769230841.0
1 6.3942931258106342 5.3380782918149473 1.17299578059071764 2.70588235294117575 -4.8618784530386746 -5.1145038167938937 -1.5224913494809698 5.6393442622950839 -5.98055105348460310 -3.684210526315789411 -2.199999999999999312 2.6373626373626372.0
1 -10.02 3.73665480427046333 2.01687763713080064 3.41176470588235155 -4.309392265193376 -4.9618320610687037 -5.0173010380622848 3.54098360655737749 -3.35494327390599710 -2.456140350877192611 -9.612 -0.10989010989010952.0
1 -2.03631647211413822 -0.427046263345195243 6.8776371308016884 2.05 -4.5856353591160226 -4.04580152671755767 1.41868512110726648 0.59016393442622939 6.3371150729335510 -2.28070175438596611 -1.012 0.43956043956043982.0
1 -0.89494163424124552 4.0569395017793593 4.6666666666666664 2.23529411764705985 -4.254143646408846 -3.81679389312977077 0.138408304498270248 -3.18032786885245949 8.21717990275526610 -2.105263157894736311 -1.800000000000000712 2.74725274725274642.0
1 -2.55512321660181562 8.0071174377224213 1.84810126582278584 1.8823529411764715 -4.5856353591160226 -3.81679389312977077 3.3217993079584778 -4.81967213114754149 9.1247974068071310 -1.052631578947368111 -2.012 2.85714285714285772.0
1 -5.6420233463035012 0.106761565836299253 5.8481012658227844 1.52941176470588055 -5.63535911602216 -4.4274809160305347 -1.90311418685121238 1.47540983606557329 2.05834683954619310 -2.80701754385964911 -1.012 0.76923076923076832.0
1 -6.2645914396887162 7.82918149466192053 -0.88607594936708984 1.8823529411764715 -4.7513812154696136 -4.1221374045801537 0.207612456747405368 -0.36065573770491719 5.3970826580226910 -1.57894736842105311 -1.400000000000000412 1.7582417582417592.0
1 3.98184176394293132 1.95729537366547923 4.5822784810126584 1.6470588235294135 -4.4751381215469616 -4.1984732824427487 2.97577854671280138 -0.327868852459015879 8.8006482982171810 -1.929824561403508611 -1.400000000000000412 1.31868131868131752.0
1 10.02 -2.2775800711743783 4.37974683544303644 1.8823529411764715 -4.7513812154696136 -4.656488549618327 3.9446366782006938 -1.31147540983606539 8.34683954619124810 -1.228070175438597611 -1.800000000000000712 1.4285714285714272.0
1 2.60700389105058332 2.20640569395017753 5.5105485232067514 1.8823529411764715 -3.70165745856353656 -3.74045801526717547 0.79584775086505398 0.5573770491803289 6.9529983792544610 -0.877192982456140411 -1.599999999999999612 1.53846153846153652.0
1 1.90661478599221822 1.03202846975088923 4.7172995780590714 2.05 -3.97790055248618756 -3.74045801526717547 7.543252595155718 9.9672131147540999 -1.215559157212318610 10.011 -10.012 -6.9230769230769232.0
1 3.2036316472114132 2.1352313167259783 -10.04 8.5882352941176495 -5.63535911602216 -2.82442748091603067 -7.1626297577854678 2.09 -10.010 -7.36842105263157911 10.012 -5.7142857142857142.0
1 -2.6329442282749672 9.8220640569395033 -3.38396624472573754 2.1176470588235295 -4.6408839779005536 -4.04580152671755767 2.76816608996539778 -4.0655737704918049 10.010 -1.403508771929825411 3.400000000000000412 2.74725274725274642.0
1 -2.6588845654993522 8.2206405693950163 -0.71729957805907144 1.17647058823529355 -4.419889502762436 -4.4274809160305347 2.04152249134948078 -2.85245901639344279 8.83306320907617610 -1.403508771929825411 -1.012 1.4285714285714272.0
1 3.41115434500648362 4.76868327402135253 3.09704641350210964 0.94117647058823555 -4.8618784530386746 -4.4274809160305347 2.80276816608996538 3.93442622950819759 5.88330632090761810 -1.929824561403508611 -2.199999999999999312 1.31868131868131752.0

第二次运行

debug 16- mparameter :  0 2 0.0078125 0.03125 40.0 0.001 0 1 0debug 16- mparameter :  0 2 0.0078125 0.03125 40.0 0.001 0 1 0 Prob AB : 1.1138705486826292 -0.2588389268559099 
1 4.2671854734111552 8.9323843416370113 -2.16877637130801664 1.29411764705882255 -4.1436464088397796 -3.58778625954198567 -7.9584775086505198 -1.40983606557377079 -4.74878444084278910 -0.3508771929824554511 -0.4000000000000003612 1.31868131868131751.0
1 -8.4695201037613492 5.7295373665480443 1.9324894514767944 -2.117647058823535 -7.6243093922651936 -10.07 -6.6782006920415238 -8.0655737704918029 0.01620745542949819710 -10.011 -9.012 10.01.0
1 0.479896238651102542 -10.03 10.04 2.23529411764705985 -3.75690607734806656 -3.81679389312977077 10.08 10.09 3.679092382495948510 -1.929824561403508611 -2.012 1.31868131868131751.0
1 5.1491569390402072 -3.2028469750889683 8.3797468354430364 -1.05882352941176455 3.86740331491712656 -7.1755725190839697 0.7266435986159178 -3.2459016393442629 7.8606158833063210 -1.403508771929825411 -2.012 1.4285714285714271.0
1 -0.0129701686121919122 8.2562277580071183 -0.194092827004219264 1.8823529411764715 -5.3038674033149176 -3.35877862595419877 3.2179930795847768 -10.09 6.5964343598055110 1.929824561403508611 -4.812 -10.01.0
1 9.273670557717252 -2.8825622775800713 5.76371308016877754 1.52941176470588055 -4.4751381215469616 -4.1221374045801537 6.19377162629757558 4.7213114754098349 6.40194489465153910 -0.877192982456140411 -1.800000000000000712 1.53846153846153651.0
1 3.3333333333333322 1.17437722419928823 5.3080168776371314 1.52941176470588055 -4.36464088397796 -3.74045801526717547 3.6332179930795858 -0.327868852459015879 7.99027552674230210 -0.701754385964912711 -3.599999999999999612 1.31868131868131751.0
1 2.76264591439688622 1.38790035587188683 5.1561181434599154 1.29411764705882255 -4.254143646408846 -4.4274809160305347 1.1418685121107268 -0.098360655737705479 7.017828200972445510 -1.403508771929825411 -1.199999999999999312 1.53846153846153651.0
1 9.0402075226977952 5.4804270462633463 0.177215189873418674 2.1176470588235295 -4.6961325966850836 -4.27480916030534357 2.52595155709342668 0.49180327868852569 6.49918962722852510 0.701754385964912711 -7.012 -0.32967032967033031.0
1 1.43968871595330582 10.03 -4.2109704641350214 2.1176470588235295 -4.4751381215469616 -3.96946564885496227 -7.7508650519031148 0.065573770491802469 -8.8654781199351710 -1.754385964912280811 -2.199999999999999312 1.4285714285714271.0
1 -3.82619974059662752 7.36654804270462553 -0.54852320675105484 1.05882352941176455 0.05524861878453186 -3.2061068702290077 3.0449826989619388 -2.196721311475419 9.6758508914100510 -3.33333333333333411 -3.399999999999999512 1.64835164835164961.0
1 -1.7769130998702992 8.6476868327402133 -3.4345991561181434 1.6470588235294135 -4.4751381215469616 -4.1984732824427487 2.52595155709342668 -3.18032786885245949 7.99027552674230210 -1.929824561403508611 -1.599999999999999612 1.2087912087912081.0
1 -9.4811932555123222 7.93594306049822153 -5.9324894514767934 10.05 10.06 6.183206106870237 -10.08 -7.7377049180327879 -8.11993517017828210 -2.982456140350877611 -1.800000000000000712 -0.43956043956043981.0
1 2.81452658884565572 2.2419928825622793 4.8691983122362874 2.05 -4.4751381215469616 -4.1221374045801537 -0.242214532871972928 -1.93442622950819759 6.46677471636952810 -1.754385964912280811 -1.599999999999999612 1.4285714285714271.0
1 7.3540856031128432 -5.4804270462633463 6.4050632911392434 -10.05 -10.06 10.07 2.87197231833910048 3.54098360655737749 5.17017828200972610 -2.80701754385964911 2.599999999999999612 2.30769230769230841.0
1 6.3942931258106342 5.3380782918149473 1.17299578059071764 2.70588235294117575 -4.8618784530386746 -5.1145038167938937 -1.5224913494809698 5.6393442622950839 -5.98055105348460310 -3.684210526315789411 -2.199999999999999312 2.6373626373626372.0
1 -10.02 3.73665480427046333 2.01687763713080064 3.41176470588235155 -4.309392265193376 -4.9618320610687037 -5.0173010380622848 3.54098360655737749 -3.35494327390599710 -2.456140350877192611 -9.612 -0.10989010989010952.0
1 -2.03631647211413822 -0.427046263345195243 6.8776371308016884 2.05 -4.5856353591160226 -4.04580152671755767 1.41868512110726648 0.59016393442622939 6.3371150729335510 -2.28070175438596611 -1.012 0.43956043956043982.0
1 -0.89494163424124552 4.0569395017793593 4.6666666666666664 2.23529411764705985 -4.254143646408846 -3.81679389312977077 0.138408304498270248 -3.18032786885245949 8.21717990275526610 -2.105263157894736311 -1.800000000000000712 2.74725274725274642.0
1 -2.55512321660181562 8.0071174377224213 1.84810126582278584 1.8823529411764715 -4.5856353591160226 -3.81679389312977077 3.3217993079584778 -4.81967213114754149 9.1247974068071310 -1.052631578947368111 -2.012 2.85714285714285772.0
1 -5.6420233463035012 0.106761565836299253 5.8481012658227844 1.52941176470588055 -5.63535911602216 -4.4274809160305347 -1.90311418685121238 1.47540983606557329 2.05834683954619310 -2.80701754385964911 -1.012 0.76923076923076832.0
1 -6.2645914396887162 7.82918149466192053 -0.88607594936708984 1.8823529411764715 -4.7513812154696136 -4.1221374045801537 0.207612456747405368 -0.36065573770491719 5.3970826580226910 -1.57894736842105311 -1.400000000000000412 1.7582417582417592.0
1 3.98184176394293132 1.95729537366547923 4.5822784810126584 1.6470588235294135 -4.4751381215469616 -4.1984732824427487 2.97577854671280138 -0.327868852459015879 8.8006482982171810 -1.929824561403508611 -1.400000000000000412 1.31868131868131752.0
1 10.02 -2.2775800711743783 4.37974683544303644 1.8823529411764715 -4.7513812154696136 -4.656488549618327 3.9446366782006938 -1.31147540983606539 8.34683954619124810 -1.228070175438597611 -1.800000000000000712 1.4285714285714272.0
1 2.60700389105058332 2.20640569395017753 5.5105485232067514 1.8823529411764715 -3.70165745856353656 -3.74045801526717547 0.79584775086505398 0.5573770491803289 6.9529983792544610 -0.877192982456140411 -1.599999999999999612 1.53846153846153652.0
1 1.90661478599221822 1.03202846975088923 4.7172995780590714 2.05 -3.97790055248618756 -3.74045801526717547 7.543252595155718 9.9672131147540999 -1.215559157212318610 10.011 -10.012 -6.9230769230769232.0
1 3.2036316472114132 2.1352313167259783 -10.04 8.5882352941176495 -5.63535911602216 -2.82442748091603067 -7.1626297577854678 2.09 -10.010 -7.36842105263157911 10.012 -5.7142857142857142.0
1 -2.6329442282749672 9.8220640569395033 -3.38396624472573754 2.1176470588235295 -4.6408839779005536 -4.04580152671755767 2.76816608996539778 -4.0655737704918049 10.010 -1.403508771929825411 3.400000000000000412 2.74725274725274642.0
1 -2.6588845654993522 8.2206405693950163 -0.71729957805907144 1.17647058823529355 -4.419889502762436 -4.4274809160305347 2.04152249134948078 -2.85245901639344279 8.83306320907617610 -1.403508771929825411 -1.012 1.4285714285714272.0
1 3.41115434500648362 4.76868327402135253 3.09704641350210964 0.94117647058823555 -4.8618784530386746 -4.4274809160305347 2.80276816608996538 3.93442622950819759 5.88330632090761810 -1.929824561403508611 -2.199999999999999312 1.31868131868131752.0

任何帮助将不胜感激。

【问题讨论】:

这些看起来不像是概率,实际上——其中一个是负数。 你是如何构造问题的?你确定两次运行之间是一样的吗? 另外,该代码没有生成这些打印输出。尽管出现在代码中,但我没有在输出中看到“training...”。 有趣的是,我注意到radial basis kernel "Gaussian" 在我使用相同的确切数据运行相同的确切代码时会产生略微不同的结果。我认为这是因为某些分布的抽样差异。 【参考方案1】:

也许数据在代码中的某个地方被打乱了,因此每次运行它时你最终会得到一个稍微不同的解决方案。

【讨论】:

【参考方案2】:

您可以在训练和预测时放弃使用-b 1,因为这会使模型在for 循环中变得不确定。但是,通过禁止-b,我们无法估计概率。

【讨论】:

以上是关于为啥 libsvm 在同一数据集上创建不同的结果的主要内容,如果未能解决你的问题,请参考以下文章

保存的随机森林模型在同一数据集上产生不同的结果

Keras:使用批量标准化在同一数据集上的不同训练和验证结果

为啥 k-means 聚类散点图显示不同的结果?

为啥 LibSvm 准确率不超过 50%?

为啥 LinearSVC 在这个数据集上效果这么差?

R语言使用caretEnsemble包的caretList函数一次性构建多个机器学习模型并使用caret包的resamples函数比较在同一数据集上多个机器学习模型的比较结果