示例#1
0
    def test_pipeline_iris(self):
        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline([('pca', PCA(n_components=2)),
                             ('no', StandardScaler()),
                             ('lr', LogisticRegression())],
                            enforce_float32=True,
                            op_version=TARGET_OPSET)
        pipe.fit(X, y)
        pipe.fit(X, y)
        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 3)
        self.assertEqual(len(pipe.raw_steps_), 3)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)
        self.assertIsInstance(pipe.steps[1][1], OnnxTransformer)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqualArray(res["label"], pipe.predict(X))
        self.assertEqualArray(res["probabilities"], pipe.predict_proba(X))
示例#2
0
    def test_pipeline_pickable(self):
        _register_converters_mlinsights(True)
        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline(
            [('gm', TransferTransformer(StandardScaler(), trainable=True)),
             ('lr', LogisticRegression())],
            enforce_float32=True,
            op_version=TARGET_OPSET)
        pipe.fit(X, y)
        pipe.fit(X, y)

        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 2)
        self.assertEqual(len(pipe.raw_steps_), 2)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqual(list(sorted(res)), ['label', 'probabilities'])
        self.assertEqualArray(res["label"], pipe.predict(X))
        self.assertEqualArray(res["probabilities"], pipe.predict_proba(X))
示例#3
0
    def test_pipeline_pickable_options(self):
        _register_converters_mlinsights(True)
        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline([('gm',
                              TransferTransformer(GaussianMixture(
                                  n_components=5, random_state=2),
                                                  trainable=True,
                                                  method='predict_proba')),
                             ('lr', LogisticRegression(random_state=2))],
                            enforce_float32=True,
                            op_version=TARGET_OPSET,
                            options={
                                'gm__score_samples': True,
                                'lr__zipmap': False
                            })
        pipe.fit(X, y)
        pipe.fit(X, y)

        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 2)
        self.assertEqual(len(pipe.raw_steps_), 2)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def, runtime="python_compiled")
        self.assertIn("'probabilities': probabilities,", str(sess))
        sess = InferenceSession(model_def.SerializeToString())
        r = sess.run(None, {'X': X})
        self.assertEqual(len(r), 2)
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqual(list(sorted(res)), ['label', 'probabilities'])
        self.assertEqualArray(res["probabilities"], pipe.predict_proba(X))
        self.assertEqualArray(res["label"], pipe.predict(X))
示例#4
0
    def test_pipeline_iris_column_transformer_nocache(self):
        class MyMemory:
            def __init__(self):
                pass

            def cache(self, obj):
                return obj

        iris = load_iris()
        X, y = iris.data, iris.target
        pipe = OnnxPipeline(
            [('col',
              ColumnTransformer([('pca', PCA(n_components=2), [0, 1]),
                                 ('no', StandardScaler(), [2]),
                                 ('pass', 'passthrough', [3])])),
             ('lr', LogisticRegression())],
            enforce_float32=True,
            op_version=TARGET_OPSET,
            memory=MyMemory())
        pipe.fit(X, y)
        pipe.fit(X, y)
        self.assertTrue(hasattr(pipe, 'raw_steps_'))
        self.assertEqual(len(pipe.steps), 2)
        self.assertEqual(len(pipe.raw_steps_), 2)
        self.assertIsInstance(pipe.steps[0][1], OnnxTransformer)
        self.assertIsInstance(pipe.steps[1][1], LogisticRegression)

        X = X.astype(numpy.float32)
        model_def = to_onnx(pipe,
                            X[:1],
                            target_opset=pipe.op_version,
                            options={id(pipe): {
                                         'zipmap': False
                                     }})
        sess = OnnxInference(model_def)
        res = sess.run({'X': X})
        self.assertEqualArray(res["label"], pipe.predict(X))
        self.assertEqualArray(res["probabilities"],
                              pipe.predict_proba(X),
                              decimal=5)