def test_lightgbm_booster_multi_classifier(self):
     X = [[0, 1], [1, 1], [2, 0], [1, 2], [-1, 2], [1, -2]]
     X = numpy.array(X, dtype=numpy.float32)
     y = [0, 1, 0, 1, 2, 2]
     data = lightgbm.Dataset(X, label=y)
     model = lightgbm.train(
         {
             'boosting_type': 'gbdt',
             'objective': 'multiclass',
             'n_estimators': 3,
             'min_child_samples': 1,
             'num_class': 3
         }, data)
     model_onnx, prefix = convert_model(
         model, 'tree-based classifier',
         [('input', FloatTensorType([None, 2]))])
     dump_data_and_model(
         X,
         model,
         model_onnx,
         allow_failure=
         "StrictVersion(onnx.__version__) < StrictVersion('1.3.0')",
         basename=prefix + "BoosterBin" + model.__class__.__name__)
     try:
         from onnxruntime import InferenceSession
     except ImportError:
         # onnxruntime not installed (python 2.7)
         return
     sess = InferenceSession(model_onnx.SerializeToString())
     out = sess.get_outputs()
     names = [o.name for o in out]
     assert names == ['label', 'probabilities']
 def test_lightgbm_classifier_nozipmap(self):
     X = [[0, 1], [1, 1], [2, 0], [1, 2], [1, 5], [6, 2]]
     X = numpy.array(X, dtype=numpy.float32)
     y = [0, 1, 0, 1, 1, 0]
     model = LGBMClassifier(n_estimators=3,
                            min_child_samples=1,
                            max_depth=2)
     model.fit(X, y)
     onx = convert_model(model,
                         'dummy',
                         input_types=[('X',
                                       FloatTensorType([None,
                                                        X.shape[1]]))],
                         zipmap=False)
     assert "zipmap" not in str(onx).lower()
     onxs = onx[0].SerializeToString()
     try:
         sess = onnxruntime.InferenceSession(onxs)
     except Exception as e:
         raise AssertionError(
             "Model cannot be loaded by onnxruntime due to %r\n%s." %
             (e, onx[0]))
     exp = model.predict(X), model.predict_proba(X)
     got = sess.run(None, {'X': X})
     assert_almost_equal(exp[0], got[0])
     assert_almost_equal(exp[1], got[1])
 def test_lightgbm_booster_regressor(self):
     X = [[0, 1], [1, 1], [2, 0]]
     X = numpy.array(X, dtype=numpy.float32)
     y = [0, 1, 1.1]
     data = lightgbm.Dataset(X, label=y)
     model = lightgbm.train(
         {
             'boosting_type': 'gbdt',
             'objective': 'regression',
             'n_estimators': 3,
             'min_child_samples': 1,
             'max_depth': 1
         }, data)
     model_onnx, prefix = convert_model(
         model,
         'tree-based binary classifier',
         [('input', FloatTensorType([None, 2]))],
         without_onnx_ml=True)
     dump_data_and_model(
         X,
         model,
         model_onnx,
         allow_failure=
         "StrictVersion(onnx.__version__) < StrictVersion('1.0.0')",
         basename=prefix + "BoosterBin" + model.__class__.__name__)
    def _test_lgbm(self, X, model, extra_config={}):
        # Create ONNX-ML model
        onnx_ml_model = convert_model(
            model, 'lgbm-onnxml',
            [("input", FloatTensorType([X.shape[0], X.shape[1]]))])[0]

        # Create ONNX model
        onnx_model = convert_model(
            model,
            'lgbm-onnx',
            [("input", FloatTensorType([X.shape[0], X.shape[1]]))],
            without_onnx_ml=True)[0]

        try:
            from onnxruntime import InferenceSession
        except ImportError:
            # onnxruntime not installed (python 2.7)
            return

        # Get the predictions for the ONNX-ML model
        session = InferenceSession(onnx_ml_model.SerializeToString())
        output_names = [
            session.get_outputs()[i].name
            for i in range(len(session.get_outputs()))
        ]
        onnx_ml_pred = [[] for i in range(len(output_names))]
        inputs = {session.get_inputs()[0].name: X}
        pred = session.run(output_names, inputs)
        for i in range(len(output_names)):
            if output_names[i] == "label":
                onnx_ml_pred[1] = pred[i]
            else:
                onnx_ml_pred[0] = pred[i]

        # Get the predictions for the ONNX model
        session = InferenceSession(onnx_model.SerializeToString())
        onnx_pred = [[] for i in range(len(output_names))]
        pred = session.run(output_names, inputs)
        for i in range(len(output_names)):
            if output_names[i] == "label":
                onnx_pred[1] = pred[i]
            else:
                onnx_pred[0] = pred[i]

        return onnx_ml_pred, onnx_pred, output_names
 def test_lightgbm_classifier_zipmap(self):
     X = [[0, 1], [1, 1], [2, 0], [1, 2]]
     X = numpy.array(X, dtype=numpy.float32)
     y = [0, 1, 0, 1]
     model = LGBMClassifier(n_estimators=3, min_child_samples=1)
     model.fit(X, y)
     onx = convert_model(
         model, 'dummy', input_types=[('X', FloatTensorType([None, X.shape[1]]))])
     assert "zipmap" in str(onx).lower()
 def test_lightgbm_booster_classifier_zipmap(self):
     X = [[0, 1], [1, 1], [2, 0], [1, 2]]
     X = numpy.array(X, dtype=numpy.float32)
     y = [0, 1, 0, 1]
     data = lightgbm.Dataset(X, label=y)
     model = lightgbm.train({'boosting_type': 'gbdt', 'objective': 'binary',
                             'n_estimators': 3, 'min_child_samples': 1},
                            data)
     model_onnx, prefix = convert_model(model, 'tree-based classifier',
                                        [('input', FloatTensorType([None, 2]))])
     assert "zipmap" in str(model_onnx).lower()
     dump_data_and_model(X, model, model_onnx,
                         allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')",
                         basename=prefix + "BoosterBin" + model.__class__.__name__)