-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathtest_sklearn_linear_converter.py
846 lines (617 loc) · 32.6 KB
/
test_sklearn_linear_converter.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
"""
Tests sklearn linear classifiers (LinearRegression, LogisticRegression, SGDClassifier, LogisticRegressionCV) converters.
"""
import unittest
import warnings
from packaging.version import Version, parse
import numpy as np
import torch
from sklearn.linear_model import (
LinearRegression,
LogisticRegression,
SGDClassifier,
LogisticRegressionCV,
RidgeCV,
Lasso,
ElasticNet,
Ridge,
TweedieRegressor,
PoissonRegressor,
GammaRegressor,
)
from sklearn import datasets
import hummingbird.ml
from hummingbird.ml._utils import tvm_installed, pandas_installed
from hummingbird.ml import constants
if pandas_installed():
import pandas
class TestSklearnLinearClassifiers(unittest.TestCase):
# LogisticRegression test function to be parameterized
def _test_logistic_regression(
self, num_classes, solver="liblinear", multi_class="auto", labels_shift=0, fit_intercept=True
):
if num_classes > 2:
model = LogisticRegression(solver=solver, multi_class=multi_class, fit_intercept=fit_intercept)
else:
model = LogisticRegression(solver="liblinear", fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = np.random.randint(num_classes, size=100) + labels_shift
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict_proba(X), torch_model.predict_proba(X), rtol=1e-6, atol=1e-6)
# LogisticRegression binary
def test_logistic_regression_bi(self):
self._test_logistic_regression(2)
# LogisticRegression multiclass with auto
def test_logistic_regression_multi_auto(self):
self._test_logistic_regression(3)
# LogisticRegression with class labels shifted
def test_logistic_regression_shifted_classes(self):
self._test_logistic_regression(3, labels_shift=2)
# LogisticRegression with multi+ovr
def test_logistic_regression_multi_ovr(self):
self._test_logistic_regression(3, multi_class="ovr")
# LogisticRegression with multi+multinomial+sag
def test_logistic_regression_multi_multin_sag(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression(3, multi_class="multinomial", solver="sag")
# LogisticRegression binary lbfgs
def test_logistic_regression_bi_lbfgs(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression(2, solver="lbfgs")
# LogisticRegression with multi+lbfgs
def test_logistic_regression_multi_lbfgs(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression(3, solver="lbfgs")
# LogisticRegression with multi+multinomial+lbfgs
def test_logistic_regression_multi_multin_lbfgs(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression(3, multi_class="multinomial", solver="lbfgs")
# LogisticRegression with multi+ovr+lbfgs
def test_logistic_regression_multi_ovr_lbfgs(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression(3, multi_class="ovr", solver="lbfgs")
# LogisticRegression with fit_intercept set to False
def test_logistic_regression_no_intercept(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression(3, fit_intercept=False)
# LinearRegression test function to be parameterized
def _test_linear_regression(self, y_input, fit_intercept=True):
model = LinearRegression(fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = y_input
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
# LinearRegression with ints
def test_linear_regression_int(self):
np.random.seed(0)
self._test_linear_regression(np.random.randint(2, size=100))
# LinearRegression with floats
def test_linear_regression_float(self):
np.random.seed(0)
self._test_linear_regression(np.random.rand(100))
# LinearRegression with fit_intercept set to False
def test_linear_regression_no_intercept(self):
np.random.seed(0)
self._test_linear_regression(np.random.rand(100), fit_intercept=False)
# Lasso test function to be parameterized
def _test_lasso(self, y_input, fit_intercept=True):
model = Lasso(fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = y_input
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
# Lasso with ints
def test_lasso_int(self):
np.random.seed(0)
self._test_lasso(np.random.randint(2, size=100))
# Lasso with floats
def test_lasso_float(self):
np.random.seed(0)
self._test_lasso(np.random.rand(100))
# Lasso with fit_intercept set to False
def test_lasso_no_intercept(self):
np.random.seed(0)
self._test_lasso(np.random.rand(100), fit_intercept=False)
# Ridge test function to be parameterized
def _test_ridge(self, y_input, fit_intercept=True):
model = Ridge(fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = y_input
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
# Ridge with ints
def test_ridge_int(self):
np.random.seed(0)
self._test_ridge(np.random.randint(2, size=100))
# Ridge with floats
def test_ridge_float(self):
np.random.seed(0)
self._test_ridge(np.random.rand(100))
# Ridge with fit_intercept set to False
def test_ridge_no_intercept(self):
np.random.seed(0)
self._test_ridge(np.random.rand(100), fit_intercept=False)
# ElasticNet test function to be parameterized
def _test_elastic_net(self, y_input, fit_intercept=True):
model = ElasticNet(fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = y_input
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
# ElasticNet with ints
def test_elastic_net_int(self):
np.random.seed(0)
self._test_elastic_net(np.random.randint(2, size=100))
# ElasticNet with floats
def test_elastic_net_float(self):
np.random.seed(0)
self._test_elastic_net(np.random.rand(100))
# ElasticNet with fit_intercept set to False
def test_elastic_net_no_intercept(self):
np.random.seed(0)
self._test_elastic_net(np.random.rand(100), fit_intercept=False)
# RidgeCV test function to be parameterized
def _test_ridge_cv(self, y_input, fit_intercept=True):
model = RidgeCV(fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = y_input
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
# RidgeCV with ints
def test_ridge_cv_int(self):
np.random.seed(0)
self._test_ridge_cv(np.random.randint(2, size=100))
# RidgeCV with floats
def test_ridge_cv_float(self):
np.random.seed(0)
self._test_ridge_cv(np.random.rand(100))
# RidgeCV with fit_intercept set to False
def test_ridge_cv_no_intercept(self):
np.random.seed(0)
self._test_ridge_cv(np.random.rand(100), fit_intercept=False)
# LogisticRegressionCV test function to be parameterized
def _test_logistic_regression_cv(
self, num_classes, solver="liblinear", multi_class="auto", labels_shift=0, fit_intercept=True
):
if num_classes > 2:
model = LogisticRegressionCV(solver=solver, multi_class=multi_class, fit_intercept=fit_intercept)
else:
model = LogisticRegressionCV(solver="liblinear", fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = np.random.randint(num_classes, size=100) + labels_shift
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict_proba(X), torch_model.predict_proba(X), rtol=1e-6, atol=1e-6)
# LogisticRegressionCV with 2 classes
def test_logistic_regression_cv_bi(self):
self._test_logistic_regression_cv(2)
# LogisticRegressionCV with 3 classes
def test_logistic_regression_cv_multi(self):
self._test_logistic_regression_cv(3)
# LogisticRegressionCV with shifted classes
def test_logistic_regression_cv_shifted_classes(self):
self._test_logistic_regression_cv(3, labels_shift=2)
# LogisticRegressionCV with multi+ovr
def test_logistic_regression_cv_multi_ovr(self):
self._test_logistic_regression_cv(3, multi_class="ovr")
# LogisticRegressionCV with multi+multinomial
def test_logistic_regression_cv_multi_multin(self):
warnings.filterwarnings("ignore")
# this will not converge due to small test size
self._test_logistic_regression_cv(3, multi_class="multinomial", solver="sag")
# LogisticRegressionCV with fit_intercept set to False
def test_logistic_regression_cv_no_intercept(self):
self._test_logistic_regression_cv(3, fit_intercept=False)
# SGDClassifier test function to be parameterized
def _test_sgd_classifier(self, num_classes, fit_intercept=True):
model = SGDClassifier(loss="log_loss", fit_intercept=fit_intercept)
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = np.random.randint(num_classes, size=100)
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict_proba(X), torch_model.predict_proba(X), rtol=1e-6, atol=1e-6)
# SGDClassifier with 2 classes
def test_sgd_classifier_bi(self):
self._test_sgd_classifier(2)
# SGDClassifier with 3 classes
def test_sgd_classifier_multi(self):
self._test_sgd_classifier(3)
# SGDClassifier with fit_intercept set to False
def test_sgd_classifier_no_intercept(self):
self._test_sgd_classifier(3, fit_intercept=False)
# SGDClassifier with log loss
def test_log_loss(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="log_loss", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict_proba(inputs), hb_model.predict_proba(inputs), rtol=1e-6, atol=1e-6)
def test_log_loss2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="log_loss", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict_proba(X), hb_model.predict_proba(X), rtol=1e-6, atol=1e-6)
def test_log_loss__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="log_loss", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict_proba(inputs), hb_model.predict_proba(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with modified huber loss
def test_modified_huber(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="modified_huber", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict_proba(inputs), hb_model.predict_proba(inputs), rtol=1e-6, atol=1e-6)
def test_modified_huber2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="modified_huber", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict_proba(X), hb_model.predict_proba(X), rtol=1e-6, atol=1e-6)
def test_modified_huber_multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="modified_huber", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict_proba(inputs), hb_model.predict_proba(inputs), rtol=1e-6, atol=1e-6)
# Only log_loss and modified_huber support the probability
# SGDClassifier with squared_hinge
def test_squared_hinge(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_hinge", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_squared_hinge2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_hinge", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_squared_hinge__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_hinge", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with hinge
def test_hinge(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="hinge", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_hinge2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="hinge", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_hinge__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="hinge", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with huber
def test_huber(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="huber", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_huber2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="huber", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_huber__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="huber", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with perceptron
def test_perceptron(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="perceptron", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_perceptron2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="perceptron", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_perceptron__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="perceptron", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with epsilon_insensitive
def test_epsilon_insensitive(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="epsilon_insensitive", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_epsilon_insensitive2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="epsilon_insensitive", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_epsilon_insensitive__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="epsilon_insensitive", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with squared_error
def test_squared_error(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_error", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_squared_error2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_error", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_squared_error__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_error", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# SGDClassifier with squared_epsilon_insensitive
def test_squared_epsilon_insensitive(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_epsilon_insensitive", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
def test_squared_epsilon_insensitive2(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([1, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_epsilon_insensitive", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
np.testing.assert_allclose(model.predict(X), hb_model.predict(X), rtol=1e-6, atol=1e-6)
def test_squared_epsilon_insensitive__multi(self):
X = np.array([[-0.5, -1], [-1, -1], [-0.1, -0.1], [0.1, -0.2], [0.5, 1], [1, 1], [0.1, 0.1], [-0.1, 0.2]])
Y = np.array([0, 1, 1, 1, 2, 2, 2, 2])
model = SGDClassifier(loss="squared_epsilon_insensitive", max_iter=1000, tol=1e-3)
model.fit(X, Y)
# Use Hummingbird to convert the model to PyTorch
hb_model = hummingbird.ml.convert(model, "torch")
inputs = [[-1, -1], [1, 1], [-0.2, 0.1], [0.2, -0.1]]
np.testing.assert_allclose(model.predict(inputs), hb_model.predict(inputs), rtol=1e-6, atol=1e-6)
# Failure cases
def test_sklearn_linear_model_raises_wrong_type(self):
warnings.filterwarnings("ignore")
np.random.seed(0)
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = np.random.randint(3, size=100).astype(np.float32) # y must be int, not float, should error
model = SGDClassifier().fit(X, y)
self.assertRaises(RuntimeError, hummingbird.ml.convert, model, "torch")
# Float 64 data tests
def test_float64_linear_regression(self):
model = LinearRegression()
np.random.seed(0)
X = np.random.rand(100, 200)
y = np.random.randint(2, size=100)
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
def test_float64_sgd_classifier(self):
model = SGDClassifier(loss="log_loss")
np.random.seed(0)
num_classes = 3
X = np.random.rand(100, 200)
y = np.random.randint(num_classes, size=100)
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-6, atol=1e-6)
# Multioutput regression tests
def test_multioutput_linear_regression(self):
for n_targets in [1, 2, 7]:
model = LinearRegression()
X, y = datasets.make_regression(
n_samples=100, n_features=10, n_informative=5, n_targets=n_targets, random_state=2021
)
model.fit(X, y)
torch_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(torch_model is not None)
np.testing.assert_allclose(model.predict(X), torch_model.predict(X), rtol=1e-5, atol=1e-5)
# Test Pandas input
@unittest.skipIf(not pandas_installed(), reason="Test requires pandas installed")
def test_logistic_regression_pandas(self):
model = LogisticRegression(solver="liblinear")
data = datasets.load_iris()
X, y = data.data[:, :3], data.target
X = X.astype(np.float32)
X_train = pandas.DataFrame(X, columns=["vA", "vB", "vC"])
X_train["vcat"] = X_train["vA"].apply(lambda x: 1 if x > 0.5 else 2)
X_train["vcat2"] = X_train["vB"].apply(lambda x: 3 if x > 0.5 else 4)
y_train = y % 2
model.fit(X_train, y_train)
hb_model = hummingbird.ml.convert(model, "torch")
self.assertTrue(hb_model is not None)
np.testing.assert_allclose(model.predict(X_train), hb_model.predict(X_train), rtol=1e-6, atol=1e-6)
np.testing.assert_allclose(model.predict_proba(X_train), hb_model.predict_proba(X_train), rtol=1e-6, atol=1e-6)
# Test Torschscript backend.
def test_logistic_regression_ts(self):
model = LogisticRegression(solver="liblinear")
data = datasets.load_iris()
X, y = data.data, data.target
X = X.astype(np.float32)
model.fit(X, y)
ts_model = hummingbird.ml.convert(model, "torch.jit", X)
self.assertTrue(ts_model is not None)
np.testing.assert_allclose(model.predict(X), ts_model.predict(X), rtol=1e-6, atol=1e-6)
np.testing.assert_allclose(model.predict_proba(X), ts_model.predict_proba(X), rtol=1e-6, atol=1e-6)
# Test TVM backends.
@unittest.skipIf(not (tvm_installed()), reason="TVM tests require TVM")
def test_sgd_classifier_tvm(self):
model = SGDClassifier(loss="log_loss")
np.random.seed(0)
num_classes = 3
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = np.random.randint(num_classes, size=100)
model.fit(X, y)
tvm_model = hummingbird.ml.convert(model, "tvm", X)
self.assertTrue(tvm_model is not None)
np.testing.assert_allclose(model.predict(X), tvm_model.predict(X), rtol=1e-6, atol=1e-6)
np.testing.assert_allclose(model.predict_proba(X), tvm_model.predict_proba(X), rtol=1e-6, atol=1e-6)
@unittest.skipIf(not (tvm_installed()), reason="TVM tests require TVM")
def test_lr_tvm(self):
model = LinearRegression()
np.random.seed(0)
num_classes = 1000
X = np.random.rand(100, 200)
X = np.array(X, dtype=np.float32)
y = np.random.randint(num_classes, size=100)
model.fit(X, y)
tvm_model = hummingbird.ml.convert(model, "tvm", X, extra_config={constants.TVM_MAX_FUSE_DEPTH: 30})
self.assertTrue(tvm_model is not None)
np.testing.assert_allclose(model.predict(X), tvm_model.predict(X), rtol=1e-6, atol=1e-3)
def test_tweedie_regressor(self):
clf = TweedieRegressor()
X = [[1, 2], [2, 3], [3, 4], [4, 3]]
y = [2, 3.5, 5, 5.5]
clf.fit(X, y)
hb_model = hummingbird.ml.convert(clf, "torch")
np.testing.assert_allclose(clf.predict([[1, 1], [3, 4]]), hb_model.predict([[1, 1], [3, 4]]), rtol=1e-6, atol=1e-3)
def test_poisson_regressor(self):
clf = PoissonRegressor()
X = [[1, 2], [2, 3], [3, 4], [4, 3]]
y = [12, 17, 22, 21]
clf.fit(X, y)
hb_model = hummingbird.ml.convert(clf, "torch")
np.testing.assert_allclose(clf.predict([[1, 1], [3, 4]]), hb_model.predict([[1, 1], [3, 4]]), rtol=1e-6, atol=1e-3)
def test_gamma_regressor(self):
clf = GammaRegressor()
X = [[1, 2], [2, 3], [3, 4], [4, 3]]
y = [19, 26, 33, 30]
clf.fit(X, y)
hb_model = hummingbird.ml.convert(clf, "torch")
np.testing.assert_allclose(clf.predict([[1, 1], [3, 4]]), hb_model.predict([[1, 1], [3, 4]]), rtol=1e-6, atol=1e-3)
if __name__ == "__main__":
unittest.main()