forked from zama-ai/concrete-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rf.py
292 lines (259 loc) · 11.2 KB
/
rf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""Implement RandomForest models."""
from typing import Any, Dict, Union
import numpy
import sklearn.ensemble
from ..sklearn.tree_to_numpy import tree_to_numpy
from .base import BaseTreeClassifierMixin, BaseTreeEstimatorMixin, BaseTreeRegressorMixin
# pylint: disable=too-many-instance-attributes
class RandomForestClassifier(BaseTreeClassifierMixin):
"""Implements the RandomForest classifier."""
sklearn_model_class = sklearn.ensemble.RandomForestClassifier
framework = "sklearn"
_is_a_public_cml_model = True
# pylint: disable-next=too-many-arguments
def __init__(
self,
n_bits: Union[int, Dict[str, int]] = 6,
n_estimators=20,
criterion="gini",
max_depth=4,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_features="sqrt",
max_leaf_nodes=None,
min_impurity_decrease=0.0,
bootstrap=True,
oob_score=False,
n_jobs=None,
random_state=None,
verbose=0,
warm_start=False,
class_weight=None,
ccp_alpha=0.0,
max_samples=None,
):
"""Initialize the RandomForestClassifier.
# noqa: DAR101
"""
# Call BaseClassifier's __init__ method
super().__init__(n_bits=n_bits)
self.n_estimators = n_estimators
self.bootstrap = bootstrap
self.oob_score = oob_score
self.n_jobs = n_jobs
self.random_state = random_state
self.verbose = verbose
self.warm_start = warm_start
self.class_weight = class_weight
self.max_samples = max_samples
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes
self.min_impurity_decrease = min_impurity_decrease
self.ccp_alpha = ccp_alpha
def post_processing(self, y_preds: numpy.ndarray) -> numpy.ndarray:
# Here, we want to use BaseTreeEstimatorMixin's `post-processing` method as
# RandomForestClassifier models directly computes probabilities and therefore don't require
# to apply a sigmoid or softmax in post-processing
return BaseTreeEstimatorMixin.post_processing(self, y_preds)
def dump_dict(self) -> Dict[str, Any]:
metadata: Dict[str, Any] = {}
# Concrete-ML
metadata["n_bits"] = self.n_bits
metadata["sklearn_model"] = self.sklearn_model
metadata["_is_fitted"] = self._is_fitted
metadata["_is_compiled"] = self._is_compiled
metadata["input_quantizers"] = self.input_quantizers
metadata["output_quantizers"] = self.output_quantizers
metadata["onnx_model_"] = self.onnx_model_
metadata["framework"] = self.framework
metadata["post_processing_params"] = self.post_processing_params
metadata["_fhe_ensembling"] = self._fhe_ensembling
# Scikit-Learn
metadata["n_estimators"] = self.n_estimators
metadata["bootstrap"] = self.bootstrap
metadata["oob_score"] = self.oob_score
metadata["n_jobs"] = self.n_jobs
metadata["random_state"] = self.random_state
metadata["verbose"] = self.verbose
metadata["warm_start"] = self.warm_start
metadata["class_weight"] = self.class_weight
metadata["max_samples"] = self.max_samples
metadata["criterion"] = self.criterion
metadata["max_depth"] = self.max_depth
metadata["min_samples_split"] = self.min_samples_split
metadata["min_samples_leaf"] = self.min_samples_leaf
metadata["min_weight_fraction_leaf"] = self.min_weight_fraction_leaf
metadata["max_features"] = self.max_features
metadata["max_leaf_nodes"] = self.max_leaf_nodes
metadata["min_impurity_decrease"] = self.min_impurity_decrease
metadata["ccp_alpha"] = self.ccp_alpha
return metadata
@classmethod
def load_dict(cls, metadata: Dict):
# Instantiate the model
obj = RandomForestClassifier(n_bits=metadata["n_bits"])
# Concrete-ML
obj.sklearn_model = metadata["sklearn_model"]
obj._is_fitted = metadata["_is_fitted"]
obj._is_compiled = metadata["_is_compiled"]
obj.input_quantizers = metadata["input_quantizers"]
obj.framework = metadata["framework"]
obj.onnx_model_ = metadata["onnx_model_"]
obj.output_quantizers = metadata["output_quantizers"]
obj._fhe_ensembling = metadata["_fhe_ensembling"]
obj._tree_inference = tree_to_numpy(
obj.sklearn_model,
numpy.zeros((len(obj.input_quantizers),))[None, ...],
framework=obj.framework,
output_n_bits=obj.n_bits["op_leaves"] if isinstance(obj.n_bits, Dict) else obj.n_bits,
fhe_ensembling=obj._fhe_ensembling,
)[0]
obj.post_processing_params = metadata["post_processing_params"]
# Scikit-Learn
obj.n_estimators = metadata["n_estimators"]
obj.bootstrap = metadata["bootstrap"]
obj.oob_score = metadata["oob_score"]
obj.n_jobs = metadata["n_jobs"]
obj.random_state = metadata["random_state"]
obj.verbose = metadata["verbose"]
obj.warm_start = metadata["warm_start"]
obj.class_weight = metadata["class_weight"]
obj.max_samples = metadata["max_samples"]
obj.criterion = metadata["criterion"]
obj.max_depth = metadata["max_depth"]
obj.min_samples_split = metadata["min_samples_split"]
obj.min_samples_leaf = metadata["min_samples_leaf"]
obj.min_weight_fraction_leaf = metadata["min_weight_fraction_leaf"]
obj.max_features = metadata["max_features"]
obj.max_leaf_nodes = metadata["max_leaf_nodes"]
obj.min_impurity_decrease = metadata["min_impurity_decrease"]
obj.ccp_alpha = metadata["ccp_alpha"]
return obj
# pylint: disable=too-many-instance-attributes
class RandomForestRegressor(BaseTreeRegressorMixin):
"""Implements the RandomForest regressor."""
sklearn_model_class = sklearn.ensemble.RandomForestRegressor
framework = "sklearn"
_is_a_public_cml_model = True
# pylint: disable-next=too-many-arguments
def __init__(
self,
n_bits: Union[int, Dict[str, int]] = 6,
n_estimators=20,
criterion="squared_error",
max_depth=4,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.0,
max_features=1.0,
max_leaf_nodes=None,
min_impurity_decrease=0.0,
bootstrap=True,
oob_score=False,
n_jobs=None,
random_state=None,
verbose=0,
warm_start=False,
ccp_alpha=0.0,
max_samples=None,
):
"""Initialize the RandomForestRegressor.
# noqa: DAR101
"""
# Call BaseTreeEstimatorMixin's __init__ method
super().__init__(n_bits=n_bits)
self.n_estimators = n_estimators
self.bootstrap = bootstrap
self.oob_score = oob_score
self.n_jobs = n_jobs
self.random_state = random_state
self.verbose = verbose
self.warm_start = warm_start
self.max_samples = max_samples
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes
self.min_impurity_decrease = min_impurity_decrease
self.ccp_alpha = ccp_alpha
def dump_dict(self) -> Dict[str, Any]:
metadata: Dict[str, Any] = {}
# Concrete-ML
metadata["n_bits"] = self.n_bits
metadata["sklearn_model"] = self.sklearn_model
metadata["_is_fitted"] = self._is_fitted
metadata["_is_compiled"] = self._is_compiled
metadata["input_quantizers"] = self.input_quantizers
metadata["output_quantizers"] = self.output_quantizers
metadata["onnx_model_"] = self.onnx_model_
metadata["framework"] = self.framework
metadata["post_processing_params"] = self.post_processing_params
metadata["_fhe_ensembling"] = self._fhe_ensembling
# Scikit-Learn
metadata["n_estimators"] = self.n_estimators
metadata["bootstrap"] = self.bootstrap
metadata["oob_score"] = self.oob_score
metadata["n_jobs"] = self.n_jobs
metadata["random_state"] = self.random_state
metadata["verbose"] = self.verbose
metadata["warm_start"] = self.warm_start
metadata["max_samples"] = self.max_samples
metadata["criterion"] = self.criterion
metadata["max_depth"] = self.max_depth
metadata["min_samples_split"] = self.min_samples_split
metadata["min_samples_leaf"] = self.min_samples_leaf
metadata["min_weight_fraction_leaf"] = self.min_weight_fraction_leaf
metadata["max_features"] = self.max_features
metadata["max_leaf_nodes"] = self.max_leaf_nodes
metadata["min_impurity_decrease"] = self.min_impurity_decrease
metadata["ccp_alpha"] = self.ccp_alpha
return metadata
@classmethod
def load_dict(cls, metadata: Dict):
# Instantiate the model
obj = RandomForestRegressor(n_bits=metadata["n_bits"])
# Concrete-ML
obj.sklearn_model = metadata["sklearn_model"]
obj._is_fitted = metadata["_is_fitted"]
obj._is_compiled = metadata["_is_compiled"]
obj.input_quantizers = metadata["input_quantizers"]
obj.framework = metadata["framework"]
obj.onnx_model_ = metadata["onnx_model_"]
obj.output_quantizers = metadata["output_quantizers"]
obj._fhe_ensembling = metadata["_fhe_ensembling"]
obj._tree_inference = tree_to_numpy(
obj.sklearn_model,
numpy.zeros((len(obj.input_quantizers),))[None, ...],
framework=obj.framework,
output_n_bits=obj.n_bits["op_leaves"] if isinstance(obj.n_bits, Dict) else obj.n_bits,
fhe_ensembling=obj._fhe_ensembling,
)[0]
obj.post_processing_params = metadata["post_processing_params"]
# Scikit-Learn
obj.n_estimators = metadata["n_estimators"]
obj.bootstrap = metadata["bootstrap"]
obj.oob_score = metadata["oob_score"]
obj.n_jobs = metadata["n_jobs"]
obj.random_state = metadata["random_state"]
obj.verbose = metadata["verbose"]
obj.warm_start = metadata["warm_start"]
obj.max_samples = metadata["max_samples"]
obj.criterion = metadata["criterion"]
obj.max_depth = metadata["max_depth"]
obj.min_samples_split = metadata["min_samples_split"]
obj.min_samples_leaf = metadata["min_samples_leaf"]
obj.min_weight_fraction_leaf = metadata["min_weight_fraction_leaf"]
obj.max_features = metadata["max_features"]
obj.max_leaf_nodes = metadata["max_leaf_nodes"]
obj.min_impurity_decrease = metadata["min_impurity_decrease"]
obj.ccp_alpha = metadata["ccp_alpha"]
return obj