-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: xadupre <xadupre@microsoft.com>
- Loading branch information
Showing
471 changed files
with
105,599 additions
and
126,492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: 62f8ee6a3f6aefe7ce3ae4611d664fc1 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: ee4940ae2593c690ef5ae868e708b4af | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
286 changes: 143 additions & 143 deletions
286
_downloads/01727087b155e5345657ebbe183f11e3/plot_gbegin_cst.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n# Store arrays in one onnx graph\n\nOnce a model is converted it can be useful to store an\narray as a constant in the graph an retrieve it through\nan output. This allows the user to store training parameters\nor other informations like a vocabulary.\nLast sections shows how to remove an output or to promote\nan intermediate result to an output.\n\n## Train and convert a model\n\nWe download one model from the :epkg:`ONNX Zoo` but the model\ncould be trained and produced by another converter library.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import pprint\nimport numpy\nfrom onnx import load\nfrom onnxruntime import InferenceSession\nfrom sklearn.datasets import load_iris\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom skl2onnx import to_onnx\nfrom skl2onnx.helpers.onnx_helper import (\n add_output_initializer, select_model_inputs_outputs)\n\n\ndata = load_iris()\nX, y = data.data.astype(numpy.float32), data.target\nX_train, X_test, y_train, y_test = train_test_split(X, y)\nmodel = LogisticRegression(penalty='elasticnet', C=2.,\n solver='saga', l1_ratio=0.5)\nmodel.fit(X_train, y_train)\n\nonx = to_onnx(model, X_train[:1], target_opset=12,\n options={'zipmap': False})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Add training parameter\n\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"new_onx = add_output_initializer(\n onx,\n ['C', 'l1_ratio'],\n [numpy.array([model.C]), numpy.array([model.l1_ratio])])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Inference\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"sess = InferenceSession(new_onx.SerializeToString())\nprint(\"output names:\", [o.name for o in sess.get_outputs()])\nres = sess.run(None, {'X': X_test[:2]})\nprint(\"outputs\")\npprint.pprint(res)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The major draw back of this solution is increase the prediction\ntime as onnxruntime copies the constants for every prediction.\nIt is possible either to store those constant in a separate ONNX graph\nor to removes them.\n\n## Select outputs\n\nNext function removes unneeded outputs from a model,\nnot only the constants. Next model only keeps the probabilities.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"simple_onx = select_model_inputs_outputs(new_onx, ['probabilities'])\n\nsess = InferenceSession(simple_onx.SerializeToString())\nprint(\"output names:\", [o.name for o in sess.get_outputs()])\nres = sess.run(None, {'X': X_test[:2]})\nprint(\"outputs\")\npprint.pprint(res)\n\n# Function *select_model_inputs_outputs* add also promote an intermediate\n# result to an output.\n#" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This example only uses ONNX graph in memory and never saves or loads a\nmodel. This can be done by using the following snippets of code.\n\n## Save a model\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"with open(\"simplified_model.onnx\", \"wb\") as f:\n f.write(simple_onx.SerializeToString())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Load a model\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = load(\"simplified_model.onnx\", \"wb\")\n\nsess = InferenceSession(model.SerializeToString())\nprint(\"output names:\", [o.name for o in sess.get_outputs()])\nres = sess.run(None, {'X': X_test[:2]})\nprint(\"outputs\")\npprint.pprint(res)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n# Store arrays in one onnx graph\n\nOnce a model is converted it can be useful to store an\narray as a constant in the graph an retrieve it through\nan output. This allows the user to store training parameters\nor other informations like a vocabulary.\nLast sections shows how to remove an output or to promote\nan intermediate result to an output.\n\n## Train and convert a model\n\nWe download one model from the :epkg:`ONNX Zoo` but the model\ncould be trained and produced by another converter library.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import pprint\nimport numpy\nfrom onnx import load\nfrom onnxruntime import InferenceSession\nfrom sklearn.datasets import load_iris\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom skl2onnx import to_onnx\nfrom skl2onnx.helpers.onnx_helper import (\n add_output_initializer, select_model_inputs_outputs)\n\n\ndata = load_iris()\nX, y = data.data.astype(numpy.float32), data.target\nX_train, X_test, y_train, y_test = train_test_split(X, y)\nmodel = LogisticRegression(penalty='elasticnet', C=2.,\n solver='saga', l1_ratio=0.5)\nmodel.fit(X_train, y_train)\n\nonx = to_onnx(model, X_train[:1], target_opset=12,\n options={'zipmap': False})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Add training parameter\n\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"new_onx = add_output_initializer(\n onx,\n ['C', 'l1_ratio'],\n [numpy.array([model.C]), numpy.array([model.l1_ratio])])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Inference\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"sess = InferenceSession(new_onx.SerializeToString())\nprint(\"output names:\", [o.name for o in sess.get_outputs()])\nres = sess.run(None, {'X': X_test[:2]})\nprint(\"outputs\")\npprint.pprint(res)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The major draw back of this solution is increase the prediction\ntime as onnxruntime copies the constants for every prediction.\nIt is possible either to store those constant in a separate ONNX graph\nor to removes them.\n\n## Select outputs\n\nNext function removes unneeded outputs from a model,\nnot only the constants. Next model only keeps the probabilities.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"simple_onx = select_model_inputs_outputs(new_onx, ['probabilities'])\n\nsess = InferenceSession(simple_onx.SerializeToString())\nprint(\"output names:\", [o.name for o in sess.get_outputs()])\nres = sess.run(None, {'X': X_test[:2]})\nprint(\"outputs\")\npprint.pprint(res)\n\n# Function *select_model_inputs_outputs* add also promote an intermediate\n# result to an output.\n#" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This example only uses ONNX graph in memory and never saves or loads a\nmodel. This can be done by using the following snippets of code.\n\n## Save a model\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"with open(\"simplified_model.onnx\", \"wb\") as f:\n f.write(simple_onx.SerializeToString())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Load a model\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = load(\"simplified_model.onnx\", \"wb\")\n\nsess = InferenceSession(model.SerializeToString())\nprint(\"output names:\", [o.name for o in sess.get_outputs()])\nres = sess.run(None, {'X': X_test[:2]})\nprint(\"outputs\")\npprint.pprint(res)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.