55
66import lightgbm
77import numpy
8+ from numpy .testing import assert_almost_equal
89from lightgbm import LGBMClassifier , LGBMRegressor
910import onnxruntime
1011from onnxmltools .convert .common .utils import hummingbird_installed
1112from onnxmltools .convert .common .data_types import FloatTensorType
13+ from onnxmltools .convert import convert_lightgbm
1214from onnxmltools .utils import dump_data_and_model
1315from onnxmltools .utils import dump_binary_classification , dump_multiple_classification
1416from onnxmltools .utils import dump_single_regression
@@ -32,6 +34,50 @@ def test_lightgbm_classifier_zipmap(self):
3234 model , 'dummy' , input_types = [('X' , FloatTensorType ([None , X .shape [1 ]]))])
3335 assert "zipmap" in str (onx ).lower ()
3436
37+ def test_lightgbm_classifier_nozipmap (self ):
38+ X = [[0 , 1 ], [1 , 1 ], [2 , 0 ], [1 , 2 ], [1 , 5 ], [6 , 2 ]]
39+ X = numpy .array (X , dtype = numpy .float32 )
40+ y = [0 , 1 , 0 , 1 , 1 , 0 ]
41+ model = LGBMClassifier (n_estimators = 3 , min_child_samples = 1 , max_depth = 2 )
42+ model .fit (X , y )
43+ onx = convert_model (
44+ model , 'dummy' , input_types = [('X' , FloatTensorType ([None , X .shape [1 ]]))],
45+ zipmap = False )
46+ assert "zipmap" not in str (onx ).lower ()
47+ onxs = onx [0 ].SerializeToString ()
48+ try :
49+ sess = onnxruntime .InferenceSession (onxs )
50+ except Exception as e :
51+ raise AssertionError (
52+ "Model cannot be loaded by onnxruntime due to %r\n %s." % (
53+ e , onx [0 ]))
54+ exp = model .predict (X ), model .predict_proba (X )
55+ got = sess .run (None , {'X' : X })
56+ assert_almost_equal (exp [0 ], got [0 ])
57+ assert_almost_equal (exp [1 ], got [1 ])
58+
59+ def test_lightgbm_classifier_nozipmap2 (self ):
60+ X = [[0 , 1 ], [1 , 1 ], [2 , 0 ], [1 , 2 ], [1 , 5 ], [6 , 2 ]]
61+ X = numpy .array (X , dtype = numpy .float32 )
62+ y = [0 , 1 , 0 , 1 , 1 , 0 ]
63+ model = LGBMClassifier (n_estimators = 3 , min_child_samples = 1 , max_depth = 2 )
64+ model .fit (X , y )
65+ onx = convert_lightgbm (
66+ model , 'dummy' , initial_types = [('X' , FloatTensorType ([None , X .shape [1 ]]))],
67+ zipmap = False )
68+ assert "zipmap" not in str (onx ).lower ()
69+ onxs = onx .SerializeToString ()
70+ try :
71+ sess = onnxruntime .InferenceSession (onxs )
72+ except Exception as e :
73+ raise AssertionError (
74+ "Model cannot be loaded by onnxruntime due to %r\n %s." % (
75+ e , onx [0 ]))
76+ exp = model .predict (X ), model .predict_proba (X )
77+ got = sess .run (None , {'X' : X })
78+ assert_almost_equal (exp [0 ], got [0 ])
79+ assert_almost_equal (exp [1 ], got [1 ])
80+
3581 def test_lightgbm_regressor (self ):
3682 model = LGBMRegressor (n_estimators = 3 , min_child_samples = 1 )
3783 dump_single_regression (model )
@@ -58,6 +104,22 @@ def test_lightgbm_booster_classifier(self):
58104 allow_failure = "StrictVersion(onnx.__version__) < StrictVersion('1.3.0')" ,
59105 basename = prefix + "BoosterBin" + model .__class__ .__name__ )
60106
107+ def test_lightgbm_booster_classifier_nozipmap (self ):
108+ X = [[0 , 1 ], [1 , 1 ], [2 , 0 ], [1 , 2 ]]
109+ X = numpy .array (X , dtype = numpy .float32 )
110+ y = [0 , 1 , 0 , 1 ]
111+ data = lightgbm .Dataset (X , label = y )
112+ model = lightgbm .train ({'boosting_type' : 'gbdt' , 'objective' : 'binary' ,
113+ 'n_estimators' : 3 , 'min_child_samples' : 1 },
114+ data )
115+ model_onnx , prefix = convert_model (model , 'tree-based classifier' ,
116+ [('input' , FloatTensorType ([None , 2 ]))],
117+ zipmap = False )
118+ assert "zipmap" not in str (model_onnx ).lower ()
119+ dump_data_and_model (X , model , model_onnx ,
120+ allow_failure = "StrictVersion(onnx.__version__) < StrictVersion('1.3.0')" ,
121+ basename = prefix + "BoosterBin" + model .__class__ .__name__ )
122+
61123 def test_lightgbm_booster_classifier_zipmap (self ):
62124 X = [[0 , 1 ], [1 , 1 ], [2 , 0 ], [1 , 2 ]]
63125 X = numpy .array (X , dtype = numpy .float32 )
0 commit comments