.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gyexamples/plot_bench_onnxruntime_logistic_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gyexamples_plot_bench_onnxruntime_logistic_regression.py: .. _l-example-onnxruntime-logreg: Benchmark of onnxruntime on LogisticRegression ============================================== The example uses what :epkg:`pymlbenchmark` implements, in particular class :class:`OnnxRuntimeBenchPerfTestBinaryClassification ` which defines a side-by-side benchmark to compare the prediction function between :epkg:`scikit-learn`, :epkg:`onnxruntime` and a simple :epkg:`numpy` implementation. .. contents:: :local: Benchmark function ++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 22-91 .. code-block:: default from time import perf_counter as time import numpy import pandas import matplotlib.pyplot as plt import sklearn from sklearn.linear_model import LogisticRegression try: from sklearn.utils._testing import ignore_warnings except ImportError: from sklearn.utils.testing import ignore_warnings from scipy.special import expit from pymlbenchmark.context import machine_information from pymlbenchmark.benchmark import BenchPerf from pymlbenchmark.external import OnnxRuntimeBenchPerfTestBinaryClassification from pymlbenchmark.plotting import plot_bench_results class OnnxRuntimeBenchPerfTestBinaryClassification3( OnnxRuntimeBenchPerfTestBinaryClassification): """ Overwrites the class to add a pure python implementation of the logistic regression. """ def fcts(self, dim=None, **kwargs): def predict_py_predict(X, model=self.skl): coef = model.coef_ intercept = model.intercept_ pred = numpy.dot(X, coef.T) + intercept return (pred >= 0).astype(numpy.int32) def predict_py_predict_proba(X, model=self.skl): coef = model.coef_ intercept = model.intercept_ pred = numpy.dot(X, coef.T) + intercept decision_2d = numpy.c_[-pred, pred] return expit(decision_2d) res = OnnxRuntimeBenchPerfTestBinaryClassification.fcts( self, dim=dim, **kwargs) res.extend([ {'method': 'predict', 'lib': 'py', 'fct': predict_py_predict}, {'method': 'predict_proba', 'lib': 'py', 'fct': predict_py_predict_proba}, ]) return res @ignore_warnings(category=FutureWarning) def run_bench(repeat=100, verbose=False): pbefore = dict(dim=[1, 5, 10, 20, 50, 100, 150], fit_intercept=[True, False]) pafter = dict(N=[1, 10, 100, 1000, 10000]) test = lambda dim=None, **opts: ( OnnxRuntimeBenchPerfTestBinaryClassification3( LogisticRegression, dim=dim, **opts)) bp = BenchPerf(pbefore, pafter, test) with sklearn.config_context(assume_finite=True): start = time() results = list(bp.enumerate_run_benchs(repeat=repeat, verbose=verbose)) end = time() results_df = pandas.DataFrame(results) print("Total time = %0.3f sec\n" % (end - start)) return results_df .. GENERATED FROM PYTHON SOURCE LINES 92-94 Run the benchmark +++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 94-100 .. code-block:: default df = run_bench(verbose=True) df.to_csv("bench_plot_onnxruntime_logistic_regression.perf.csv", index=False) print(df.head(n=4).T) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/70 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bench_onnxruntime_logistic_regression.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_