LibLinear(SVM包)使用说明之(一)README

LibLinear(SVM包)使用说明之(一)README

zouxy09@qq.com

本文主要是翻译liblinear-1.93版本的README文件。里面介绍了liblinear的详细使用方法。更多信息请参考:

~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来处理这些情况:

~cjlin/papers/guide/guide.pdf

注意:如果你是个新手,而且你的数据集不是很大,那么建议你先考虑LIBSVM。

LIBSVM page:

~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也是。

在时光的激流中,我们总会长大。

LibLinear(SVM包)使用说明之(一)README

相关文章:

你感兴趣的文章:

标签云: