LibLinear(SVM包)使用说明之(一)README
LibLinear(SVM包)使用說明之(一)README
LibLinear(SVM包)使用說明之(一)README
zouxy09@qq.com
http://blog.csdn.net/zouxy09
?
????? 本文主要是翻譯liblinear-1.93版本的README文件。里面介紹了liblinear的詳細使用方法。更多信息請參考:
???????http://www.csie.ntu.edu.tw/~cjlin/liblinear/
???????在這里我用到的是LibLinear的Matlab接口,這個在下一博文中介紹。
?
???????LIBLINEAR是一個簡單的求解大規模規則化線性分類和回歸的軟件包。它最討人喜歡的特點就是速度快!!!它目前支持:
- L2-regularized logisticregression/L2-loss support vector classification/L1-loss support vectorclassification
- L1-regularized L2-loss support vectorclassification/L1-regularized logistic regression
- L2-regularized L2-loss support vectorregression/L1-loss support vector regression.
?
一、何時用LIBLINEAR,而不是LIBSVM
???????對一些大數據來說,有沒有非線性映射,他們的性能差不多。如果不使用核,我們可以用線性分類或者回歸來訓練一個更大的數據集。這些數據往往具有非常高維的特征。例如文本分類Document classification。
???????注意:一般來說,liblinear是很快的,但它的默認配置在某些情況下可能會比較慢,例如數據沒有經過scale或者C很大的時候。可以參考SVM的說明中的附錄B來處理這些情況:
http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf
???????注意:如果你是個新手,而且你的數據集不是很大,那么建議你先考慮LIBSVM。
LIBSVM page:
http://www.csie.ntu.edu.tw/~cjlin/libsvm
????????更多也可以參考:
?????????liblinear與libsvm選擇
?????????Libsvm和Liblinear的使用經驗談
LIBLINEAR: Alibrary for large linear classification
?
二、快速入門
???????按“安裝”這一章節的說明來安裝LIBLINEAR。安裝完成后,就會得到兩個程序,train和predict,分別是用來訓練分類器和測試分類器的兩個程序。
???????對于數據格式。請查看LIBSVM的README文件。需要注意的是,特征的索引是從1開始的,而不是0 。
???????這個包還包含了一個例子,分類的數據是`heart_scale'。
???????運行`trainheart_scale',train程序就會讀取對應的訓練數據,然后輸出訓練好的分類器模型`heart_scale.model'。如果你有一個測試集,命名為heart_scale.t,那么你可以運行`predict heart_scale.t heart_scale.model output'來測試該分類器的識別準確率。這個output文件包含了分類器對測試集中每個樣本預測得到的對應的類標簽。
???????為了獲得好的性能,有時候需要先對數據進行scale。可以查看LIBSVM的`svm-scale'程序來獲得相關的信息。對于非常大和稀疏的數據,使用參數`-l 0'來保持訓練中數據的稀疏性。
?????????建議的訓練步驟(來源于libSVM,感覺對這個也有幫助,所以也標記在這):
1)將我們的訓練數據和測試數據轉換為該SVM軟件包支持的格式;
2)對數據進行簡單的尺度化scale;
3)先考慮用RBF核;
4)使用交叉檢驗方法去尋找最優的參數C和γ;
5)使用找到的最好的參數C和γ來訓練整個訓練集;
6)在測試集上測試。
?
三、安裝
???????在Unix系統,使用make來編譯`train' 和 `predict'程序。編譯完后,直接無參數運行這兩個程序,就會打印出相應的用法。
???????對于其他系統,我們通過Makefile來編譯。例如在windows下,我們可以參考本文中的'BuildingWindows binaries'章節。或者可以直接使用我們預編譯好的可執行文件。Windows的可執行文件在windows目錄下。
本軟件需要一些level-1 BLAS 子程序才能運行(BLAS(Basic LinearAlgebra Subprograms,基礎線性代數程序集)是一個應用程序接口(API)標準,用以規范發布基礎線性代數操作的數值庫(如矢量或矩陣乘法)。該程序集最初發布于1979年,并用于建立更大的數值程序包(如LAPACK)。在高性能計算領域,BLAS被廣泛使用。BLAS按照功能被分為三個級別,Level 1為矢量-矢量運算)。這些需要的函數已經包含在該軟件包的blas目錄里了。如果你的電腦已經存在了一個BLAS庫,那么你可以通過修改Makefile文件來使用你電腦上的BLAS庫。
注銷以下行:
???????#LIBS ?= -lblas
???????? 然后添加以下行:
???????LIBS ?= blas/blas.a
?
四、程序train的用法
用法:train [options] training_set_file [model_file]
options:
-s type : 對于多分類,指定使用的分類器(默認是1):
???????? ?0 -- L2-regularized logistic regression(primal)
???????? ?1 -- L2-regularized L2-loss support vectorclassification (dual)
???????? ?2 -- L2-regularized L2-loss support vectorclassification (primal)
???????? ?3 -- L2-regularized L1-loss support vectorclassification (dual)
???????? ?4 -- support vector classification by Crammerand Singer
???????? ?5 -- L1-regularized L2-loss support vectorclassification
???????? ?6 -- L1-regularized logistic regression
???????? ?7 -- L2-regularized logistic regression (dual)
? 對于回歸:
???????? 11-- L2-regularized L2-loss support vector regression (primal)
???????? 12-- L2-regularized L2-loss support vector regression (dual)
???????? 13-- L2-regularized L1-loss support vector regression (dual)
-c cost : 設置參數 C(默認是1)
-p epsilon : 設置epsilon-SVR的損失函數的參數epsilon(默認是0.1)
-e epsilon : 設置迭代終止條件的容忍度tolerance
???????? -s0 and 2
?????????????????? |f'(w)|_2<= eps*min(pos,neg)/l*|f'(w0)|_2,
?????????????????? f是primal 函數,pos/neg 是對應的正樣本和負樣本數目(默認是0.01)
???????? -s11
?????????????????? |f'(w)|_2<= eps*|f'(w0)|_2 (默認是0.001)
???????? -s1, 3, 4 and 7
?????????????????? Dualmaximal violation <= eps; 和 libsvm相似(默認是0.1)
???????? -s5 and 6
?????????????????? |f'(w)|_inf<= eps*min(pos,neg)/l*|f'(w0)|_inf,
?????????????????? f是primal 函數,pos/neg 是對應的正樣本和負樣本數目(默認是0.01)
???????? -s12 and 13\n"
?????????????????? |f'(alpha)|_1<= eps |f'(alpha0)|,
?????????????????? f是dual(對偶)函數(默認是0.1)
-B bias : 如果bias >= 0,那樣樣本x變為[x; bias],如果小于0,則不增加bias項(默認是-1)
-wi weight: 調整不同類別的參數C的權值(具體見README)
-v n: n-fold交叉檢驗模式。它隨機的將數據劃分為n個部分,然后計算它們的交叉檢驗準確率。
-q : 安靜模式(無輸出信息)
?
Formulations公式(優化問題):
For L2-regularized logistic regression (-s0), we solve
min_w w^Tw/2 + C\sum log(1 + exp(-y_i w^Tx_i))
?
For L2-regularized L2-loss SVC dual (-s 1),we solve
min_alpha? 0.5(alpha^T (Q + I/2/C) alpha) - e^T alpha
???s.t.?? 0 <= alpha_i,
?
For L2-regularized L2-loss SVC (-s 2), wesolve
min_w w^Tw/2 + C\sum max(0, 1- y_i w^Tx_i)^2
?
For L2-regularized L1-loss SVC dual (-s 3),we solve
min_alpha? 0.5(alpha^T Q alpha) - e^T alpha
???s.t.?? 0 <= alpha_i <= C,
?
For L1-regularized L2-loss SVC (-s 5), wesolve
min_w \sum |w_j|+ C \sum max(0, 1- y_i w^Tx_i)^2
?
For L1-regularized logistic regression (-s6), we solve
min_w \sum |w_j|+ C \sum log(1 + exp(-y_i w^Tx_i))
?
For L2-regularized logistic regression (-s7), we solve
min_alpha? 0.5(alpha^T Q alpha) + \sumalpha_i*log(alpha_i) + \sum (C-alpha_i)*log(C-alpha_i) - a constant
???s.t.?? 0 <= alpha_i <= C,
where, Q is a matrix with Q_ij = y_i y_jx_i^T x_j.
?
For L2-regularized L2-loss SVR (-s 11), wesolve
min_w w^Tw/2 + C\sum max(0, |y_i-w^Tx_i|-epsilon)^2
?
For L2-regularized L2-loss SVR dual (-s12), we solve
min_beta? 0.5(beta^T (Q + lambda I/2/C) beta) - y^Tbeta + \sum |beta_i|
?
For L2-regularized L1-loss SVR dual (-s13), we solve
min_beta? 0.5(beta^T Q beta) - y^T beta + \sum |beta_i|
???s.t.?? -C <= beta_i <= C,
where, Q is a matrix with Q_ij = x_i^T x_j.
?
如果bias >= 0,那么w變為[w;w_{n+1}] ,x 變為[x; bias]。
primal-dual的關系表明了-s 1 和 -s 2學習到的是同樣的模型。-s0 和 -s 7,-s 11 和 -s 12也是。
???????我們實現了一對多的多分類方法。在訓練i類和non_i類的時候,它們的參數C分別是(weight from -wi)*C和C。如果只有兩類,我們只訓練一個模型。這時候使用weight1*C和weight2*C。看下面的例子。
???????我們還實現了多類SVM byCrammer and Singer (-s 4):
min_{w_m, \xi_i}? 0.5 \sum_m ||w_m||^2 + C \sum_i \xi_i
???s.t.? w^T_{y_i} x_i - w^T_m x_i>= \e^m_i - \xi_i \forall m,i
where, ?e^m_i = 0 if y_i? = m,
?????e^m_i = 1 if y_i != m,
?
這里我們解dual 問題:
min_{\alpha}? 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_me^m_i alpha^m_i
???s.t.? \alpha^m_i <= C^m_i\forall m,i , \sum_m \alpha^m_i=0 \forall i
?
where, w_m(\alpha) = \sum_i \alpha^m_i x_i,
and C^m_i = C if m? = y_i,
???C^m_i = 0 if m != y_i.
?
五、程序predict的用法
用法:predict [options] test_file model_file output_file
options:
-b probability_estimates: 是否輸出概率估計。默認是0,不輸出。只對logistic回歸有用
-q : 安靜模式(無輸出信息)
?
需要注意的是-b只在預測階段用到。這個和LIBSVM不同。
?
六、例子
> train data_file
默認參數的時候,訓練的是L2損失函數的線性SVM
?
> train -s 0 data_file
-s 0指定訓練一個logistic回歸模型
?
> train -v 5 -e 0.001 data_file
-v 5指定5-fold的交叉檢驗模式。-e 0.001指定一個比默認值更小的迭代停止容忍度。
?
> train -c 10 -w1 2 -w2 5 -w3 2four_class_data_file
-c 10指定參數C是10,-w1 2指定第一類的權值w是2,這時候對應的C是w*C,其他同。例如我們要訓練四類。-w1 2 -w2 5 -w3 2分別指定了類1的C=w*C=2*10=20,類2的C=w*C=5*10=50,類3同。類4沒有指定,所以是C。其他對應的負類的C都是10 。(我們要訓練四類的分類器,就需要四個二分類器,正負樣本各需要一個參數C)
Train four classifiers:
positive??????? negative??????? Cp?????Cn
class 1???????? class 2,3,4.??? 20?????10
class 2????????class 1,3,4.??? 50?????10
class 3???????? class 1,2,4.??? 20?????10
class 4???????? class 1,2,3.??? 10?????10
?
> train -c 10 -w3 1 -w2 5two_class_data_file
如果只有兩類,我們只訓練一個模型。這時候兩類的C值分別是10 和 50。
?
> predict -b 1 test_file data_file.modeloutput_file
-b 1指定輸出每類估計得到的概率值。只對logistic回歸有效。
?
七、庫的用法
1、函數train()
-Function:?model* train(const struct problem *prob,
??????????????? const struct parameter *param);
這個函數根據給定的訓練數據和參數構造一個線性分類器或者回歸模型并返回。
?
2、結構體struct problem
??? 結構體structproblem 描述我們要求解的問題:
???????struct problem
???????{
???????????int l, n;
???????????int *y;
???????????struct feature_node **x;
???????????double bias;
???????};
???????? l表示訓練數據的個數。如果bias>= 0,那么我們會在每個樣本的末尾添加一個額外的值,這時候,樣本x變為[x; bias]。n表示特征(樣本)的維數(包括bias)。y是保存了目標值(期望輸出或者樣本標簽)的數組。x是一個指針數組,每個元素指向一個保存了一個樣本的稀疏表示的數組(結構體feature_node的數組)。
???????? 例如,如果我們有以下的訓練數據:
???LABEL?????? ATTR1?? ATTR2??ATTR3?? ATTR4?? ATTR5
???-----?????? -----?? -----??-----?? -----?? -----
???1?????????? 0?????? 0.1????0.2???? 0?????? 0
???2?????????? 0?????? 0.1????0.3??? -1.2???? 0
???1?????????? 0.4???? 0??????0?????? 0?????? 0
???2?????????? 0?????? 0.1????0?????? 1.4???? 0.5
???3????????? -0.1??? -0.2????0.1???? 1.1???? 0.1
還有bias = 1,那這個問題描述的結構體struct problem就可以描述為:
??? l= 5共五個樣本
??? n= 6 特征的維數5+1=6
??? y-> 1 2 1 2 3 每個樣本對應的標簽值
??? x-> [ ] -> (2,0.1) (3,0.2) (6,1) (-1,?)
????????[ ] -> (2,0.1) (3,0.3) (4,-1.2) (6,1) (-1,?)
????????[ ] -> (1,0.4) (6,1) (-1,?)
????????[ ] -> (2,0.1) (4,1.4) (5,0.5) (6,1) (-1,?)
????????[ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (6,1) (-1,?)
x保存的是每個樣本的稀疏表示,也就是0值就不保存,只保存非零值和其對應的索引號。例如第一個樣本,第一個特征值是0,不管,第二個特征值是0.1,所以保存格式為(索引號, 特征值),也就是(2,0.1)。同理,第三個特征值是0.2,所以保存(3,0.2),后面同理。然后通過一個-1的索引號來標記這個樣本的結束。保存為(-1,?)。
?
3、結構體struct parameter
????? 結構體structparameter 描述一個線性分類器或者回歸模型的參數:
???????struct parameter
???????{
??????????????? int solver_type;
?
??????????????? /* these are for training only*/
??????????????? double eps;???????????? /* stopping criteria */
??????????????? double C;
???????????????int nr_weight;
??????????????? int *weight_label;
??????????????? double* weight;
??????????????? double p;
???????};
????? 下面介紹下各個成員變量,也就是各個參數,這些參數和在命令行中給train傳入的參數是一致的:
solver_type是solver的類型,可以是以下的其中一種:
L2R_LR,L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS,L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL, L2R_L2LOSS_SVR, L2R_L2LOSS_SVR_DUAL,L2R_L1LOSS_SVR_DUAL.
? 對于分類器:
???L2R_LR???????????????L2-regularized logistic regression (primal)
???L2R_L2LOSS_SVC_DUAL??L2-regularized L2-loss support vector classification (dual)
???L2R_L2LOSS_SVC???????L2-regularized L2-loss support vector classification (primal)
???L2R_L1LOSS_SVC_DUAL??L2-regularized L1-loss support vector classification (dual)
???MCSVM_CS????????????? supportvector classification by Crammer and Singer
???L1R_L2LOSS_SVC???????L1-regularized L2-loss support vector classification
???L1R_LR???????????????L1-regularized logistic regression
???L2R_LR_DUAL??????????L2-regularized logistic regression (dual)
? 對于回歸模型:
???L2R_L2LOSS_SVR???????L2-regularized L2-loss support vector regression (primal)
???L2R_L2LOSS_SVR_DUAL??L2-regularized L2-loss support vector regression (dual)
???L2R_L1LOSS_SVR_DUAL??L2-regularized L1-loss support vector regression (dual)
?
C是約束violation的代價參數
P是supportvector regression的損失靈敏度
???eps是迭代停止條件
?
?????? nr_weight, weight_label,和 weight 用來改變對一些類的懲罰。默認是1 。這對于使用unbalanced 的輸入數據或者不對稱的誤分類代價來訓練分類器時是很有效的。
?????? nr_weight是數組weight_label 和 weight的元素個數。每個weight[i]對應weight_label[i]。表示類weight_label[i] 的懲罰會被weight[i]進行尺度化。也就是C= weight_label[i]*C。
???????如果你不需要對任何類改變懲罰,直接設置nr_weight為0即可。
注意:為了避免錯誤的參數設置,在調用train()之前最好先調用check_parameter()來檢查參數的正確性。
?
4、結構體struct model
結構體struct model 保存訓練得到的模型:
???????struct model
???????{
??????????????? struct parameter param;
??? ????????????int nr_class;?????????? /* number of classes */
??????????????? int nr_feature;
??????????????? double *w;
??????????????? int *label;???????????? /* label of each class */
??????????????? double bias;
???????};
?
????param描述獲得這個模型對應的參數設置。
????nr_class 和 nr_feature分別是類和特征的個數。對于回歸來說,nr_class = 2
???? 數組w 的大小是nr_feature*nr_class,是每個特征對應的權值。對于多分類,我們使用一對多的方法,所以每個特征都會對應nr_class 個類的特征權值。權值的保存通過以下方式來組織:
????+------------------+------------------+------------+
????| nr_class weights | nr_class weights |?...
????| for 1st feature? | for 2ndfeature? |
????+------------------+------------------+------------+
????? 如果bias >= 0,x 變為 [x; bias]。特征的數目或者維數就會加1,所以數組w的大小就變為(nr_feature+1)*nr_class。Bias的值保存在bias這個變量中。
???? 數組 label 保存的是類的標簽值。
?
5、其他函數
-Function:?void cross_validation(const problem*prob, const parameter *param,
int nr_fold, double *target);
交叉檢驗函數。數據會被劃分為nr_fold個folds。對每個fold,用剩余的fold去訓練模型,然后用這個fold來校驗,這個校驗過程得到的預測標簽都會保存在target這個數組中。
???
-Function:?double predict(const model *model_, constfeature_node *x);
???????預測函數。對一個分類模型,傳入一個樣本x,會返回預測到的對應的類。對于一個回歸模型,就會返回一個由模型計算得到的函數值。
?
-Function:?double predict_values(const struct model*model_,
???????????const struct feature_node *x, double* dec_values);
???????這個函數得到nr_w個保存在數組dec_values的決策值。當使用回歸模型或者二分類時,nr_w=1。一個例外的情況是Crammer and Singer (-s 4)的多分類SVM。對于其他情況nr_w是類的數目。
???????我們實現了one-vs-therest一對多的多分類(-s 0,1,2,3,5,6,7)和由Crammer and Singer (-s 4)實現的多分類SVM。該函數返回具有最高決策值的類別。
???
-Function:?double predict_probability(const structmodel *model_,
???????????const struct feature_node *x, double* prob_estimates);
????????該函數得到nr_class的概率估計值。保存在prob_estimates數組中。nr_class可以通過函數get_nr_class獲得。該函數返回最高概率對應的類別。概率的輸出只在logistic回歸時有效。
?? ?
-Function:?int get_nr_feature(const model *model_);
????????該函數返回模型的attributes的個數。
???
-Function:?int get_nr_class(const model *model_);
????????該函數返回模型的類的個數。如果是回歸模型,返回2.
?
-Function:?void get_labels(const model *model_, int*label);
????????該函數輸出標簽的名字到一個label的數組中。
?
-Function:?const char *check_parameter(const structproblem *prob,
???????????const struct parameter *param);
????????該函數檢測參數的有效性。其需要在train() 和 cross_validation()前調用。如果參數有效,那么返回NULL,否則返回其他的錯誤信息。
?
-Function:?int save_model(const char*model_file_name,
???????????const struct model *model_);
????????該函數將模型保存到一個文件中。返回0表示成功,-1表示失敗。
?
- Function:struct model *load_model(const char*model_file_name);
????????該函數從一個文件中加載模型。指針為空,表示加載失敗。
?
-Function:?void free_model_content(struct model*model_ptr);
????????該函數清理內存。在一個模型結構的入口處可以調用。
??
-Function:?void free_and_destroy_model(struct model**model_ptr_ptr);
????????該函數干掉一個模型,并釋放其占用的內存。
?
-Function:?void destroy_param(struct parameter*param);
????????該函數釋放參數結構體占用的內存。
?
-Function:?void set_print_string_function(void(*print_func)(const char *));
????????用戶可以指定輸出的格式。set_print_string_function(NULL);將信息輸入到stdout。
?
八、編譯Windows下可執行文件
Windows下可執行文件在目錄windows下。可以通過VisualC++來編譯得到。編譯過程如下:
1、打開dos命令行窗口,定位到liblinear目錄下。如果VC++的環境變量還沒設置,敲入以下命令來實現:
"C:\Program Files\Microsoft VisualStudio 10.0\VC\bin\vcvars32.bat"
你可能需要根據你的VC++的版本來稍微修改上述命令。
?
2、輸入
nmake -f Makefile.win clean all
?
九、其他接口
? ? ? ? ?MATLAB/OCTAVE 接口查看matlab目錄下的README文件。PYTHON接口查看python'目錄下的README文件。
?
十、其他信息
? ? ? ? 如果你覺得LIBLINEAR 對你有幫助的話,pleasecite it as:
R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R.Wang, and C.-J. Lin.
LIBLINEAR: A Library for Large LinearClassification, Journal of
Machine Learning Research 9(2008),1871-1874. Software available at
http://www.csie.ntu.edu.tw/~cjlin/liblinear
?
For any questions and comments, please sendyour email to
cjlin@csie.ntu.edu.tw
轉載于:https://www.cnblogs.com/yymn/p/4589196.html
總結
以上是生活随笔為你收集整理的LibLinear(SVM包)使用说明之(一)README的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦断代码
- 下一篇: Reveal:分析iOS UI该武器