Libsvm 是一個簡單、方便使用和普遍適用高效率的軟件,它是針對SVM分類的。這個文檔將解釋如何使用libsvm。
Libsvm 在http://www.csie.ntu.edu.tw/~cjlin/libsvm 可以得到。在你使用libsvm前請閱讀COPYRIGHT文件。
安裝
============
在 Unix系統中,`make'類是用來建立`svm-train' 和 `svm-classify' 程序的。不用看它們的使用說明,去運行它們。
在其它的系統中,使用pre-built binaries(Windows 的binaries 在子目錄`windows'下)或者求助`Makefile'創建它們,這很容易。
訓練和測試數據文件的格式是:
<label> <index1>:<value1> <index2>:<value2> ...
.
.
.
<label>(標籤)的值是+1或-1,<index>是一個從1開始的整型值,<value>是一個實數。
在這個打包文件裏有一個示例訓練數據文件(heart_scale) 。type`./svm-train heart_scale'和它的程序來讀訓練數據文件,然後輸出它的模型文件`heart_scale.model',這時你就可以type`./svm-classify heart_scale heart_scale.model output'看到訓練數據的分類速度。`output'文件包含了它的決策函數的輸出屬性。
在這個打包文件裏還有其它的一些有用的程序。
scale:
這是個工具,用來歸一化輸入的數據文件,Type `make scale'來創建它。
svm-toy:
這是一個簡單的圖形接口,用來顯示SVM在平面上如何分割數據。
你可以在窗口的底端輸入選項,選項的語法規則與`svm-train'相同。
在各自的目錄下創建類`make'。
你需要Qt圖書館來創建Qt版本。
(你可以從http://www.trolltech.com下載)
你需要GTK+圖書館來創建GTK版本。
(你可以從http://www.gtk.org下載)
我們用Visual C++來創建窗口視圖。
pre-built Windows binaries 在windows 子目錄下。
`svm-train' 的使用方法
=================
用法: svm-train [選項] training_set_file [model_file]
選項:
-c cost : 設置約束違反的代價C(C是個懲罰函數,是個常數)
-t kernel_type : 設置核函數的類型(缺省值 2)
0 --線性核函數
1 --多項式核函數
2 -- 徑向基核函數
3 -- sigmoid核函數
-d degree :設置核函數的度
-g gamma :在覈函數中設置gamma
-r coef0 :在多項式/sigmoid 核函數中設置coef0
-m cachesize :在MB中設置cache內存大小
-e epsilon : 設置終止條件的容許誤差(缺省值爲0.001)
Library 的使用
=============
這些函數和結構在頭文件`svm.h'中已經聲明瞭。
你需要#include "svm.h" 在你的C/C++源文件中,然後把你的程序鏈接到`svm.cpp',這時你就能看到`svm-train.c' 和`svm-classify.c',來顯示如何使用它們。
在分類測試數據之前,你需要用訓練數據構建一個SVM模型(`svm_model')。這個模型被保存在一個文件裏方便以後使用。一旦一個SVM模型創建好了,你就可以使用它來分類新的數據。
- Function: struct svm_model *svm_train(const struct svm_problem *prob,
const struct svm_parameter *param);
這個函數根據你給定的訓練數據和參數,創建和返回一個SVM模型。
struct svm_problem describes the problem:
struct svm_problem
{
int l;
signed char *y;
struct svm_node **x;
};
`l'爲訓練數據的個數,`y'是一個包含標籤(+1/-1)的數組,`x' 是數組的指針,每一個指針都分別代表(svm_node數組)一個訓練向量。
例如,有如下的一個訓練數據:
LABEL ATTR1 ATTR2 ATTR3 ATTR4 ATTR5
----- ----- ----- ----- ----- -----
1 0 0.1 0.2 0 0
-1 0 0.1 0.3 -1.2 0
1 0.4 0 0 0 0
1 0 0.1 0 1.4 0.5
-1 -0.1 -0.2 0.1 1.1 0.1
這時svm_problem 的各部分分別代表:
l = 5
y -> 1 -1 1 1 -1
x -> [ ] -> (2,0.1) (3,0.2) (-1,?)
[ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)
[ ] -> (1,0.4) (-1,?)
[ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)
[ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)
(index,value)儲存在結構`svm_node'中:
struct svm_node
{
int index;
double value;
};
index = -1 表明一個向量的結束。
struct svm_parameter describes the parameters of an SVM model:
struct svm_parameter
{
int kernel_type;
double degree; // for poly
double gamma; // for poly/rbf/sigmoid
double coef0; // for poly/sigmoid
// these are for training only
double cache_size; // in MB
double C;
double eps;
};
kernel_type can be one of LINEAR, POLY, RBF, SIGMOID.(幾種核函數的類型)
LINEAR: u'*v (線性)
POLY: (gamma*u'*v + coef0)^degree (多項式)
RBF: exp(-gamma*|u-v|^2) (徑向基函數)
SIGMOID: tanh(gamma*u'*v + coef0) (SIGMOID函數)
cache_size 爲核內存的大小,單位爲MB。
C是懲罰函數的值,爲一常數. (我們通常使用 1 到 1000)
Eps是終止條件的容錯誤差 (我們通常使用 0.001)
*注意* 因爲 svm_model 包含指針指向 svm_problem, 如果你仍然使用由svm_train()創建的svm_model ,你可以使用svm_problem 不釋放內存。(這裏翻譯的不好)
- Function: int svm_save_model(const char *model_file_name,
const struct svm_model *model);
這個函數將一個模型保存到一個文件裏,如果成功的話就返回0,如果不成功就返回-1.
- Function: struct svm_model *svm_load_model(const char *model_file_name);
這個函數從一個文件裏讀出一種模式,然後返回一個指針,如果找不到模式,就返回空指針.
- Function: double svm_classify(const struct svm_model *model,
const struct svm_node *x);
這個函數使用一個模式來分類一個文本向量x,返回決策函數的值(正用+1表示,負用 -1表示)
- Function: void svm_destroy_model(struct svm_model *model);
這個函數用一個模式來釋放內存.
其它的一些信息
============
Chih-Chung Chang and Chih-Jen Lin
Libsvm: Introduction and Benchmarks
http://www.csie.ntu.edu.tw/~cjlin/papers/q2.ps.gz
聲明:
This work was supported in part by the National Science
Council of Taiwan via the grant NSC 89-2213-E-002-013.
The authors thank Chih-Wei Hsu and Jen-Hao Lee
for many helpful discussions and comments.