QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
line_search.h
Go to the documentation of this file.
1 /*
2  * QuickRank - A C++ suite of Learning to Rank algorithms
3  * Webpage: http://quickrank.isti.cnr.it/
4  * Contact: quickrank@isti.cnr.it
5  *
6  * Unless explicitly acquired and licensed from Licensor under another
7  * license, the contents of this file are subject to the Reciprocal Public
8  * License ("RPL") Version 1.5, or subsequent versions as allowed by the RPL,
9  * and You may not copy or use this file in either source code or executable
10  * form, except in compliance with the terms and conditions of the RPL.
11  *
12  * All software distributed under the RPL is provided strictly on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND
14  * LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
15  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16  * PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific
17  * language governing rights and limitations under the RPL.
18  *
19  * Contributors:
20  * - Andrea Battistini (andreabattistini@hotmail.com)
21  * - Chiara Pierucci (chiarapierucci14@gmail.com)
22  * - Claudio Lucchese (claudio.lucchese@isti.cnr.it)
23  */
24 #pragma once
25 
26 #include <memory>
27 
28 #include "data/dataset.h"
29 #include "metric/ir/metric.h"
30 #include "learning/ltr_algorithm.h"
31 
32 namespace quickrank {
33 namespace learning {
34 namespace linear {
35 
36 /// This implements the Line Search algorithm.
37 class LineSearch: public LTR_Algorithm {
38 
39  public:
40 
41  LineSearch(unsigned int num_points, double window_size,
42  double reduction_factor, unsigned int max_iterations,
43  unsigned int max_failed_vali, bool adaptive,
44  unsigned int last_only = 0);
45 
46  LineSearch(const pugi::xml_document &model);
47 
48  virtual ~LineSearch();
49 
50  /// Returns the name of the ranker.
51  virtual std::string name() const {
52  return NAME_;
53  }
54 
55  unsigned int get_last_only() const {
56  return train_only_last_;
57  }
58 
59  void set_last_only(unsigned int last_only) {
60  train_only_last_ = last_only;
61  }
62 
63  void reset_weights() {
64  best_weights_.clear();
65  }
66 
67  static const std::string NAME_;
68 
69  /// Executes the learning process.
70  ///
71  /// \param training_dataset The training dataset.
72  /// \param validation_dataset The validation training dataset.
73  /// \param metric The metric to be optimized.
74  /// \param partial_save Allows to save a partial model every given number of iterations.
75  /// \param model_filename The file where the model, and the partial models, are saved.
76  virtual void learn(std::shared_ptr<data::Dataset> training_dataset,
77  std::shared_ptr<data::Dataset> validation_dataset,
78  std::shared_ptr<metric::ir::Metric> metric,
79  size_t partial_save,
80  const std::string model_filename);
81 
82  /// Returns the score of a given document.
83  virtual Score score_document(const Feature *d) const;
84 
85  /// Returns the learned weights
86  virtual std::shared_ptr<std::vector<double>> get_weights() const {
87 
88  return std::shared_ptr<std::vector<double>>(
89  new std::vector<double>(best_weights_));
90  }
91 
92  virtual bool update_weights(std::shared_ptr<std::vector<double>> weights);
93 
94  /// Return the xml model representing the current object
95  virtual pugi::xml_document *get_xml_model() const;
96 
97  private:
98  unsigned int num_points_;
99  double window_size_;
101  unsigned int max_iterations_;
102  unsigned int max_failed_vali_;
103  bool adaptive_;
104  unsigned int train_only_last_;
105 
106  std::vector<double> best_weights_;
107 
108  /// The output stream operator.
109  friend std::ostream &operator<<(std::ostream &os, const LineSearch &a) {
110  return a.put(os);
111  }
112 
113  /// Prints the description of Algorithm, including its parameters
114  virtual std::ostream &put(std::ostream &os) const;
115 
116  virtual void preCompute(Feature *training_dataset,
117  unsigned int num_samples,
118  unsigned int num_features,
119  Score *pre_sum,
120  double *weights,
121  Score *training_score,
122  unsigned int feature_exclude);
123 
124  virtual void score(Feature *dataset, unsigned int num_samples,
125  unsigned int num_features, double *weights, Score *scores);
126 };
127 
128 } // namespace linear
129 } // namespace learning
130 } // namespace quickrank
virtual pugi::xml_document * get_xml_model() const
Return the xml model representing the current object.
Definition: line_search.cc:389
static const std::string NAME_
Definition: line_search.h:67
unsigned int train_only_last_
Definition: line_search.h:104
Definition: dataset.cc:28
unsigned int max_iterations_
Definition: line_search.h:101
bool adaptive_
Definition: line_search.h:103
double window_size_
Definition: line_search.h:99
std::vector< double > best_weights_
Definition: line_search.h:106
unsigned int get_last_only() const
Definition: line_search.h:55
Definition: ltr_algorithm.h:33
virtual std::string name() const
Returns the name of the ranker.
Definition: line_search.h:51
unsigned int num_points_
Definition: line_search.h:98
float Feature
data type for instance predicted label
Definition: types.h:31
void reset_weights()
Definition: line_search.h:63
virtual void score(Feature *dataset, unsigned int num_samples, unsigned int num_features, double *weights, Score *scores)
Definition: line_search.cc:443
This implements the Line Search algorithm.
Definition: line_search.h:37
virtual ~LineSearch()
Definition: line_search.cc:101
virtual bool update_weights(std::shared_ptr< std::vector< double >> weights)
Update the weights for the ensemble models (only).
Definition: line_search.cc:374
double Score
data type for instance truth label
Definition: types.h:30
virtual void learn(std::shared_ptr< data::Dataset > training_dataset, std::shared_ptr< data::Dataset > validation_dataset, std::shared_ptr< metric::ir::Metric > metric, size_t partial_save, const std::string model_filename)
Executes the learning process.
Definition: line_search.cc:117
virtual std::shared_ptr< std::vector< double > > get_weights() const
Returns the learned weights.
Definition: line_search.h:86
unsigned int max_failed_vali_
Definition: line_search.h:102
virtual Score score_document(const Feature *d) const
Returns the score of a given document.
Definition: line_search.cc:366
double reduction_factor_
Definition: line_search.h:100
virtual std::ostream & put(std::ostream &os) const
Prints the description of Algorithm, including its parameters.
Definition: line_search.cc:104
friend std::ostream & operator<<(std::ostream &os, const LineSearch &a)
The output stream operator.
Definition: line_search.h:109
virtual void preCompute(Feature *training_dataset, unsigned int num_samples, unsigned int num_features, Score *pre_sum, double *weights, Score *training_score, unsigned int feature_exclude)
Definition: line_search.cc:424
LineSearch(unsigned int num_points, double window_size, double reduction_factor, unsigned int max_iterations, unsigned int max_failed_vali, bool adaptive, unsigned int last_only=0)
Definition: line_search.cc:37
void set_last_only(unsigned int last_only)
Definition: line_search.h:59