def test_transfer_transformer(self): _register_converters_mlinsights(True) iris = load_iris() X, y = iris.data, iris.target pipe = TransferTransformer(StandardScaler(), trainable=True) pipe.fit(X, y) model_def = to_onnx(pipe, X[:1].astype(numpy.float32)) sess = OnnxInference(model_def) res = sess.run({'X': X.astype(numpy.float32)}) exp = pipe.transform(X.astype(numpy.float32)) self.assertEqualArray(exp, res['variable'], decimal=5)
def test_transfer_logistic_regression(self): _register_converters_mlinsights(True) iris = load_iris() X, y = iris.data, iris.target pipe = TransferTransformer(LogisticRegression(solver='liblinear'), trainable=True) pipe.fit(X, y) model_def = to_onnx(pipe, X[:1]) sess = OnnxInference(model_def) res = sess.run({'X': X}) exp = pipe.transform(X) self.assertEqualArray(exp, res['probabilities'], decimal=5)
def test_transfer_transformer_pickle(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) Y = numpy.array([1., 1.1, 1.2, 10, 1.4]) norm = StandardScaler() norm.fit(X) X2 = norm.transform(X) clr = LinearRegression() clr.fit(X2, Y) pipe = make_pipeline(TransferTransformer(norm), TransferTransformer(clr)) pipe.fit(X) test_sklearn_pickle(lambda: pipe, X, Y)
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))
def test_transfer_transformer_diff_trainable(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) Y = numpy.array([1., 1.1, 1.2, 10, 1.4]) norm = StandardScaler() norm.fit(X) X2 = norm.transform(X) clr = LinearRegression() clr.fit(X2, Y) exp = clr.predict(X2) pipe = make_pipeline(TransferTransformer(norm, trainable=True), TransferTransformer(clr, trainable=True)) pipe.fit(X, Y) got = pipe.transform(X) self.assertEqual(exp, got)
def test_transfer_transformer_logreg(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) Y = numpy.array([0, 0, 1, 1, 1]) norm = StandardScaler() norm.fit(X) X2 = norm.transform(X) clr = LogisticRegression() clr.fit(X2, Y) exp = clr.predict_proba(X2) pipe = make_pipeline(TransferTransformer(norm), TransferTransformer(clr)) pipe.fit(X) got = pipe.transform(X) self.assertEqual(exp, got)
def test_transfer_transformer_decision_function(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) Y = numpy.array([0, 0, 1, 1, 1]) norm = StandardScaler() norm.fit(X) X2 = norm.transform(X) clr = LogisticRegression() clr.fit(X2, Y) exp = clr.decision_function(X2) pipe = make_pipeline( TransferTransformer(norm), TransferTransformer(clr, method="decision_function")) pipe.fit(X) got = pipe.transform(X) self.assertEqual(exp, got)
def test_transfer_transformer_sample_weight(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) Y = numpy.array([1., 1.1, 1.2, 10, 1.4]) sw = numpy.array([1, 1, 1.5, 1.5, 1.]) norm = StandardScaler() norm.fit(X) X2 = norm.transform(X) clr = LinearRegression() clr.fit(X2, Y) exp = clr.predict(X2) pipe = Pipeline(steps=[ ('scaler', TransferTransformer(norm)), ('model', TransferTransformer(clr))]) pipe.fit(X, model__sample_weight=sw) got = pipe.transform(X) self.assertEqual(exp, got)
def test_transfer_transformer_cloned0(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) norm = StandardScaler() norm.fit(X) tr1 = TransferTransformer(norm, copy_estimator=True) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=False) tr1.fit(X) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=False) tr1 = TransferTransformer(norm, copy_estimator=True) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True) tr1.fit(X) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True)
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))
def test_transfer_transformer_clone(self): X = numpy.array([[0.1], [0.2], [0.3], [0.4], [0.5]]) Y = numpy.array([1., 1.1, 1.2, 10, 1.4]) norm = StandardScaler() norm.fit(X) X2 = norm.transform(X) clr = LinearRegression() clr.fit(X2, Y) tr1 = TransferTransformer(norm, copy_estimator=False) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True) tr1.fit(X) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True) tr1 = TransferTransformer(norm, copy_estimator=True) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True) tr1.fit(X) test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True) tr1 = TransferTransformer(norm, copy_estimator=True) tr2 = TransferTransformer(clr, copy_estimator=True) pipe = make_pipeline(tr1, tr2) pipe.fit(X) self.maxDiff = None test_sklearn_clone(lambda: tr1, ext=self, copy_fitted=True) test_sklearn_clone(lambda: tr2, ext=self, copy_fitted=True) test_sklearn_clone(lambda: pipe, ext=self, copy_fitted=True)