Turi Create  4.0
transformer_base.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_TRANSFORMER_BASE_H
7 #define TURI_TRANSFORMER_BASE_H
8 
9 #include <core/data/sframe/gl_sframe.hpp>
10 #include <model_server/lib/extensions/ml_model.hpp>
11 #include <core/export.hpp>
12 
13 namespace turi{
14 namespace sdk_model {
15 namespace feature_engineering {
16 
17 /**
18  * transformer_base model interface.
19  * ---------------------------------------
20  *
21  * Base class for handling feature engineering transformers. This class is meant
22  * to be a guide to aid model writing for feature engineering modules.
23  *
24  * Each C++ toolkit contains the following:
25  *
26  * *) state: This is the key-value map that stores the "model" attributes.
27  * The value is of type "variant_type" which is fully interfaced
28  * with python. You can add basic types, vectors, SFrames etc.
29  *
30  *
31  * *) options: Option manager which keeps track of default options, current
32  * options, option ranges, type etc. This must be initialized only
33  * once in the set_options() function.
34  *
35  *
36  * Functions that should always be implemented. Here are some notes about
37  * each of these functions that may help guide you in writing your model.
38  *
39  * *) init_transformer : Initializer the transformer i.e this is the same as
40  * __init__ from the python side.
41  *
42  * *) fit : Fit the transformer with the data (SFrame)
43  *
44  * *) transform : Transform the data (SFrame) to another SFrame after the model
45  * has been fit.
46  *
47  * *) init_options : Initialize the options manager.
48  *
49  * *) save: Save the model with the turicreate iarc. Turi is a server-client
50  * module. DO NOT SAVE ANYTHING in the client side. Make sure that
51  * everything is in the server side. For example: You might be tempted
52  * do keep options that the user provides into the server side but
53  * DO NOT do that because save and load will break things for you!
54  *
55  * *) load: Load the model with the turicreate oarc.
56  *
57  * *) version: A get version for this model
58  */
59 class EXPORT transformer_base : public ml_model_base {
60  public:
61 
62  static constexpr size_t TRANSFORMER_BASE_VERSION = 0;
63 
64  public:
65 
66  virtual ~transformer_base() {}
67 
68  /**
69  * Returns the current model version
70  */
71  virtual size_t get_version() const = 0;
72 
73  /**
74  * Serializes the model. Must save the model to the file format version
75  * matching that of get_version()
76  */
77  virtual void save_impl(oarchive& oarc) const = 0;
78 
79  /**
80  * Loads a model previously saved at a particular version number.
81  * Should raise an exception on failure.
82  */
83  virtual void load_version(iarchive& iarc, size_t version) = 0;
84 
85 
86  /**
87  * Set one of the options in the algorithm. Use the option manager to set
88  * these options. If the option does not satisfy the conditions that the
89  * option manager has imposed on it. Errors will be thrown.
90  *
91  * \param[in] options Options to set
92  */
93  virtual void init_options(const std::map<std::string,
94  flexible_type>& _options) = 0;
95 
96  /**
97  * Init the transformer and return an instantiated object.
98  *
99  * \param[in] options (**kwargs from python)
100  *
101  * Python side interface
102  * ------------------------
103  * This function directly interfaces with "__init__" in python.
104  *
105  */
106  virtual void init_transformer(const std::map<std::string,
107  flexible_type>& _options) = 0;
108  /**
109  * Fit the transformer and make it ready for transformations.
110  *
111  * \param[in] data (SFrame of data)
112  *
113  * Python side interface
114  * ------------------------
115  * This function directly interfaces with "fit" in python.
116  *
117  */
118  virtual void fit(gl_sframe data) = 0;
119 
120  /**
121  * Transform the given data.
122  *
123  * \param[in] data (SFrame of data)
124  *
125  * Python side interface
126  * ------------------------
127  * This function directly interfaces with "transform" in python.
128  *
129  */
130  virtual gl_sframe transform(gl_sframe data) = 0;
131 
132  /**
133  * Fit and transform the given data. Intended as an optimization because
134  * fit and transform are usually always called together. The default
135  * implementaiton calls fit and then transform.
136  *
137  * \param[in] data (SFrame of data)
138  *
139  * Python side interface
140  * ------------------------
141  * This function directly interfaces with "fit_transform" in python.
142  *
143  */
145  fit(data);
146  return transform(data);
147  }
148 
149 };
150 
151 } // feature_engineering
152 } // sdk_model
153 } // turicreate
154 #endif
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
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
void transform(S &&input, T &&output, TransformFn transformfn, std::set< size_t > constraint_segments=std::set< size_t >())
Definition: algorithm.hpp:64