"Learning sort" learning to Rank listwise about listnet algorithm and its realization

Source: Internet
Author: User

In the previous article "Learning to rank in pointwise about prank algorithm source code realization " tells the realization of the point-based learning sorting prank algorithm. This article mainly describes listwise approach and neural network based listnet algorithm and Java implementation. Include:
1. Column-Based learning sequencing (listwise) Introduction
Introduction to 2.ListNet Algorithms
3.ListNet algorithm Java implementation
The single document method in the LTR is to use each document in the training set as a training instance, and the document alignment method is to use any two document pairs as a training instance in the search results of the same query, and the document column method is to use all the search results lists in a query as a training instance.

I. Column-based learning sequencing (listwise) Introduction

The Listwise method takes all the search results scored as an instance of a query and trains an optimal scoring function. In the following datasets: (see the previous article for data set introduction)

===============================================================================0 qid:10 1:0.000000 2:0.000000 3:0.000000 ... 45:0.000000 46:0.000000 #docid =1 qid:10 1:0.031310 2:0.666667 3:0.500000 ... 45:0.448276 46:0.000000 #docid =1 qid:10 1:0.078682 2:0.166667 3:0.500000 ... 45:1.000000 46:0.000000 #docid = 0 qid:50 1:0.044248 2:0.400000 3:0.333333 ... 45:0.622951 46:0.000000 #docid = 2 qid:50 1:0.764381 2:0.200000 3:0.000000 ... 45:0.252874 46:0.000000 #docid =1 qid:50 1:0.693584 2:0.000000 3:0.000000 ... 45:0.275862 46:0.000000 #docid ================================================================================column-Based learning ordering (listwise approach) is to train all the query documents that correspond to qid=10 as an instance, that is, a query and all of its corresponding search result scores are trained as an instance; After training to get a final scoring function f, Test set a new query, function f for each document is scored, and then according to the order of scoring from high to low sort is the result of the corresponding search.
The following is a description of the probability distribution based on the combination of search results sorting to train. For example:
Refer to "This is the search engine: core technical details by: Zhang Junlin" the 5th Chapter

user input Query Q1, assuming that the returned search result collection contains a, B and C three documents, search engine to sort the results, and 3 document order a total of 6 combinations: ABC, ACB, BAC, BCA, cab and CBA, Each permutation combination is a possible sorting method for search results.
We can think of the function g as the optimal scoring function (manual scoring), for query Q1: document A 6 points, document B 4 points, document C 3 points; Our task is to find a function that makes the scoring order of Q1 's search results as close to the standard function g as possible. Where functions F and H are the actual scoring functions, By comparing the KL distances between two probabilities, it is found that F is closer to the hypothetical optimal function g. So select function f as the scoring function for the search.
listwise main algorithms include: Adarank, Svm-map, Listnet, Lambdamart and so on.

two. Listnet Algorithm Introduction

Pointwise learning sort is to treat each document in the training set as a sample get Rank function, the main solution is to convert the classification problem into a single document classification and regression problems, such as prank.
Pairwise Learning Sort (Next article) is the same query in the different related annotations of two documents as a sample, the main solution is to convert rank to two value classification problems, such as ranknet.
Listwise learning Sorting is the whole document sequence as a sample, mainly through the direct optimization of the Information retrieval evaluation method and the definition of loss function two methods to achieve. The listnet algorithm introduces the Luce model to the sequencing learning method to represent the document sequence, while most of the neural network-based sequencing learning algorithms are based on the Luce model (Luce model is to represent any sort of sequence as a probability value ) To indicate how the sequence is sorted.
Listnet Algorithm Reference:
"Learning to Rank:from pairwise approach to listwiseapproach"
Research on listwise sequencing learning method based on neural network by : Hayashibara


The steps explained by this algorithm are as follows:
1. First enter the training set train.txt data. {x, y} represents the sample document corresponding to the query number, including the label level Label=y (46-D Microsoft Datasets Total 3 levels: 0-unrelated, 1-partially related, 2-all related), x represents the corresponding feature and eigenvalues, and it is important to note that X (m) represents M-qid, each x (m) There are several sample documents in the.
2. Initialize the operation. The number of iterations T (set to 30) and learning rate (Ita can be 0.003, 0.001, 0.03, 0.01, and so on), while initializing the weight w.
3. Two-layer loop operation. The first layer is the number of loop iterations: for t = 1 to T, and the second loop is the total number of iterations (qid total): For i = 1 to M do.
4. Calculate the line fraction with the current weight w. Note that weight w[46] is a one-dimensional array that corresponds to 46 eigenvalues, and f (w) = w * x.

5. Calculate Gradient vector Delta_w (46 dimensions). Where the formula is calculated as follows:

where n (i) represents the total number of documents corresponding to the query number Qid=i, and J represents the current document for Qid=i. The upper-right subscript of x indicates the corresponding qid number, and the lower-right subscript indicates the corresponding document designator. and P is the function that calculates the probability, as follows:

it represents the probability value of the first S1 row, the second S2 row and the third of the S3 row. This is the use of the Luce model to make the ordering of a sequence a single probability value. In practice, we represent FAI by using the exp () function. The main guarantee is that the value is positive and increment.
However, the time complexity of n! is obviously inefficient, so the top-k probability is proposed to be solved by using the permutation probability of the former K term to approximate the probability of the whole sequence, in exchange for running time by reducing the precision.
The top-k probability formula is as follows:

in the following Java code implementation, I'm using Top-1, which is the probability value for the first row of the current line.
6. Cycle Update weight W.
7. Final output W[46] weights, the training process is finished. The model can be used to predict the sequencing of the test, test.txt by the weight of the w*x score, and then from high to low order.

PS: This is only my personal understanding after combining the two papers, if there are errors or shortcomings, welcome to explore! At the same time thanks to my classmates XP and MT, we explored and shared only to understand some listnet algorithm and code.

three. Listnet Algorithm Java Implementation

(PS: This part of the code is very grateful to my team Leader XP and MT, they on the whole programming on my help is a lifetime. At the same time, I also hope to find more teachers and friends in the future to guide me forward ~)
The code has a detailed comment, which is done in each step. The diagram on the left is the main function, which mainly includes: reading the file and parsing the data, writing the data, learning the sorting model and scoring prediction, right is the core algorithm of learning sorting.

  
The code is as follows:
Package Listnet_xiuzhang;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import                   Java.io.filewriter;import Java.io.inputstreamreader;public class Listnet {//Total number of files (number of marks) private static int sumlabel;                Eigenvalues of 46 (designator 1-46) private static double feature[][] = new double[100000][48]; Eigenvalue weight 46 (label 1-46) private static double weight [] = new double[48];//correlation its value has 0-2 three levels starting from 1 record private static int label [] = New int[1000000];//Query ID starting from 1 record private static int qid [] = new int[1000000];//doc number per qid private static int doc_ofqid[] = n EW int[100000];     private static int iter_num=30;        Number of iterations private static int weidu=46;       Characteristic number private static int qid_num=0;      QID number private static int tempqid=-1;       Temporary qid number private static int tempdoc=0;        Temp doc Number/** * Function function Read file * parameter String filePath file path */public static void Readtxtfile (String filePath) {try {        String encoding= "GBK";        File File=new file (FilePath); if (fIle.isfile () && file.exists ()) {//Determine if the file exists inputstreamreader read = new InputStreamReader (New Fileinputstr                 EAM (file), encoding);                BufferedReader BufferedReader = new BufferedReader (read);                String linetxt = null; Sumlabel = 1;                Initialize from 1 records//Read data by rows and decompose data while ((Linetxt = Bufferedreader.readline ()) = null) {                String str = NULL;                int lengthline = Linetxt.length ();                Gets the data string space-delimited string arrays[] = Linetxt.split (""); for (int i=0; i<arrays.length; i++) {//Gets the label value of each line swatch if (i==0) {LABEL[SUML                Abel] = Integer.parseint (arrays[0]);                } else if (i>=weidu+2) {//read to # jump out 0-label 1-qid 2:47-Feature continue; } else {String subarrays[] = Arrays[i].split (":");              Features: eigenvalues if (i==1) {//Get QID  Determine if the new qid if (tempqid! = Integer.parseint (subarrays[1])) {if (tempqid! =-1) {///does not appear for the first time                    New QID//assigns a Tempdoc document Doc_ofqid[qid_num]=tempdoc corresponding to Qid_num;                tempdoc=0;                }//When the tempqid is not equal to the current QID 1//equal then jumps directly to Doc plus 1 until unequal qid_num++;                    Tempqid=integer.parseint (Subarrays[1]); } tempdoc++;                New Document Qid[sumlabel] = Integer.parseint (subarrays[1]); } else {//Get 46-D eigenvalues int number = Integer.parseint (subarrays[0]);//Judgment feature Doub                Le value = double.parsedouble (Subarrays[1]); Feature[sumlabel][number] = value;                Number array label: 1-46}}} sumlabel++;                } Doc_ofqid[qid_num]=tempdoc;        Read.close (); } else {System.out.println ("Cannot find the specifiedFile \ n ");            }} catch (Exception e) {System.out.println ("Error reading file contents");        E.printstacktrace ();  }}/** * Learn sort * Training model get 46 dimensional weight */public static void Learningtorank () {//variable double index [] = new double[1000000];d ouble Tao [] = new double[1000000];d ouble yita=0.00003;//initialize for (int i=0;i<weidu+2;i++) {//from 1 to 136 for weights, 0 and 137 useless weight[i] = ( Double) 1.0; Weighted initial value}system.out.println ("Training ...");//Compute Weights Learning Algorithm for (int iter = 0; iter<iter_num; iter++)//Iteration iter_num times {System.out.println ("---iterations:" +iter); int now_doc=0; Global Document index for (int i=1; i<=qid_num; i++)//Total sample qid equivalent to two-layer loop T and M {double delta_w[] = new double[weidu+2];//46 gradient-composed vector int do C_of_i=doc_ofqid[i]; The QID document//score F (w), a QID has multiple documents, a document is a single point, so an I corresponds to a fractional array of double fw[] = new double[doc_of_i+2];/* The first step of the fractional Group FW fin */for (int k =1;k<=doc_of_i;k++) {//initialize fw[k]=0.0;} for (int k=1;k<=doc_of_i;k++) {//score per document for (int p=1;p<=weidu;p++) {fw[k]=fw[k]+weight[p]*feature[now_doc+k][p ]; Figure out the score for this document}}/* * Second step calculation gradient delta_w vector * A=σp*x,a is vector * B=ΣEXPF (x), B is number * C=EXPF (x) *x,c is vector * Final result delta_w is vector */double[] a=new double[weidu+2],c=new double[weidu+2 ];for (int k=0;k<weidu+2;k++) {a[k]=0.0;}//Initialize for (int k=0;k<weidu+2;k++) {c[k]=0.0;}//Initialize double b=0.0;// Count A:----for (int k=1; k<=doc_of_i; k++) {double p=1.0;//First topkdouble[] temp=new double[48];for (int q=1;q<=weidu;q+ +) {//Count P:----The probability of the Q-vector row xx is how much//denominator: double fenmu=0.0;for (int m=1;m<=doc_of_i;m++) {fenmu=fenmu+math.exp (fw[m]); All document Scores}//top-1 exp (S1)/exp (s1) +exp (S2) +: +EXP (SN) for (int m=1;m<=doc_of_i;m++) {p=p* (Math.exp (fw[m])/fenmu);} Calculation temp[q]=temp[q]+p*feature[now_doc+k][q];} for (int q=1; q<=weidu; q++) {a[q]=a[q]+temp[q];}} End a//B:----fin.for (int k=1; k<=doc_of_i; k++) {b=b+math.exp (fw[k]);} Count C:----for (int k=1; k<=doc_of_i; k++) {double[] temp=new double[weidu+2];for (int q=1; q<=weidu; q++) {temp[q]= Temp[q]+math.exp (Fw[k]) *feature[now_doc+k][q];} for (int q=1; q<=weidu; q++) {c[q]=c[q]+temp[q];}} Calculate gradient: delta_x=-a+1/b*cfor (int q=1;q<=weidu; q++) {delta_w[q]= ( -1) *a[q] + ((1.0/b) *c[q]);} /* The third step updates the weight fin. */for (int k=1; k<=weidu; k++) {weight[k]=weight[k]-yita*delta_w[k];} Now_doc=now_doc+doc_of_i; Update current document Index}}//end iterations//output weights for (int i=1;i<=weidu;i++)//from 1 to 136 for weights, 0 and 137 useless {System.out.println (i+ "Wei:" +weight[i ]);}} /** * Output weights to file Filemodel * @param filemodel */public static void Writefilemodel (String filemodel) {//Output weights to file try {System.out. println ("write start. Total number of rows:" +sumlabel); FileWriter FileWriter = new FileWriter (Filemodel);//Write Data Filewriter.write ("# # listnet"); Filewriter.write ("\ r \ n"); Filewriter.write ("# # epochs =" +iter_num); Filewriter.write ("\ r \ n"); Filewriter.write ("# # No.  of features = "); Filewriter.write (" \ r \ n "); Filewriter.write (" 1 2 3 4 5 6 7 8 9 10 ... Filewriter.write ("\ r \ n"), Filewriter.write ("0"), Filewriter.write ("\ r \ n"); for (int k=0; k< Weidu; k++) {Filewriter.write ("0" +k+ "" +weight[k+1] "); Filewriter.write (" \ r \ n ");} Filewriter.close (); System.out.println ("Write Fin."); catch (Exception e) {System.out.println ("error in writing file contents"); E.printstacktrace ();}} /** * Forecast Sort * Regular response to Test.txt file scoring sort * But we're doing that in Hadoop. Sort step This function only tests Train.txt score */public static void Predictrank (String Filesco RE) {//Output score try {System.out.println ("write start. Total number of rows:" +sumlabel); String encoding = "GBK"; FileWriter FileWriter = new FileWriter (filescore);//write data for (int k=1; k<sumlabel; k++) {double score=0.0;for (int j=1;j& lt;=weidu;j++) {score=score+weight[j]*feature[k][j];} Filewriter.write ("qid:" +qid[k]+ "score:" +score+ "Label:" +label[k] "); Filewriter.write (" \ r \ n ");} Filewriter.close (); System.out.println ("Write Fin.");            catch (Exception e) {System.out.println ("error in writing file contents"); E.printstacktrace ();}}       /** * Main function */public static void main (string args[]) {string fileInput = "Fold1\\train.txt";       Training string Filemodel = "Model_weight.txt";      Output weight model string filescore = "Score_listnet.txt"; Output score//1th step read the file and parse the data System.out.println ("read ..."); Readtxtfile (FileInput); System.out.println ("Read and WRite well. ");  /2nd Step Sort calculation learningtorank ();//3rd Step Output Model Writefilemodel (Filemodel);//4th Step score Forecast Sequencing Predictrank (filescore); }/* * End */}

Four. Summary

The above code I would like you to focus on is listnet in the training model process of the code, that is, through the train.txt to get the 46-dimensional weight of the model. With this model you can rate the test.txt (weight * eigenvalues), The code above is just a simple scoring operation for Train.txt, because our job is based on the distributed processing of Hadoop or spark. So this part is done by other students.
at the same time you can also through the open source Ranklib or Loeffler classmate's listnet algorithm to study, the address is as follows:
http://sourceforge.net/projects/minorthird/
http://code.google.com/p/learning-to-rank-listnet/
http://people.cs.umass.edu/~vdang/ranklib.html
Finally, we used an open source map and [email protected] to simply perform a performance evaluation of the algorithm, along with the operation on Hadoop (MapReduce only found one of the prank).



Hope that the article is helpful to everyone, at the same time I was written in accordance with the Java code, if there are errors or shortcomings, but also please Haihan ~ at the same time welcome to ask questions, I understand the machine learning and algorithm is still beginner, but will try to reply. At the same time found that part of the code is really very few, so only to write It is also ready to write pairwise and MAP\NDCG reviews.
(By:eastmount 2015-2-5 night 10 o ' http://blog.csdn.net/eastmount/article/)


"Learning sort" learning to Rank listwise about listnet algorithm and its realization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.