简体   繁体   English

如何使用libsvm matlab固定参数cost和gamma以提高准确性?

[英]how can fixed parameters cost and gamma using libsvm matlab to improve accuracy?

I use libsvm to classify a data base that contain 1000 labels. 我使用libsvm对包含1000个标签的数据库进行分类。 I am new in libsvm and I found a problem to choose the parameters c and g to improve performance. 我是libsvm的新手,我发现选择参数c和g来提高性能的问题。 First, here is the program that I use to set the parameters: 首先,这是我用来设置参数的程序:

  bestcv = 0;
for log2c = -1:3,
 for log2g = -4:1,
  cmd = ['-v 5 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)];
  cv = svmtrain(yapp, xapp, cmd);
  if (cv >= bestcv),
   bestcv = cv; bestc = 2^log2c; bestg = 2^log2g;
 end
  fprintf('%g %g %g (best c=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv);
end
end

as a result, this program gives c = 8 and g = 2 and when I use these values c and g, I found an accuracy rate of 55%. 结果,该程序给出c = 8和g = 2,当我使用这些值c和g时,我发现准确率为55%。 for classification, I use svm one against all. 为了进行分类,我对所有人使用svm one。

 numLabels=max(yapp);
 numTest=size(ytest,1);

   %# train one-against-all models
    model = cell(numLabels,1);
   for k=1:numLabels
      model{k} = svmtrain(double(yapp==k),xapp, '  -c 1000 -g 10 -b 1 ');
    end

 %# get probability estimates of test instances using each model
 prob_black = zeros(numTest,numLabels);
   for k=1:numLabels
     [~,~,p] = svmpredict(double(ytest==k), xtest, model{k}, '-b 1');
     prob_black(:,k) = p(:,model{k}.Label==1);    %# probability of class==k
   end

 %# predict the class with the highest probability
 [~,pred_black] = max(prob_black,[],2);
 acc = sum(pred_black == ytest) ./ numel(ytest)    %# accuracy

The problem is that I need to change these parameters to increase performance. 问题是我需要更改这些参数以提高性能。 for example, when I put randomly c = 10000 and g = 100, I found a better accuracy rate: 70%. 例如,当我随机放置c = 10000和g = 100时,我发现更好的准确率:70%。

Please I need help, how can I set theses parameters ( c and g) so to find the optimum accuracy rate? 请帮助我,如何设置这些参数(c和g)以找到最佳准确率? thank you in advance 先感谢您

Hyperparameter tuning is a nontrivial problem in machine learning. 在机器学习中,超参数调整是一个重要的问题。 The simplest approach is what you've already implemented: define a grid of values, and compute the model on the grid until you find some optimal combination. 最简单的方法就是已经实现的方法:定义值的网格,然后在网格上计算模型,直到找到最佳的组合。 A key assumption is that the grid itself is a good approximation of the surface: that it's fine enough to not miss anything important, but not so fine that you waste time computing values that are essentially the same as neighboring values. 一个关键的假设是,网格本身是曲面的良好近似:它足够精细,不会错过任何重要的东西,但又不够精细,以至于浪费时间来计算与相邻值基本相同的值。 I'm not aware of any method to, in general, know ahead of time how fine a grid is necessary. 我不知道任何方法通常可以提前知道网格的精细程度。 As illustration: imagine that the global optimum is at $(5,5)$ and the function is basically flat elsewhere. 举例说明:假设全局最优值为$(5,5)$,而该函数在其他位置基本平坦。 If your grid is $(0,0),(0,10),(10,10),(0,10)$, you'll miss the optimum completely. 如果您的网格是$(0,0),(0,10),(10,10),(0,10)$,则将完全错过最优值。 Likewise, if the grid is $(0,0), (-10,-10),(-10,0),(0,-10)$, you'll never be anywhere near the optimum. 同样,如果网格为$(0,0),(-10,-10),(-10,0),(0,-10)$,则您永远都不会接近最优值。 In both cases, you have no hope of finding the optimum itself. 在这两种情况下,您都不希望找到最佳的自身。

Some rules of thumb exist for SVM with RBF kernels, though: a grid of $\\gamma\\in\\{2^{-15},2^{-14},...,2^5\\}$ and $C \\in \\{2^{-5}, 2^{-4},...,2^{15}\\}$ is one such recommendation. 但是,对于具有RBF内核的SVM,存在一些经验法则:$ \\ gamma \\ in \\ {2 ^ {-15},2 ^ {-14},...,2 ^ 5 \\} $和$ C的网格\\ in \\ {2 ^ {-5},2 ^ {-4},...,2 ^ {15} \\} $中的一个就是这样的建议。

If you found a better solution outside of the range of grid values that you tested, this suggests you should define a larger grid. 如果在测试的网格值范围之外找到更好的解决方案,则建议您定义一个更大的网格。 But larger grids take more time to evaluate, so you'll either have to commit to waiting a while for your results, or move to a more efficient method of exploring the hyperparameter space. 但是,较大的网格需要花费更多时间进行评估,因此您要么必须等待一段时间才能获得结果,要么要采用更有效的方法来探索超参数空间。

Another alternative is random search: define a "budget" of the number of SVMs that you want to try out, and generate that many random tuples to test. 另一种选择是随机搜索:定义要尝试的SVM数量的“预算”,并生成要测试的这么多随机元组。 This approach is mostly just useful for benchmarking purposes, since it's entirely unintelligent. 这种方法大多数情况下仅用于基准测试,因为它完全不智能。

Both grid search and random search have the advantage of being stupidly easy to implement in parallel. 网格搜索和随机搜索都具有非常容易并行实现的优势。

Better options fall in the domain of global optimization. 更好的选择属于全局优化领域。 Marc Claeson et al have devised the Optunity package, which uses particle swarm optimization. Marc Claeson等人设计了Optunity软件包,该软件包使用粒子群优化。 My research focuses on refinements of the Efficient Global Optimization algorithm ( EGO ), which builds up a Gaussian process as an approximation of the hyperparameter response surface and uses that to make educated predictions about which hyperparameter tuples are most likely to improve upon the current best estimate. 我的研究集中在高效全局优化算法( EGO )的改进上,该算法建立了一个高斯过程作为超参数响应面的近似值,并使用该方法对当前的最佳估计最有可能改善哪些超参数元组进行有根据的预测。

Imagine that you've evaluated the SVM at some hyperparameter tuple $(\\gamma, C)$ and it has some out-of-sample performance metric $y$. 想象一下,您已经在一些超参数元组$(\\ gamma,C)$上评估了SVM,并且它具有一些样本外性能指标$ y $。 An advantage to EGO-inspired methods is that it assumes that the values $y^*$ nearby $(\\gamma,C)$ will be "close" to $y$, so we don't necessarily need to spend time exploring those tuples nearby, especially if $y-y_{min}$ is very large (where $y_{min}$ is the smallest $y$ value we've discovered). 受EGO启发的方法的一个优点是,它假定附近$(\\ gamma,C)$的值y ^ * $将“接近”到y $,因此我们不必花时间探索这些值。附近的元组,尤其是在$ y-y_ {min} $非常大的情况下(其中$ y_ {min} $是我们发现的最小$ y $值)。 EGO will identify and evaluate the SVM at points where it estimates there is a high probability of improvement, so it will intelligently move through the hyper-parameter space: in the ideal case, it will skip over regions of low performance in favor of focusing on regions of high performance. EGO将在估计有改善的可能性的点上识别和评估SVM,因此它将智能地在超参数空间中移动:在理想情况下,它将跳过性能低下的区域,而专注于高性能区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM