Note
Go to the end to download the full example code
Compares the conversions of the same model with different options#
The script compares two onnx models obtained with the same trained scikit-learn models but converted with different options.
A model#
from sklearn.mixture import GaussianMixture
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from skl2onnx import to_onnx
from onnx_array_api.reference import compare_onnx_execution
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot
data = load_iris()
X_train, X_test = train_test_split(data.data)
model = GaussianMixture()
model.fit(X_train)
Conversion to onnx#
opset: domain='' version=12
input: name='X' type=dtype('float64') shape=['', 4]
init: name='Ad_Addcst' type=dtype('float64') shape=(1,) -- array([7.35150827])
init: name='Ge_Gemmcst' type=dtype('float64') shape=(4, 4)
init: name='Ge_Gemmcst1' type=dtype('float64') shape=(4,) -- array([-7.05890183, -7.78812023, 5.41461403, 0.37703043])
init: name='Mu_Mulcst' type=dtype('float64') shape=(1,) -- array([-0.5])
init: name='Ad_Addcst1' type=dtype('float64') shape=(1,) -- array([3.20369698])
init: name='Ad_Addcst2' type=dtype('float64') shape=(1,) -- array([0.])
Gemm(X, Ge_Gemmcst, Ge_Gemmcst1, alpha=1.00, beta=1.00) -> Ge_Y0
ReduceSumSquare(Ge_Y0, axes=[1], keepdims=1) -> Re_reduced0
Concat(Re_reduced0, axis=1) -> Co_concat_result0
Add(Ad_Addcst, Co_concat_result0) -> Ad_C02
Mul(Ad_C02, Mu_Mulcst) -> Mu_C0
Add(Mu_C0, Ad_Addcst1) -> Ad_C01
Add(Ad_C01, Ad_Addcst2) -> Ad_C0
ArgMax(Ad_C0, axis=1) -> label
ReduceLogSumExp(Ad_C0, axes=[1], keepdims=1) -> score_samples
Sub(Ad_C0, score_samples) -> Su_C0
Exp(Su_C0) -> probabilities
output: name='label' type=dtype('int64') shape=['', 1]
output: name='probabilities' type=dtype('float64') shape=['', 1]
output: name='score_samples' type=dtype('float64') shape=['', 1]
Conversion to onnx without ReduceLogSumExp#
opset: domain='' version=12
input: name='X' type=dtype('float64') shape=['', 4]
init: name='Ad_Addcst' type=dtype('float64') shape=(1,) -- array([7.35150827])
init: name='Ge_Gemmcst' type=dtype('float64') shape=(4, 4)
init: name='Ge_Gemmcst1' type=dtype('float64') shape=(4,) -- array([-7.05890183, -7.78812023, 5.41461403, 0.37703043])
init: name='Mu_Mulcst' type=dtype('float64') shape=(1,) -- array([-0.5])
init: name='Ad_Addcst1' type=dtype('float64') shape=(1,) -- array([3.20369698])
init: name='Ad_Addcst2' type=dtype('float64') shape=(1,) -- array([0.])
Gemm(X, Ge_Gemmcst, Ge_Gemmcst1, alpha=1.00, beta=1.00) -> Ge_Y0
Mul(Ge_Y0, Ge_Y0) -> Mu_C01
ReduceSum(Mu_C01, axes=[1], keepdims=1) -> Re_reduced0
Concat(Re_reduced0, axis=1) -> Co_concat_result0
Add(Ad_Addcst, Co_concat_result0) -> Ad_C02
Mul(Ad_C02, Mu_Mulcst) -> Mu_C0
Add(Mu_C0, Ad_Addcst1) -> Ad_C01
Add(Ad_C01, Ad_Addcst2) -> Ad_C0
ArgMax(Ad_C0, axis=1) -> label
ReduceMax(Ad_C0, axes=[1], keepdims=1) -> Re_reduced03
Sub(Ad_C0, Re_reduced03) -> Su_C01
Exp(Su_C01) -> Ex_output0
ReduceSum(Ex_output0, axes=[1], keepdims=1) -> Re_reduced02
Log(Re_reduced02) -> Lo_output0
Add(Lo_output0, Re_reduced03) -> score_samples
Sub(Ad_C0, score_samples) -> Su_C0
Exp(Su_C0) -> probabilities
output: name='label' type=dtype('int64') shape=['', 1]
output: name='probabilities' type=dtype('float64') shape=['', 1]
output: name='score_samples' type=dtype('float64') shape=['', 1]
Differences#
Function onnx_array_api.reference.compare_onnx_execution()
compares the intermediate results of two onnx models. Then it finds
the best alignmet between the two models using an edit distance.
[compare_onnx_execution] generate inputs
[compare_onnx_execution] execute with 1 inputs
[compare_onnx_execution] execute first model
[compare_onnx_execution] got 21 results
[compare_onnx_execution] execute second model
[compare_onnx_execution] got 27 results
[compare_onnx_execution] compute edit distance
[compare_onnx_execution] got 27 pairs
[compare_onnx_execution] done
------------
001 = | INITIA float64 1:1 HAAA Ad | INITIA float64 1:1 HAAA Ad
002 = | INITIA float64 2:4x4 ACZF Ge | INITIA float64 2:4x4 ACZF Ge
003 = | INITIA float64 1:4 TTFA Ge | INITIA float64 1:4 TTFA Ge
004 = | INITIA float64 1:1 AAAA Mu | INITIA float64 1:1 AAAA Mu
005 = | INITIA float64 1:1 DAAA Ad | INITIA float64 1:1 DAAA Ad
006 = | INITIA float64 1:1 AAAA Ad | INITIA float64 1:1 AAAA Ad
007 = | INPUT float64 2:1x4 AAAA X | INPUT float64 2:1x4 AAAA X
008 = | RESULT float64 2:1x4 TTGC Gemm Ge | RESULT float64 2:1x4 TTGC Gemm Ge
009 + | | RESULT float64 2:1x4 XZSG Mul Mu
010 ~ | RESULT float64 2:1x1 VAAA ReduceSumSquare Re | RESULT float64 2:1x1 VAAA ReduceSum Re
011 = | RESULT float64 2:1x1 VAAA Concat Co | RESULT float64 2:1x1 VAAA Concat Co
012 = | RESULT float64 2:1x1 DAAA Add Ad | RESULT float64 2:1x1 DAAA Add Ad
013 = | RESULT float64 2:1x1 ZAAA Mul Mu | RESULT float64 2:1x1 ZAAA Mul Mu
014 = | RESULT float64 2:1x1 CAAA Add Ad | RESULT float64 2:1x1 CAAA Add Ad
015 = | RESULT float64 2:1x1 CAAA Add Ad | RESULT float64 2:1x1 CAAA Add Ad
016 = | RESULT int64 2:1x1 AAAA ArgMax la | RESULT int64 2:1x1 AAAA ArgMax la
017 + | | RESULT float64 2:1x1 CAAA ReduceMax Re
018 + | | RESULT float64 2:1x1 AAAA Sub Su
019 + | | RESULT float64 2:1x1 BAAA Exp Ex
020 + | | RESULT float64 2:1x1 BAAA ReduceSum Re
021 + | | RESULT float64 2:1x1 AAAA Log Lo
022 ~ | RESULT float64 2:1x1 CAAA ReduceLogSumExp sc | RESULT float64 2:1x1 CAAA Add sc
023 = | RESULT float64 2:1x1 AAAA Sub Su | RESULT float64 2:1x1 AAAA Sub Su
024 = | RESULT float64 2:1x1 BAAA Exp pr | RESULT float64 2:1x1 BAAA Exp pr
025 = | OUTPUT int64 2:1x1 AAAA la | OUTPUT int64 2:1x1 AAAA la
026 = | OUTPUT float64 2:1x1 BAAA pr | OUTPUT float64 2:1x1 BAAA pr
027 = | OUTPUT float64 2:1x1 CAAA sc | OUTPUT float64 2:1x1 CAAA sc
See onnx_array_api.reference.compare_onnx_execution for a better view. The display shows that ReduceSumSquare was replaced by Mul + ReduceSum, and ReduceLogSumExp by ReduceMax + Sub + Exp + Log + Add.
Total running time of the script: (0 minutes 1.189 seconds)