/
glm.py
316 lines (251 loc) · 9.7 KB
/
glm.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""Implement sklearn's Generalized Linear Models (GLM)."""
from __future__ import annotations
import copy
from abc import abstractmethod
from typing import Union
import numpy
import sklearn
import torch
from ..common.check_inputs import check_X_y_and_assert
from ..common.debugging.custom_assert import assert_true
from ..quantization import PostTrainingAffineQuantization
from ..torch.numpy_module import NumpyModule
from .base import SklearnLinearModelMixin
from .torch_module import _LinearRegressionTorchModel
# pylint: disable=too-many-instance-attributes
class _GeneralizedLinearRegressor(SklearnLinearModelMixin, sklearn.base.RegressorMixin):
"""Regression via a penalized Generalized Linear Model (GLM) with FHE."""
# The inheritance method does not inherit directly from the related Sklearn model and therefore
# is not initialized by using super()
# pylint: disable=super-init-not-called
def __init__(
self,
*,
n_bits: Union[int, dict] = 2,
alpha: float = 1.0,
fit_intercept: bool = True,
solver: str = "lbfgs",
max_iter: int = 100,
tol: float = 1e-4,
warm_start: bool = False,
verbose: int = 0,
):
self.n_bits = n_bits
self.alpha = alpha
self.fit_intercept = fit_intercept
self.solver = solver
self.max_iter = max_iter
self.tol = tol
self.warm_start = warm_start
self.verbose = verbose
self._onnx_model_ = None
super().__init__(n_bits=n_bits)
# pylint: enable=super-init-not-called
@property
def onnx_model(self):
return self._onnx_model_
def fit(self, X, y: numpy.ndarray, *args, **kwargs) -> None:
"""Fit the GLM regression quantized model.
Args:
X : The training data, which can be:
* numpy arrays
* torch tensors
* pandas DataFrame or Series
y (numpy.ndarray): The target data.
*args: The arguments to pass to the sklearn linear model.
**kwargs: The keyword arguments to pass to the sklearn linear model.
"""
# GLMS don't handle the use of a sum workaround
kwargs.pop("use_sum_workaround", None)
# Copy X and check that is has a proper type
X = copy.deepcopy(X)
X, y = check_X_y_and_assert(X, y)
# Retrieving the Sklearn parameters
params = self.get_params()
params.pop("n_bits", None)
# Initialize a sklearn generalized linear model
# pylint: disable=attribute-defined-outside-init
self.sklearn_model = self.sklearn_alg(**params)
# Fit the sklearn model
self.sklearn_model.fit(X, y, *args, **kwargs)
# Extract the weights
weight = self.sklearn_model.coef_
# Extract the input and output sizes
input_size = weight.shape[0]
output_size = weight.shape[1] if len(weight.shape) > 1 else 1
# Initialize the Torch model. Using a small Torch model that reproduces the proper
# inference is necessary in this case because the Hummingbird library, which is used for
# converting a Sklearn model into an ONNX one, doesn't not support GLMs. Also, the Torch
# module can be given to the NumpyModule class the same way it is done for its ONNX
# equivalent, thus making the initial workflow still relevant.
torch_model = _LinearRegressionTorchModel(
input_size=input_size,
output_size=output_size,
use_bias=self.fit_intercept,
)
# Update the Torch model's weights and bias using the Sklearn model's one
torch_model.linear.weight.data = torch.from_numpy(weight).reshape(output_size, input_size)
if self.fit_intercept:
torch_model.linear.bias.data = torch.tensor(self.sklearn_model.intercept_)
# Create a NumpyModule from the Torch model
numpy_module = NumpyModule(
torch_model,
dummy_input=torch.from_numpy(X[0]),
)
self._onnx_model_ = numpy_module.onnx_model
# Apply post-training quantization
post_training = PostTrainingAffineQuantization(
n_bits=self.n_bits, numpy_model=numpy_module, is_signed=True
)
# Calibrate and create quantize module
self.quantized_module_ = post_training.quantize_module(X)
# pylint: enable=attribute-defined-outside-init
def post_processing(
self, y_preds: numpy.ndarray, already_dequantized: bool = False
) -> numpy.ndarray:
"""Post-processing the predictions.
Args:
y_preds (numpy.ndarray): The predictions to post-process.
already_dequantized (bool): Wether the inputs were already dequantized or not. Default
to False.
Returns:
numpy.ndarray: The post-processed predictions.
"""
# If y_preds were already dequantized previously, there is no need to do so once again.
# This step is necessary for the client-server workflow as the post_processing method
# is directly called on the quantized outputs, contrary to the base class' predict method.
if not already_dequantized:
y_preds = self.quantized_module_.dequantize_output(y_preds)
return self._inverse_link(y_preds)
def predict(self, X: numpy.ndarray, execute_in_fhe: bool = False) -> numpy.ndarray:
"""Predict on user data.
Predict on user data using either the quantized clear model, implemented with tensors, or,
if execute_in_fhe is set, using the compiled FHE circuit.
Args:
X (numpy.ndarray): The input data.
execute_in_fhe (bool): Whether to execute the inference in FHE. Default to False.
Returns:
numpy.ndarray: The model's predictions.
"""
y_preds = super().predict(X, execute_in_fhe=execute_in_fhe)
y_preds = self.post_processing(y_preds, already_dequantized=True)
return y_preds
@abstractmethod
def _inverse_link(self, y_preds):
"""Apply the link function's inverse on the inputs.
Args:
y_preds (numpy.ndarray): The input data.
"""
# pylint: enable=too-many-instance-attributes
class PoissonRegressor(_GeneralizedLinearRegressor):
"""A Poisson regression model with FHE."""
sklearn_alg = sklearn.linear_model.PoissonRegressor
def __init__(
self,
*,
n_bits: Union[int, dict] = 2,
alpha: float = 1.0,
fit_intercept: bool = True,
max_iter: int = 100,
tol: float = 1e-4,
warm_start: bool = False,
verbose: int = 0,
):
super().__init__(
n_bits=n_bits,
alpha=alpha,
fit_intercept=fit_intercept,
max_iter=max_iter,
tol=tol,
warm_start=warm_start,
verbose=verbose,
)
def _inverse_link(self, y_preds) -> numpy.ndarray:
"""Apply the link function's inverse on the inputs.
PoissonRegressor uses the exponential function.
Args:
y_preds (numpy.ndarray): The input data.
Returns:
The model's final predictions.
"""
return numpy.exp(y_preds)
class GammaRegressor(_GeneralizedLinearRegressor):
"""A Gamma regression model with FHE."""
sklearn_alg = sklearn.linear_model.GammaRegressor
def __init__(
self,
*,
n_bits: Union[int, dict] = 2,
alpha: float = 1.0,
fit_intercept: bool = True,
max_iter: int = 100,
tol: float = 1e-4,
warm_start: bool = False,
verbose: int = 0,
):
super().__init__(
n_bits=n_bits,
alpha=alpha,
fit_intercept=fit_intercept,
max_iter=max_iter,
tol=tol,
warm_start=warm_start,
verbose=verbose,
)
def _inverse_link(self, y_preds) -> numpy.ndarray:
"""Apply the link function's inverse on the inputs.
GammaRegressor uses the exponential function.
Args:
y_preds (numpy.ndarray): The input data.
Returns:
The model's final predictions.
"""
return numpy.exp(y_preds)
class TweedieRegressor(_GeneralizedLinearRegressor):
"""A Tweedie regression model with FHE."""
sklearn_alg = sklearn.linear_model.TweedieRegressor
def __init__(
self,
*,
n_bits: Union[int, dict] = 2,
power: float = 0.0,
alpha: float = 1.0,
fit_intercept: bool = True,
link: str = "auto",
max_iter: int = 100,
tol: float = 1e-4,
warm_start: bool = False,
verbose: int = 0,
):
super().__init__(
n_bits=n_bits,
alpha=alpha,
fit_intercept=fit_intercept,
max_iter=max_iter,
tol=tol,
warm_start=warm_start,
verbose=verbose,
)
assert_true(
link in ["auto", "log", "identity"],
f"link must be an element of ['auto', 'identity', 'log'], got '{link}'",
)
self.power = power
self.link = link
def _inverse_link(self, y_preds) -> numpy.ndarray:
"""Apply the link function's inverse on the inputs.
TweedieRegressor uses either the identity or the exponential function.
Args:
y_preds (numpy.ndarray): The input data.
Returns:
The model's final predictions.
"""
if self.link == "auto":
# Identity link
if self.power <= 0:
return y_preds
# Log link
return numpy.exp(y_preds)
if self.link == "log":
return numpy.exp(y_preds)
return y_preds