Turi Create  4.0
logistic_regression.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_LOGISTIC_REGRESSION_H_
7 #define TURI_REGR_LOGISTIC_REGRESSION_H_
8 
9 // ML-Data Utils
10 #include <ml/ml_data/ml_data.hpp>
11 
12 // Toolkits
13 #include <toolkits/supervised_learning/supervised_learning.hpp>
14 #include <toolkits/coreml_export/mlmodel_wrapper.hpp>
15 
16 // Optimization Interface
17 #include <ml/optimization/optimization_interface.hpp>
18 
19 #include <core/export.hpp>
20 
21 namespace turi {
22 namespace supervised {
23 
24 class logistic_regression_opt_interface;
25 
26 /*
27  * Logistic Regression Model
28  * ****************************************************************************
29  */
30 
31 /**
32  * Logistic regression model class definition.
33  *
34  */
36 
37  protected:
38 
39  bool m_simple_mode;
40 
41  std::shared_ptr<logistic_regression_opt_interface> lr_interface;
42  Eigen::Matrix<double, Eigen::Dynamic,1> coefs; /**< Coefs */
43  Eigen::Matrix<double, Eigen::Dynamic,1> std_err;
44 
45  size_t num_classes = 0; /**< fast access: num classes */
46  size_t num_coefficients= 0; /**< fast access: num coefs */
47  public:
48 
49  static constexpr size_t LOGISTIC_REGRESSION_MODEL_VERSION = 6;
50 
51  public:
52 
53  /**
54  * Destructor. Make sure bad things don't happen
55  */
57 
58 
59  /**
60  * Set the default evaluation metric during model evaluation..
61  */
63  set_evaluation_metric({
64  "accuracy",
65  "auc",
66  "confusion_matrix",
67  "f1_score",
68  "log_loss",
69  "precision",
70  "recall",
71  "roc_curve",
72  });
73  }
74 
75  /**
76  * Set the default evaluation metric for progress tracking.
77  */
78  void set_default_tracking_metric() override {
79  set_tracking_metric({
80  "accuracy",
81  });
82  }
83 
84  /**
85  * Initialize things that are specific to your model.
86  *
87  * \param[in] data ML-Data object created by the init function.
88  *
89  */
90  void model_specific_init(const ml_data& data, const ml_data& valid_data) override;
91 
92  bool is_classifier() const override { return true; }
93 
94  /**
95  * Initialize the options.
96  *
97  * \param[in] _options Options to set
98  */
99  void init_options(const std::map<std::string,flexible_type>& _options) override;
100 
101  /**
102  * Gets the model version number
103  */
104  size_t get_version() const override;
105 
106 
107  /**
108  * Train a regression model.
109  */
110  void train() override;
111 
112  /**
113  * Setter for model coefficieints.
114  */
115  void set_coefs(const DenseVector& _coefs) override;
116 
117  /**
118  * Serialize the object.
119  */
120  void save_impl(turi::oarchive& oarc) const override;
121 
122  /**
123  * Load the object
124  */
125  void load_version(turi::iarchive& iarc, size_t version) override;
126 
127  /**
128  * Predict for a single example.
129  *
130  * \param[in] x Single example.
131  * \param[in] output_type Type of prediction.
132  *
133  * \returns Prediction for a single example.
134  *
135  */
136  flexible_type predict_single_example(
137  const DenseVector& x,
138  const prediction_type_enum& output_type=prediction_type_enum::NA) override;
139 
140  /**
141  * Fast path predictions given a row of flexible_types.
142  *
143  * \param[in] rows List of rows (each row is a flex_dict)
144  * \param[in] output_type Output type.
145  */
146  gl_sframe fast_predict_topk(
147  const std::vector<flexible_type>& rows,
148  const std::string& missing_value_action ="error",
149  const std::string& output_type="",
150  const size_t topk = 5) override;
151 
152  /**
153  * Predict for a single example.
154  *
155  * \param[in] x Single example.
156  * \param[in] output_type Type of prediction.
157  *
158  * \returns Prediction for a single example.
159  *
160  */
161  flexible_type predict_single_example(
162  const SparseVector& x,
163  const prediction_type_enum& output_type=prediction_type_enum::NA) override;
164 
165  /**
166  * Get coefficients for a trained model.
167  */
168  void get_coefficients(DenseVector& _coefs) const{
169  _coefs.resize(coefs.size());
170  _coefs = coefs;
171  }
172 
173  std::shared_ptr<coreml::MLModelWrapper> export_to_coreml() override;
174 
175  BEGIN_CLASS_MEMBER_REGISTRATION("classifier_logistic_regression");
178 
179 };
180 } // supervised
181 } // turicreate
182 
183 #endif
#define BEGIN_CLASS_MEMBER_REGISTRATION(python_facing_classname)
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
#define IMPORT_BASE_CLASS_REGISTRATION(base_class)
#define END_CLASS_MEMBER_REGISTRATION
Eigen::Matrix< double, Eigen::Dynamic, 1 > coefs
void get_coefficients(DenseVector &_coefs) const
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80