Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
onnx-array-api 0.3.4 documentation
Logo
onnx-array-api 0.3.4 documentation

Contents

More

Back to top

Note

Go to the endto 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 trainedscikit-learn models but converted with different options.

A model

fromsklearn.mixtureimportGaussianMixturefromsklearn.datasetsimportload_irisfromsklearn.model_selectionimporttrain_test_splitfromskl2onnximportto_onnxfromonnx_array_api.referenceimportcompare_onnx_executionfromonnx_array_api.plotting.text_plotimportonnx_simple_text_plotdata=load_iris()X_train,X_test=train_test_split(data.data)model=GaussianMixture()model.fit(X_train)
GaussianMixture()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Conversion to onnx

onx=to_onnx(model,X_train[:1],options={id(model):{"score_samples":True}},target_opset=12)print(onnx_simple_text_plot(onx))
opset: domain='' version=12input: name='X' type=dtype('float64') shape=['', 4]init: name='Ad_Addcst' type=float64 shape=(1,) -- array([7.35150827])init: name='Ge_Gemmcst' type=float64 shape=(4, 4)init: name='Ge_Gemmcst1' type=float64 shape=(4,) -- array([-7.05659095, -6.75209331,  3.74519126,  0.83306511])init: name='Mu_Mulcst' type=float64 shape=(1,) -- array([-0.5])init: name='Ad_Addcst1' type=float64 shape=(1,) -- array([3.08489761])init: name='Ad_Addcst2' type=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) -> probabilitiesoutput: 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

onx2=to_onnx(model,X_train[:1],options={id(model):{"score_samples":True}},black_op={"ReduceLogSumExp"},target_opset=12,)print(onnx_simple_text_plot(onx2))
opset: domain='' version=12input: name='X' type=dtype('float64') shape=['', 4]init: name='Ad_Addcst' type=float64 shape=(1,) -- array([7.35150827])init: name='Ge_Gemmcst' type=float64 shape=(4, 4)init: name='Ge_Gemmcst1' type=float64 shape=(4,) -- array([-7.05659095, -6.75209331,  3.74519126,  0.83306511])init: name='Mu_Mulcst' type=float64 shape=(1,) -- array([-0.5])init: name='Ad_Addcst1' type=float64 shape=(1,) -- array([3.08489761])init: name='Ad_Addcst2' type=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) -> probabilitiesoutput: 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

Functiononnx_array_api.reference.compare_onnx_execution()compares the intermediate results of two onnx models. Then it findsthe best alignmet between the two models using an edit distance.

res1,res2,align,dc=compare_onnx_execution(onx,onx2,verbose=1)print("------------")text=dc.to_str(res1,res2,align)print(text)
[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 21 results (first model)[compare_onnx_execution] got 27 results (second model)[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                 Ad002 = | INITIA float64  2:4x4                ADZF                 Ge | INITIA float64  2:4x4                ADZF                 Ge003 = | INITIA float64  1:4                  TUDA                 Ge | INITIA float64  1:4                  TUDA                 Ge004 = | INITIA float64  1:1                  AAAA                 Mu | INITIA float64  1:1                  AAAA                 Mu005 = | INITIA float64  1:1                  DAAA                 Ad | INITIA float64  1:1                  DAAA                 Ad006 = | INITIA float64  1:1                  AAAA                 Ad | INITIA float64  1:1                  AAAA                 Ad007 = | INPUT  float64  2:1x4                AAAA                 X  | INPUT  float64  2:1x4                AAAA                 X008 = | RESULT float64  2:1x4                TUFD Gemm            Ge | RESULT float64  2:1x4                TUFD Gemm            Ge009 + |                                                              | RESULT float64  2:1x4                XMZJ Mul             Mu010 ~ | RESULT float64  2:1x1                TAAA ReduceSumSquare Re | RESULT float64  2:1x1                TAAA ReduceSum       Re011 = | RESULT float64  2:1x1                TAAA Concat          Co | RESULT float64  2:1x1                TAAA Concat          Co012 = | RESULT float64  2:1x1                AAAA Add             Ad | RESULT float64  2:1x1                AAAA Add             Ad013 = | RESULT float64  2:1x1                NAAA Mul             Mu | RESULT float64  2:1x1                NAAA Mul             Mu014 = | RESULT float64  2:1x1                QAAA Add             Ad | RESULT float64  2:1x1                QAAA Add             Ad015 = | RESULT float64  2:1x1                QAAA Add             Ad | RESULT float64  2:1x1                QAAA Add             Ad016 = | RESULT int64    2:1x1                AAAA ArgMax          la | RESULT int64    2:1x1                AAAA ArgMax          la017 + |                                                              | RESULT float64  2:1x1                QAAA ReduceMax       Re018 + |                                                              | RESULT float64  2:1x1                AAAA Sub             Su019 + |                                                              | RESULT float64  2:1x1                BAAA Exp             Ex020 + |                                                              | RESULT float64  2:1x1                BAAA ReduceSum       Re021 + |                                                              | RESULT float64  2:1x1                AAAA Log             Lo022 ~ | RESULT float64  2:1x1                QAAA ReduceLogSumExp sc | RESULT float64  2:1x1                QAAA Add             sc023 = | RESULT float64  2:1x1                AAAA Sub             Su | RESULT float64  2:1x1                AAAA Sub             Su024 = | RESULT float64  2:1x1                BAAA Exp             pr | RESULT float64  2:1x1                BAAA Exp             pr025 = | OUTPUT int64    2:1x1                AAAA                 la | OUTPUT int64    2:1x1                AAAA                 la026 = | OUTPUT float64  2:1x1                BAAA                 pr | OUTPUT float64  2:1x1                BAAA                 pr027 = | OUTPUT float64  2:1x1                QAAA                 sc | OUTPUT float64  2:1x1                QAAA                 sc

Seeonnx_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 2.984 seconds)

Gallery generated by Sphinx-Gallery

On this page

[8]ページ先頭

©2009-2026 Movatter.jp