MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/svm_rank_ex.cpp:32–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30
31
32int main()
33{
34 try
35 {
36 // Make a typedef for the kind of object we will be ranking. In this
37 // example, we are ranking 2-dimensional vectors.
38 typedef matrix<double,2,1> sample_type;
39
40
41 // Now let's make some testing data. To make it really simple, let's
42 // suppose that vectors with positive values in the first dimension
43 // should rank higher than other vectors. So what we do is make
44 // examples of relevant (i.e. high ranking) and non-relevant (i.e. low
45 // ranking) vectors and store them into a ranking_pair object like so:
46 ranking_pair<sample_type> data;
47 sample_type samp;
48
49 // Make one relevant example.
50 samp = 1, 0;
51 data.relevant.push_back(samp);
52
53 // Now make a non-relevant example.
54 samp = 0, 1;
55 data.nonrelevant.push_back(samp);
56
57
58 // Now that we have some data, we can use a machine learning method to
59 // learn a function that will give high scores to the relevant vectors
60 // and low scores to the non-relevant vectors.
61
62 // The first thing we do is select the kernel we want to use. For the
63 // svm_rank_trainer there are only two options. The linear_kernel and
64 // sparse_linear_kernel. The later is used if you want to use sparse
65 // vectors to represent your objects. Since we are using dense vectors
66 // (i.e. dlib::matrix objects to represent the vectors) we use the
67 // linear_kernel.
68 typedef linear_kernel<sample_type> kernel_type;
69
70 // Now make a trainer and tell it to learn a ranking function based on
71 // our data.
72 svm_rank_trainer<kernel_type> trainer;
73 decision_function<kernel_type> rank = trainer.train(data);
74
75 // Now if you call rank on a vector it will output a ranking score. In
76 // particular, the ranking score for relevant vectors should be larger
77 // than the score for non-relevant vectors.
78 cout << "ranking score for a relevant vector: " << rank(data.relevant[0]) << endl;
79 cout << "ranking score for a non-relevant vector: " << rank(data.nonrelevant[0]) << endl;
80 // These output the following:
81 /*
82 ranking score for a relevant vector: 0.5
83 ranking score for a non-relevant vector: -0.5
84 */
85
86
87 // If we want an overall measure of ranking accuracy we can compute the
88 // ordering accuracy and mean average precision values by calling
89 // test_ranking_function(). In this case, the ordering accuracy tells

Callers

nothing calls this directly

Calls 5

test_ranking_functionFunction · 0.85
push_backMethod · 0.45
trainMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected