SVMrank——Support Vector Machine for Ranking(SVMrank——使用svm的排序)

preface:最近所忙的任务需要用到排序,同仁提到SVMrank这个工具,好像好强大的样纸,不过都快十年了,还有其他ranklib待了解。

原文链接:SVMrank,百度搜索svm rank即可。


SVMrank

基于支持向量机的排序
康奈尔大学

计算机系

版本号:1.00

日起:2009年3月21



总览

根据[Joachims, 2002c]论文中定义了,svm_rank是SVMstruct的一种实例,用于有效地训练排名。SVM_rank使用"-z p"参数,可以解决跟SVMlight同样的最优化问题,并且更快。在LETOR 3.0数据集上,需要花费约1秒钟的时间训练任何数据集(在任意折交叉验证中)。在[Joachims, 2006]论文中提到,用于解决二次规划问题的算法是ROC面积优化算法的扩展,为了排序使用了SVMstruct中的一种。然而,因为我不想在SVMrank上花费一下午编程,我只能实现一个简单的数据库排序,一个平方次数量上的排序(不是[Joachims, 2006]论文中描述的O(k*logk)时间内解决的)。使用[Herbrich et al, 1999]论文中提到的回归排序,虽然算法的实现不是很容易,但它仍然是最快的小型排序(比如说k<1000),并且和排名的数量成线性比(比如查询操作)。


源代码

这些程序免费为科学家使用。如果你想基于商业目的使用程序,请联系我,该软件不能在未经作者的事先许可使用。如果在您的科研工作使用SVMrank,请引作:

  • T. Joachims, Training Linear SVMs in Linear Time, Proceedings of the ACM Conference on Knowledge Discovery and Data Mining (KDD), 2006. [Postscript (gz)]  [PDF]

软件基于linux系统下,使用gcc编译,但同时也可以在Solaris,Cygwin,Windows(使用MinGW)和Mac下使用。源代码可以通过以下链接下载:

http://download.joachims.org/svm_rank/current/svm_rank.tar.gz

如果你想要通过二进制文件使用,你可以通过在以下系统中分别下载它们:

请给我发邮件并且让我知道你下载了。归档文件中包含了最新版本SVMrank,其中包括SVMstruct的源代码和SVMlight二次优化的源代码,使用以下shell命令解压缩归档文件:

      gunzip –c svm_rank.tar.gz | tar xvf –

这将扩展归档文件到当前目录,它现在包含所有相关文件。你可以用命令编译SVMrank:

       make

这将产生svm_rank_learn和svm_rank_classify两个可执行命令,如果系统不能正常编译,参考FAQ。


如何使用SVMrank

SVMrank包含一个学习模型(svm_rank_learn)和一个预测模型(svm_rank_classify)。SVMrank用跟SVMlight相同的输入输出文件格式,并且它也跟SVMlight使用一样的"-z p"参数,你可以训练模型:

      svm_rank_learn -c 20.0 train.dat model.dat

在训练集training.dat上训练一个排序的SVM,用正则化参数c,设置为20.0, 训练学习到规则输出到model.dat文件中。然而,SVMrank参数c的解释跟SVMlight不同。特别得,Clight = Crank/n,n是训练集中query(比如在训练集中不同query id)的个数。在SVMstruct和SVMlight中,大多数参数介绍如下所述。只有少部分特定参数列表的介绍在下面。

General Options:
         -?          -> this help
         -v [0..3]   -> verbosity level (default 1)
         -y [0..3]   -> verbosity level for svm_light (default 0)
Learning Options:
         -c float    -> C: trade-off between training error
                        and margin (default 0.01)
         -p [1,2]    -> L-norm to use for slack variables. Use 1 for L1-norm,
                        use 2 for squared slacks. (default 1)
         -o [1,2]    -> Rescaling method to use for loss.
                        1: slack rescaling
                        2: margin rescaling
                        (default 2)
         -l [0..]    -> Loss function to use.
                        0: zero/one loss
                        ?: see below in application specific options
                        (default 1)
Optimization Options (see [2][5]):
         -w [0,..,9] -> choice of structural learning algorithm (default 3):
                        0: n-slack algorithm described in [2]
                        1: n-slack algorithm with shrinking heuristic
                        2: 1-slack algorithm (primal) described in [5]
                        3: 1-slack algorithm (dual) described in [5]
                        4: 1-slack algorithm (dual) with constraint cache [5]
                        9: custom algorithm in svm_struct_learn_custom.c
         -e float    -> epsilon: allow that tolerance for termination
                        criterion (default 0.001000)
         -k [1..]    -> number of new constraints to accumulate before
                        recomputing the QP solution (default 100)
                        (-w 0 and 1 only)
         -f [5..]    -> number of constraints to cache for each example
                        (default 5) (used with -w 4)
         -b [1..100] -> percentage of training set for which to refresh cache
                        when no epsilon violated constraint can be constructed
                        from current cache (default 100%) (used with -w 4)
SVM-light Options for Solving QP Subproblems (see [3]):
         -n [2..q]   -> number of new variables entering the working set
                        in each svm-light iteration (default n = q).
                        Set n < q to prevent zig-zagging.
         -m [5..]    -> size of svm-light cache for kernel evaluations in MB
                        (default 40) (used only for -w 1 with kernels)
         -h [5..]    -> number of svm-light iterations a variable needs to be
                        optimal before considered for shrinking (default 100)
         -# int      -> terminate svm-light QP subproblem optimization, if no
                        progress after this number of iterations.
                        (default 100000)
Kernel Options:
         -t int      -> type of kernel function:
                        0: linear (default)
                        1: polynomial (s a*b+c)^d
                        2: radial basis function exp(-gamma ||a-b||^2)
                        3: sigmoid tanh(s a*b + c)
                        4: user defined kernel from kernel.h
         -d int      -> parameter d in polynomial kernel
         -g float    -> parameter gamma in rbf kernel
         -s float    -> parameter s in sigmoid/poly kernel
         -r float    -> parameter c in sigmoid/poly kernel
         -u string   -> parameter of user defined kernel
Output Options:
         -a string   -> write all alphas to this file after learning
                        (in the same order as in the training set)
常用参数:

-?:查看帮助

-v:冗长度级别,取值范围为[1,3]默认为1。

-y:svm_light冗长度级别,取值范围为[1,3]默认为0。

学习参数:

-c:权衡训练错误率和边缘值,为浮点数,默认为0.01。

-p:L范式用于松弛变量。1表示L1范式,2表示平方松弛,默认为1。

-o:用于重新调整损失函数。1表示重新调整松弛变量,2表示重新调整边缘值。

-l:用于损失函数。0表示0/1损失,?表示应用于具体参数。默认为1。

最优参数(参考论文[2][5]):

-w:选择结构学习算法,默认为3

0:表示n-松弛算法,在论文[2]中有描述。

1:表示带启发式缩减的n-松弛算法。

2:表示1-松弛算法(主要使用),在论文[5]中有描述。

3:表示1-松弛算法(双核),在论文[5]中有描述。

4:表示带缓存约束的1-松弛算法(双核),在论文[5]中有描述。

9:表示在svm_struct_learn_custom.c文件中自定义算法。

-e:epsilon,表示允许终止阈值,默认为0.001000.

-k:。。。

-f:。。。

-b:。。。

用二次规划子问题(QP子问题)解决的SVMlight参数说明(参考论文[3]):

-n:

-m:

-h:

-#:

核参数

-t

-d

-g

-s

-r

-u

输出参数

-a

特定应用程序选项:

下面损失函数可以使用-l参数进行选择:

1.交换所有查询对的分数的总和。

2.交换所有查询对的分数。

注:SVMlight在'-z p'模式下,等同于SVM-rank用L1损失函数,c_light = c_rank/n,n为训练排名的实体个数。

SVMrank学习一个不带偏置的线性分类规则(比如:规则W*X不带明确阈值)。通过“-l”参数可以选择被优化的损失函数。损失函数1,用于SVMlight排名模型,并且它使得总交换排名最优。损失函数2,是损失函数1的正则化形式。对每个query,损失函数通过最大化交换查询对来划分交换对。

你可以使用-t参数,就像在SVMligth中使用一样。但是它比较慢,并且最好还是说不要用SVMlight。

训练文件和测试文件的文件格式与SVMlight相同(见进一步细节),所不同的是输入文件中的行必须通过增加QID进行排序。第一行可能包括注释,如果包含#,那么将被忽略。下面每个行代表一个训练例子,并且格式如下:

<target> .=. <float>

<line> .=. <target> qid:<qid> <feature>:<value> <feature>:<value> ... <feature>:<value> # <info>

<qid> .=. <positive integer>

<feature> .=. <positive integer>

<value> .=. <float>

<info> .=. <string>

目标值与各特征/值对必须通过分隔符隔开。特征/值对必须是递增顺序的。特征值为0则被跳过。目标值为用来作为每个query例子的顺序。隐式地,目标值被用来产生query对的偏好约束,可参考[Joachims, 2002c]论文。对每个不同的目标值,一个偏好约束包括所有实体对。特别特征“qid”可以被用来重新产生约束。两个例子可以被认为是实体对偏好约束当且仅当“qid”值是相同的,例如,给定一下文件例子:

3 qid:1 1:1 2:1 3:0 4:0.2 5:0 # 1A

2 qid:1 1:0 2:0 3:1 4:0.1 5:1 # 1B 

1 qid:1 1:0 2:1 3:0 4:0.4 5:0 # 1C

1 qid:1 1:0 2:0 3:1 4:0.3 5:0 # 1D  

1 qid:2 1:0 2:0 3:1 4:0.2 5:0 # 2A  

2 qid:2 1:1 2:0 3:1 4:0.4 5:0 # 2B 

1 qid:2 1:0 2:0 3:1 4:0.1 5:0 # 2C 

1 qid:2 1:0 2:0 3:1 4:0.2 5:0 # 2D  

2 qid:3 1:0 2:0 3:1 4:0.1 5:1 # 3A 

3 qid:3 1:1 2:1 3:0 4:0.3 5:0 # 3B 

4 qid:3 1:1 2:0 3:0 4:0.4 5:1 # 3C 

1 qid:3 1:0 2:1 3:1 4:0.5 5:0 # 3D

可以产生下列实体对约束(例子后面#之后的)

1A>1B, 1A>1C, 1A>1D, 1B>1C, 1B>1D, 2B>2A, 2B>2C, 2B>2D, 3C>3A, 3C>3B, 3C>3D, 3B>3A, 3B>3D, 3A>3D

svm_rank_svm通过train.dat学习到模型,模型文件被写到model.dat文件。然后预测测试集,svm_rank_classify读入文件。svm_rank_classifys使用如下:

svm_rank_classify test.dat model.dat predictions

对每行测试文件,预测排名,写到predictions文件中,在predictions文件中的每行对应test.dat文件。对于这些得分,排名可以排序。


开始:问题例子

你可以在以下链接中找到训练集和测试集:

http://download.joachims.org/svm_light/examples/example3.tar.gz

它包含3个排序和4个例子。在测试集中,也包含了4个例子,解压即可,产生example3文件夹,根据以下命令执行:

svm_rank_learn -c 3 example3/train.dat example3/model 

svm_classify example3/test.dat example3/model example3/predictions

注:在windows下,带.exe执行,如svm_rank_learn.exe xxxxxx。

卤煮注:

train.dat:

# query 1
3 qid:1 1:1 2:1 3:0 4:0.2 5:0
2 qid:1 1:0 2:0 3:1 4:0.1 5:1
1 qid:1 1:0 2:1 3:0 4:0.4 5:0
1 qid:1 1:0 2:0 3:1 4:0.3 5:0
# query 2
1 qid:2 1:0 2:0 3:1 4:0.2 5:0
2 qid:2 1:1 2:0 3:1 4:0.4 5:0
1 qid:2 1:0 2:0 3:1 4:0.1 5:0
1 qid:2 1:0 2:0 3:1 4:0.2 5:0
# query 3
2 qid:3 1:0 2:0 3:1 4:0.1 5:1
3 qid:3 1:1 2:1 3:0 4:0.3 5:0
4 qid:3 1:1 2:0 3:0 4:0.4 5:1
1 qid:3 1:0 2:1 3:1 4:0.5 5:0

test.dat:

4 qid:4 1:1 2:0 3:0 4:0.2 5:1
3 qid:4 1:1 2:1 3:0 4:0.3 5:0
2 qid:4 1:0 2:0 3:0 4:0.2 5:1
1 qid:4 1:0 2:0 3:1 4:0.2 5:0

predictions:

2.43987615
1.40287793
0.91675182
-0.56012331

输出预测文件可以对测试集中的例子排名。如果你那么做,你将会看到它成功地预测对了排名。在预测文件中的值没有绝对的意义。他们仅仅只是为了排序。在SVMlight中等同于:

svm_learn -z p -c 1 example3/train.dat example3/model 

注意参数c的不同值,因为我们有3个训练排序。

它也可以查看SVM排序的训练错误。SVM排序的训练错误等同于在训练模型中的错误排序。找到这些排序对,可以应用模型在训练集文件中:

svm_rank_classify example3/train.dat example3/model example3/predictions.train

再次提醒,预测文件隐式地表明了模型给出的排序。模型排序对所有训练文件正确地排序了。

注意排序是完全对相同qid例子进行的。也注意目标值(在数据集文件中的每行第一个值)是用于定义例子的顺序。它的绝对值不重要,只要排序相对于另一个相同的qid仍然是相同的即可。

声明

这个软件是免费的仅用于非商业用途。它不能未经作者许可之前进行分配。作者不负责因使用本软件的影响。



猜你喜欢

转载自blog.csdn.net/u010454729/article/details/51037898
今日推荐