Turi Create  4.0
linear_regression_opt_interface.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_REGR_LINEAR_REGRESSION_OPT_INTERFACE_H_
7 #define TURI_REGR_LINEAR_REGRESSION_OPT_INTERFACE_H_
8 
9 // ML-Data Utils
10 #include <ml/ml_data/ml_data.hpp>
11 
12 
13 // Toolkits
14 #include <toolkits/supervised_learning/supervised_learning.hpp>
15 #include <toolkits/supervised_learning/standardization-inl.hpp>
16 #include <toolkits/supervised_learning/linear_regression.hpp>
17 
18 // Optimization Interface
19 #include <ml/optimization/optimization_interface.hpp>
20 
21 
22 // TODO: List of todo's for this file
23 //------------------------------------------------------------------------------
24 
25 namespace turi {
26 namespace supervised {
27 
28 /*
29  * Linear Regression Solver
30  * ****************************************************************************
31  */
32 
33 /**
34  * Solver interface for the linear regression problem.
35  *
36  */
39 {
40  protected:
41 
42  ml_data data;
43  ml_data valid_data;
44  linear_regression smodel;
45 
46  size_t examples = 0; /**< Number of examples */
47  size_t features = 0; /**< Number of features */
48  size_t variables = 0; /**< Number of variables */
49  size_t n_threads; /** < Num threads */
50 
51  std::shared_ptr<l2_rescaling> scaler; /** <Scale features */
52  bool feature_rescaling = false; /** Feature rescaling */
53  bool is_dense = false; /** Is the data sparse */
54 
55  public:
56 
57 
58  /**
59  * Default constructor.
60  *
61  * \param[in] _ml_data ML Data containing everything!
62  * \param[in] feature_rescaling Feature Rescaling
63  *
64  * \note Default options are to be used when the interface is called from the
65  * linear_regression class.
66  */
68  const ml_data& _valid_data,
69  linear_regression& _model,
70  bool _feature_rescaling=true);
71 
72  /**
73  * Default destructor.
74  */
76 
77  /**
78  * Set the number of threads.
79  *
80  * \param[in] _n_threads Set the number of threads.
81  */
82  void set_threads(size_t _n_threads);
83 
84  /**
85  * Set feature scaling
86  */
88 
89  /**
90  * Transform the final solution back to the original scale.
91  *
92  * \param[in,out] coefs Solution vector
93  */
94  void rescale_solution(DenseVector& coefs);
95 
96  /**
97  * Get the number of examples in the model
98  *
99  * \returns Number of examples
100  */
101  size_t num_examples() const;
102 
103  /**
104  * Get the number of variables in the model
105  *
106  * \returns Number of variables
107  */
108  size_t num_variables() const;
109 
110  /**
111  * Get the number of validation-set examples in the model
112  *
113  * \returns Number of examples
114  */
115  size_t num_validation_examples() const;
116 
117  /**
118  * Get strings needed to print the header for the progress table.
119  *
120  * \param[in] a vector of strings to print at the beginning of the header.
121  */
122  std::vector<std::pair<std::string, size_t>>
123  get_status_header(const std::vector<std::string>& stat_names);
124 
125  /**
126  * Get strings needed to print a row of the progress table.
127  *
128  * \param[in] a vector of model coefficients.
129  * \param[in] a vector of stats to print at the beginning of each row
130  */
131  std::vector<std::string> get_status(const DenseVector& coefs,
132  const std::vector<std::string>& stats);
133 
134  /**
135  * Compute first order statistics at the given point. (Gradient & Function value)
136  *
137  * \param[in] point Point at which we are computing the stats.
138  * \param[out] gradient Dense gradient
139  * \param[out] function_value Function value
140  * \param[in] mbStart Minibatch start index
141  * \param[in] mbSize Minibatch size (-1 implies all)
142  *
143  */
144  void compute_first_order_statistics(const DenseVector &point, DenseVector&
145  gradient, double & function_value, const size_t mbStart = 0, const size_t
146  mbSize = -1);
147 
148  /**
149  * Compute second order statistics at the given point. (Gradient & Function value)
150  *
151  * \param[in] point Point at which we are computing the stats.
152  * \param[out] hessian Hessian (Dense)
153  * \param[out] gradient Dense gradient
154  * \param[out] function_value Function value
155  *
156  */
157  void compute_second_order_statistics(const DenseVector &point, DenseMatrix&
158  hessian, DenseVector& gradient, double & function_value);
159 
160  /**
161  * Compute first order statistics at the given point with respect to the
162  * validation data. (Gradient & Function value)
163  *
164  * \param[in] point Point at which we are computing the stats.
165  * \param[out] gradient Dense gradient
166  * \param[out] function_value Function value
167  *
168  */
170  const DenseVector& point, DenseVector& gradient, double &function_value);
171 
172  private:
173 
174  void compute_first_order_statistics(const ml_data& data, const DenseVector
175  &point, DenseVector& gradient, double & function_value, const size_t
176  mbStart = 0, const size_t mbSize = -1);
177 };
178 
179 
180 
181 } // supervised
182 } // turicreate
183 
184 #endif
void compute_first_order_statistics(const DenseVector &point, DenseVector &gradient, double &function_value, const size_t mbStart=0, const size_t mbSize=-1)
linear_regression_opt_interface(const ml_data &_data, const ml_data &_valid_data, linear_regression &_model, bool _feature_rescaling=true)
std::vector< std::string > get_status(const DenseVector &coefs, const std::vector< std::string > &stats)
std::vector< std::pair< std::string, size_t > > get_status_header(const std::vector< std::string > &stat_names)
void compute_validation_first_order_statistics(const DenseVector &point, DenseVector &gradient, double &function_value)
void compute_second_order_statistics(const DenseVector &point, DenseMatrix &hessian, DenseVector &gradient, double &function_value)