[LTR] RankLib.jar 包介紹

来源:https://www.cnblogs.com/memento/archive/2018/07/31/9398047.html
-Advertisement-
Play Games

一、介紹 RankLib.jar 是一個學習排名(Learning to rank)演算法的庫,目前已經實現瞭如下幾種演算法: + MART + RankNet + RankBoost + AdaRank + Coordinate Ascent + LambdaMART + ListNet + Rand ...


一、介紹

RankLib.jar 是一個學習排名(Learning to rank)演算法的庫,目前已經實現瞭如下幾種演算法:

  • MART
  • RankNet
  • RankBoost
  • AdaRank
  • Coordinate Ascent
  • LambdaMART
  • ListNet
  • Random Forests
  • Linear regression

二、jar 包

Usage: java -jar RankLib.jar <Params>
Params:
  [+] Training (+ tuning and evaluation)
        # 訓練數據
        -train <file>           Training data
        # 指定排名演算法
        -ranker <type>          Specify which ranking algorithm to use
                                0: MART (gradient boosted regression tree)
                                1: RankNet
                                2: RankBoost
                                3: AdaRank
                                4: Coordinate Ascent
                                6: LambdaMART
                                7: ListNet
                                8: Random Forests
                                9: Linear regression (L2 regularization)
        # 特征描述文件,列出要學習的特征,每行一個特征,預設使用所有特征
        [ -feature <file> ]     Feature description file: list features to be considered by the learner, each on a separate line
                                If not specified, all features will be used.
        # 
        [ -metric2t <metric> ]  Metric to optimize on the training data. Supported: MAP, NDCG@k, DCG@k, P@k, RR@k, ERR@k (default=ERR@10)
        [ -gmax <label> ]       Highest judged relevance label. It affects the calculation of ERR (default=4, i.e. 5-point scale {0,1,2,3,4})
        
        [ -silent ]             Do not print progress messages (which are printed by default)
        # 是否在驗證數據集上調整模型
        [ -validate <file> ]    Specify if you want to tune your system on the validation data (default=unspecified)
                                If specified, the final model will be the one that performs best on the validation data
        # 訓練-驗證數據集的分割比例
        [ -tvs <x \in [0..1]> ] If you don't have separate validation data, use this to set train-validation split to be (x)(1.0-x)
        # 學習模型保存到指定文件
        [ -save <model> ]       Save the model learned (default=not-save)
        # 是否要在數據上測試訓練的模型
        [ -test <file> ]        Specify if you want to evaluate the trained model on this data (default=unspecified)
        # 訓練-測試數據集的分割比例
        [ -tts <x \in [0..1]> ] Set train-test split to be (x)(1.0-x). -tts will override -tvs
        # 預設與 metric2t 一致
        [ -metric2T <metric> ]  Metric to evaluate on the test data (default to the same as specified for -metric2t)
        # 歸一化特征向量,方法包括求和歸一化,均值/標準差歸一化,最大值/最小值歸一化
        [ -norm <method>]       Normalize all feature vectors (default=no-normalization). Method can be:
                                sum: normalize each feature by the sum of all its values
                                zscore: normalize each feature by its mean/standard deviation
                                linear: normalize each feature by its min/max values
        # 在訓練數據集上執行交叉驗證
        [ -kcv <k> ]            Specify if you want to perform k-fold cross validation using the specified training data (default=NoCV)
                                -tvs can be used to further reserve a portion of the training data in each fold for validation
        # 交叉驗證訓練庫模型的目錄
        [ -kcvmd <dir> ]        Directory for models trained via cross-validation (default=not-save)
        
        [ -kcvmn <model> ]      Name for model learned in each fold. It will be prefix-ed with the fold-number (default=empty)

    [-] RankNet-specific parameters # 特定參數
        # 訓練迭代次數
        [ -epoch <T> ]          The number of epochs to train (default=100)
        # 隱含層個數
        [ -layer <layer> ]      The number of hidden layers (default=1)
        # 每層隱含節點個數
        [ -node <node> ]        The number of hidden nodes per layer (default=10)
        # 學習率
        [ -lr <rate> ]          Learning rate (default=0.00005)

    [-] RankBoost-specific parameters # 特定參數
        # 訓練迭代次數
        [ -round <T> ]          The number of rounds to train (default=300)
        # 搜索的閾值候選個數
        [ -tc <k> ]             Number of threshold candidates to search. -1 to use all feature values (default=10)

    [-] AdaRank-specific parameters # 特定參數
        # 訓練迭代次數
        [ -round <T> ]          The number of rounds to train (default=500)
        # 
        [ -noeq ]               Train without enqueuing too-strong features (default=unspecified)
        # 連續兩輪學習之間的誤差
        [ -tolerance <t> ]      Tolerance between two consecutive rounds of learning (default=0.002)
        # 一個特征可以被連續選擇而不改變性能的最大次數
        [ -max <times> ]        The maximum number of times can a feature be consecutively selected without changing performance (default=5)

    [-] Coordinate Ascent-specific parameters # 特定參數
        [ -r <k> ]              The number of random restarts (default=5)
        [ -i <iteration> ]      The number of iterations to search in each dimension (default=25)
        [ -tolerance <t> ]      Performance tolerance between two solutions (default=0.001)
        [ -reg <slack> ]        Regularization parameter (default=no-regularization)

    [-] {MART, LambdaMART}-specific parameters # 特定參數
        # 樹的個數
        [ -tree <t> ]           Number of trees (default=1000)
        # 一個葉子的樣本個數
        [ -leaf <l> ]           Number of leaves for each tree (default=10)
        # 學習率
        [ -shrinkage <factor> ] Shrinkage, or learning rate (default=0.1)
        # 樹分割時的候選特征個數
        [ -tc <k> ]             Number of threshold candidates for tree spliting. -1 to use all feature values (default=256)
        # 一個葉子最少的樣本個數
        [ -mls <n> ]            Min leaf support -- minimum #samples each leaf has to contain (default=1)
        [ -estop <e> ]          Stop early when no improvement is observed on validaton data in e consecutive rounds (default=100)

    [-] ListNet-specific parameters
        [ -epoch <T> ]          The number of epochs to train (default=1500)
        [ -lr <rate> ]          Learning rate (default=0.00001)

    [-] Random Forests-specific parameters # 隨機森林特定參數
        [ -bag <r> ]            Number of bags (default=300)
        # 子集採樣率
        [ -srate <r> ]          Sub-sampling rate (default=1.0)
        # 特征採樣率
        [ -frate <r> ]          Feature sampling rate (default=0.3)
        [ -rtype <type> ]       Ranker to bag (default=0, i.e. MART)
        # 樹個數
        [ -tree <t> ]           Number of trees in each bag (default=1)
        # 每棵樹的葉節點個數
        [ -leaf <l> ]           Number of leaves for each tree (default=100)
        # 學習率
        [ -shrinkage <factor> ] Shrinkage, or learning rate (default=0.1)
        # 樹分割時使用的候選特征閾值個數
        [ -tc <k> ]             Number of threshold candidates for tree spliting. -1 to use all feature values (default=256)
        [ -mls <n> ]            Min leaf support -- minimum #samples each leaf has to contain (default=1)

    [-] Linear Regression-specific parameters
        [ -L2 <reg> ]           L2 regularization parameter (default=1.0E-10)

  [+] Testing previously saved models # 測試已保存的模型
        # 載入模型
        -load <model>           The model to load
                                Multiple -load can be used to specify models from multiple folds (in increasing order),
                                  in which case the test/rank data will be partitioned accordingly.
        # 測試數據
        -test <file>            Test data to evaluate the model(s) (specify either this or -rank but not both)
        # 對指定文件中的樣本排序,與 -test 不能同時使用
        -rank <file>            Rank the samples in the specified file (specify either this or -test but not both)
        [ -metric2T <metric> ]  Metric to evaluate on the test data (default=ERR@10)
        [ -gmax <label> ]       Highest judged relevance label. It affects the calculation of ERR (default=4, i.e. 5-point scale {0,1,2,3,4})
        [ -score <file>]        Store ranker's score for each object being ranked (has to be used with -rank)
        # 列印單個排名列表上的性能(必須與 -test 一起使用)
        [ -idv <file> ]         Save model performance (in test metric) on individual ranked lists (has to be used with -test)
        # 特征歸一化
        [ -norm ]               Normalize feature vectors (similar to -norm for training/tuning)

1. -train <file>

指定訓練數據的文件,訓練數據格式:

label    qid:$id    $featureid:$featurevalue    $featureid:$featurevalue ... # description

每行代表一個樣本,相同查詢請求的樣本的 qid 相同,label 表示該樣本和該查詢請求的相關程度,description 描述信息,不參與訓練計算。

2、-ranker <type>

指定排名演算法

  • MART(Multiple Additive Regression Tree)多重增量回歸樹
  • GBDT(Gradient Boosting Decision Tree)梯度漸進決策樹
  • GBRT(Gradient Boosting Regression Tree)梯度漸進回歸樹
  • TreeNet 決策樹網路
  • RankNet
  • RankBoost
  • AdaRank
  • Coordinate Ascent
  • LambdaMART
  • ListNet
  • Random Forests
  • Linear regression

3、-feature <file>

指定樣本的特征定義文件,格式如下:

feature1
feature2
...
# featureK(該特征不參與分析)

4、-metric2t <metric>

指定信息檢索中的評價指標,包括:
MAP, NDCG@k, DCG@k, P@k, RR@k, ERR@k

5、Example

java -jar bin/RankLib.jar -train MQ2008/Fold1/train.txt -test MQ2008/Fold1/test.txt -validate MQ2008/Fold1/vali.txt -ranker 6 -metric2t NDCG@10 -metric2T ERR@10 -save mymodel.txt

命令解釋 >>>
訓練數據:MQ2008/Fold1/train.txt
測試數據:MQ2008/Fold1/test.txt
驗證數據:MQ2008/Fold1/vali.txt
排名演算法:6,LambdaMART
評估指標:NDCG,取排名前 10 個數據進行計算
測試數據評估指標:ERR,取排名前 10 個數據進行計算
保存模型:mymodel.txt

  • 參數 -validate 是可選的,但可以更好的模型結果,對於 RankNet/MART/LambdaMART 非常重要。
  • -metric2t 僅應用於 list-wise 演算法(AdaRank、Coordinate Ascent 和 LambdaMART);point-wise 和 Pair-wise 演算法(MART、RankNet、RankBoost)是使用自己內部的 RMSE/pair-wise loss 作為評價指標。ListNet 雖然是 list-wise 演算法,但是也不用 metric2t 指定評價指標。

6、k-fold cross validation

  • 順序分區
java -jar bin/RankLib.jar -train MQ2008/Fold1/train.txt -ranker 4 -kcv 5 -kcvmd models/ -kcvmn ca -metric2t NDCG@10 -metric2T ERR@10

按順序將訓練數據拆分5等份,第 i 份數據作為第 i 摺疊的測試數據,第 i 摺疊的訓練數據則是由其他摺疊的數據組成。

  • 隨機分區
java -cp bin/RankLib.jar ciir.umass.edu.features.FeatureManager -input MQ2008/Fold1/train.txt -output mydata/ -shuffle

將訓練數據 train.txt 重新洗牌存儲在 mydata/ 目錄下 train.txt.shuffled

  • 獲取每個摺疊中的數據
java -cp bin/RankLib.jar ciir.umass.edu.features.FeatureManager -input MQ2008/Fold1/train.txt.shuffled -output mydata/ -k 5

7、評估已訓練的模型

java -jar bin/RankLib.jar -load mymodel.txt -test MQ2008/Fold1/test.txt -metric2T ERR@10

8、模型對比

java -jar bin/RankLib.jar -test MQ2008/Fold1/test.txt -metric2T NDCG@10 -idv output/baseline.ndcg.txt
java -jar bin/RankLib.jar -load ca.model.txt -test MQ2008/Fold1/test.txt -metric2T NDCG@10 -idv output/ca.ndcg.txt
java -jar bin/RankLib.jar -load lm.model.txt -test MQ2008/Fold1/test.txt -metric2T NDCG@10 -idv output/lm.ndcg.txt

輸出文件中包含了每條查詢的 NDCG@10 指標值,以及所有查詢的綜合指標,例如:

NDCG@10   170   0.0
NDCG@10   176   0.6722390270733757
NDCG@10   177   0.4772656487866462
NDCG@10   178   0.539003131276382
NDCG@10   185   0.6131471927654585
NDCG@10   189   1.0
NDCG@10   191   0.6309297535714574
NDCG@10   192   1.0
NDCG@10   194   0.2532778777010656
NDCG@10   197   1.0
NDCG@10   200   0.6131471927654585
NDCG@10   204   0.4772656487866462
NDCG@10   207   0.0
NDCG@10   209   0.123151194370365
NDCG@10   221   0.39038004999210174
NDCG@10   all   0.5193204478059303

然後再進行對比:

java -cp RankLib.jar ciir.umass.edu.eval.Analyzer -all output/ -base baseline.ndcg.txt > analysis.txt

對比結果 analysis.txt 如下:

Overall comparison
  ------------------------------------------------------------------------
  System  Performance     Improvement     Win     Loss    p-value
  baseline_ndcg.txt [baseline]    0.093
  LM_ndcg.txt     0.2863  +0.1933 (+207.8%)       9       1       0.03
  CA_ndcg.txt     0.5193  +0.4263 (+458.26%)      12      0       0.0

  Detailed break down
  ------------------------------------------------------------------------
             [ < -100%)  [-100%,-75%)  [-75%,-50%)  [-50%,-25%)  [-25%,0%)  (0%,+25%]  (+25%,+50%]  (+50%,+75%]  (+75%,+100%]  ( > +100%]
  LM_ndcg.txt    0        0           1            0            0         4            2            2            1            0
  CA_ndcg.txt    0             0            0            0            0        1            6            2            3            0

9、利用訓練模型重排名

java -jar RankLib.jar -load mymodel.txt -rank myResultLists.txt -score myScoreFile.txt

myScoreFile.txt 文件中只是增加了一列,表示重新計算的排名評分,需要自己另外根據該評分排序獲取新的排名順序。

1   0   -7.528650760650635
1   1   2.9022061824798584
1   2   -0.700125515460968
1   3   2.376657485961914
1   4   -0.29666265845298767
1   5   -2.038628101348877
1   6   -5.267711162567139
1   7   -2.022146463394165
1   8   0.6741248369216919
...

參考

RankLib wiki


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • nc 命令介紹: Ncat is a feature-packed networking utility which reads and writes data across networks from the command line. Ncat was written for the Nmap ...
  • 正則表達式含義與構成:用單個字元串來描述或者匹配一系列符合某個句法規則的字元串,一般是由普通字元與特殊字元組成 awk 選項 '模式或者命令{編輯指令}' 文件名 awk '//{print}' awk -f 腳本文件 文件名 元字元 : \ 轉義 ^ 以....開頭 $ 以....結尾 . 任意符 ...
  • 轉載來源:https://blog.csdn.net/qq_22260641/article/details/76182447 一、安裝Oracle前準備 1.創建運行oracle資料庫的系統用戶和用戶組 <code class="language-plain">[humf@localhost ~] ...
  • 一句話很簡單,一個shell腳本搞定,不跟你嘻嘻哈哈 #!/bin/shexe="ThorIceLocker"#存放你的可執行文件的名字des="/home/ninetripod/Desktop/thor/debug/"#可執行文件的路徑deplist=$(ldd $exe | awk '{if(m ...
  • 如果我們使用lua 運行腳本程式,或者使用python執行程式。我們不用明確 python的位置,可以使用env 命令來運行: env 程式介紹: 使用方式如下所示: 轉化為: 保持更新,轉載請註明出處。 ...
  • 刪除某一行 delete from name1 where agee=10 limit 1; insert into name1(agee,namee)values(10,'wwww'),(10,'wwww1'),(10,'wwww2'),(10,'www3w'); 插入多行數據; update n ...
  • Preface I've stuck twice in my previous experiments in backing up dropped tables.I am still not sure that why I got failure yesterday.Therefore,I deci ...
  • 1. 恢復和複製的需要,對innodb鎖機制的影響 mysql 通過binlog文件對增刪除改等更新數據的sql語句,實現資料庫的恢復和主從複製。mysql的恢復機制(複製其實就是在slave mysql不斷做基於binglog的恢復)特點有如下: (1) mysql 的恢復是sql語句級的,也就是 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...