Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 
========
Tutorial
========
 
*ONNX Runtime* provides an easy way to run
machine learned models with high performance on CPU or GPU
without dependencies on the training framework.
Machine learning frameworks are usually optimized for
batch training rather than for prediction, which is a
more common scenario in applications, sites, and services.
At a high level, you can:
 
1. Train a model using your favorite framework.
2. Convert or export the model into ONNX format.
   See `ONNX Tutorials <https://github.com/onnx/tutorials>`_
   for more details.
3. Load and run the model using *ONNX Runtime*.
 
In this tutorial, we will briefly create a
pipeline with *scikit-learn*, convert it into
ONNX format and run the first predictions.
 
.. _l-logreg-example:
 
Step 1: Train a model using your favorite framework
+++++++++++++++++++++++++++++++++++++++++++++++++++
 
We'll use the famous iris datasets.
 
.. code-block:: python
 
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    iris = load_iris()
    X, y = iris.data, iris.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
 
    from sklearn.linear_model import LogisticRegression
    clr = LogisticRegression(max_iter=200, random_state=42)
    clr.fit(X_train, y_train)
 
Step 2: Convert or export the model into ONNX format
++++++++++++++++++++++++++++++++++++++++++++++++++++
 
`ONNX <https://github.com/onnx/onnx>`_ is a format to describe
the machine learned model.
It defines a set of commonly used operators to compose models.
There are `tools <https://github.com/onnx/tutorials>`_
to convert other model formats into ONNX. Here we will use
`ONNXMLTools <https://github.com/onnx/onnxmltools>`_.
 
.. code-block:: python
 
    from skl2onnx import convert_sklearn
    from skl2onnx.common.data_types import FloatTensorType
 
    initial_type = [('float_input', FloatTensorType([None, 4]))]
    onx = convert_sklearn(clr, initial_types=initial_type)
    with open("logreg_iris.onnx", "wb") as f:
        f.write(onx.SerializeToString())
 
Step 3: Load and run the model using ONNX Runtime
+++++++++++++++++++++++++++++++++++++++++++++++++
 
We will use *ONNX Runtime* to compute the predictions
for this machine learning model.
 
.. exec_code::
 
    # hide: start
    from sklearn.datasets import load_iris
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    from skl2onnx import convert_sklearn
    from skl2onnx.common.data_types import FloatTensorType
 
    iris = load_iris()
    X, y = iris.data, iris.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    clr = LogisticRegression(max_iter=200, random_state=42)
    clr.fit(X_train, y_train)
    initial_type = [('float_input', FloatTensorType([None, 4]))]
    onx = convert_sklearn(clr, initial_types=initial_type)
    with open("logreg_iris.onnx", "wb") as f:
        f.write(onx.SerializeToString())
 
    # Force GetPciBusId errors to be piped to null. When generating the logs
    # This is because we get some output during initialization in the Sphinx generator
    import os
    _stderr_fd = os.dup(2)
    _devnull = open(os.devnull, "w")
    os.dup2(_devnull.fileno(), 2)
    # hide: stop
 
    import numpy
    import onnxruntime as rt
 
    # hide: start
    os.dup2(_stderr_fd, 2)
    os.close(_stderr_fd)
    _devnull.close()
    # hide: stop
 
    sess = rt.InferenceSession("logreg_iris.onnx", providers=["CPUExecutionProvider"])
    input_name = sess.get_inputs()[0].name
    pred_onx = sess.run(None, {input_name: X_test.astype(numpy.float32)})[0]
    print(pred_onx)
 
The code can be changed to get one specific output
by specifying its name into a list.
 
.. exec_code::
 
    # hide: start
    from sklearn.datasets import load_iris
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    from skl2onnx import convert_sklearn
    from skl2onnx.common.data_types import FloatTensorType
 
    iris = load_iris()
    X, y = iris.data, iris.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    clr = LogisticRegression(max_iter=200, random_state=42)
    clr.fit(X_train, y_train)
    initial_type = [('float_input', FloatTensorType([None, 4]))]
    onx = convert_sklearn(clr, initial_types=initial_type)
    with open("logreg_iris.onnx", "wb") as f:
        f.write(onx.SerializeToString())
 
    # Force GetPciBusId errors to be piped to null. When generating the logs
    # This is because we get some output during initialization in the Sphinx generator
    import os
    _stderr_fd = os.dup(2)
    _devnull = open(os.devnull, "w")
    os.dup2(_devnull.fileno(), 2)
    # hide: stop
 
    import numpy
    import onnxruntime as rt
 
    # hide: start
    os.dup2(_stderr_fd, 2)
    os.close(_stderr_fd)
    _devnull.close()
    # hide: stop
 
    sess = rt.InferenceSession("logreg_iris.onnx", providers=["CPUExecutionProvider"])
    input_name = sess.get_inputs()[0].name
    label_name = sess.get_outputs()[0].name
    pred_onx = sess.run([label_name], {input_name: X_test.astype(numpy.float32)})[0]
    print(pred_onx)