plotting#

Dot#

onnx_array_api.plotting.dot_plot.to_dot(proto: ModelProto, recursive: bool = False, prefix: str = '', use_onnx: bool = False, add_functions: bool = True, rt_shapes: Dict[str, Tuple[int, ...]] | None = None, **params) str[source]#

Produces a DOT language string for the graph.

Parameters:
  • params – additional params to draw the graph

  • recursive – also show subgraphs inside operator like Scan

  • prefix – prefix for every node name

  • use_onnx – use onnx dot format instead of this one

  • add_functions – add functions to the graph

  • rt_shapes – indicates shapes obtained from the execution or inference

Returns:

string

Default options for the graph are:

options = {
    'orientation': 'portrait',
    'ranksep': '0.25',
    'nodesep': '0.05',
    'width': '0.5',
    'height': '0.1',
    'size': '7',
}

One example:

<<<

import numpy as np  # B
from onnx_array_api.npx import absolute, jit_onnx
from onnx_array_api.plotting.dot_plot import to_dot


def l1_loss(x, y):
    return absolute(x - y).sum()


def l2_loss(x, y):
    return ((x - y) ** 2).sum()


def myloss(x, y):
    return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])


jitted_myloss = jit_onnx(myloss)

x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)
res = jitted_myloss(x, y)
print(res)

>>>

    [0.042]

Text#

onnx_array_api.plotting.text_plot.onnx_text_plot_tree(node)[source]#

Gives a textual representation of a tree ensemble.

Parameters:

nodeTreeEnsemble*

Returns:

text

<<<

import numpy
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeRegressor
from skl2onnx import to_onnx
from onnx_array_api.plotting.text_plot import onnx_text_plot_tree

iris = load_iris()
X, y = iris.data.astype(numpy.float32), iris.target
clr = DecisionTreeRegressor(max_depth=3)
clr.fit(X, y)
onx = to_onnx(clr, X)
res = onnx_text_plot_tree(onx.graph.node[0])
print(res)

>>>

    n_targets=1
    n_trees=1
    ----
    treeid=0
    n X3 <= 0.8
       -n X3 <= 1.75
          -n X2 <= 4.85
             -f 0:2
             +f 0:1.67
          +n X2 <= 4.95
             -f 0:1.67
             +f 0:1.02
       +f 0:0
onnx_array_api.plotting.text_plot.onnx_text_plot_io(model, verbose=False, att_display=None)[source]#

Displays information about input and output types.

Parameters:
  • model – ONNX graph

  • verbose – display debugging information

Returns:

str

An ONNX graph is printed the following way:

<<<

import numpy
from sklearn.cluster import KMeans
from skl2onnx import to_onnx
from onnx_array_api.plotting.text_plot import onnx_text_plot_io

x = numpy.random.randn(10, 3)
y = numpy.random.randn(10)
model = KMeans(3)
model.fit(x, y)
onx = to_onnx(model, x.astype(numpy.float32),
              target_opset=15)
text = onnx_text_plot_io(onx, verbose=False)
print(text)

>>>

    opset: domain='' version=14
    input: name='X' type=dtype('float32') shape=['', 3]
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,)
    init: name='Ge_Gemmcst' type=dtype('float32') shape=(3, 3)
    init: name='Mu_Mulcst' type=dtype('float32') shape=(1,)
    output: name='label' type=dtype('int64') shape=['']
    output: name='scores' type=dtype('float32') shape=['', 3]
onnx_array_api.plotting.text_plot.onnx_simple_text_plot(model, verbose=False, att_display=None, add_links=False, recursive=False, functions=True, raise_exc=True, sub_graphs_names=None, level=1, indent=True)[source]#

Displays an ONNX graph into text.

Parameters:
  • model – ONNX graph

  • verbose – display debugging information

  • att_display – list of attributes to display, if None, a default list if used

  • add_links – displays links of the right side

  • recursive – display subgraphs as well

  • functions – display functions as well

  • raise_exc – raises an exception if the model is not valid, otherwise tries to continue

  • sub_graphs_names – list of sub-graphs names

  • level – sub-graph level

  • indent – use indentation or not

Returns:

str

An ONNX graph is printed the following way:

<<<

import numpy
from sklearn.cluster import KMeans
from skl2onnx import to_onnx
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

x = numpy.random.randn(10, 3)
y = numpy.random.randn(10)
model = KMeans(3)
model.fit(x, y)
onx = to_onnx(model, x.astype(numpy.float32),
              target_opset=15)
text = onnx_simple_text_plot(onx, verbose=False)
print(text)

>>>

    opset: domain='' version=14
    input: name='X' type=dtype('float32') shape=['', 3]
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,) -- array([1.54 , 4.218, 2.122], dtype=float32)
    init: name='Ge_Gemmcst' type=dtype('float32') shape=(3, 3)
    init: name='Mu_Mulcst' type=dtype('float32') shape=(1,) -- array([0.], dtype=float32)
    ReduceSumSquare(X, axes=[1], keepdims=1) -> Re_reduced0
      Mul(Re_reduced0, Mu_Mulcst) -> Mu_C0
        Gemm(X, Ge_Gemmcst, Mu_C0, alpha=-2.00, transB=1) -> Ge_Y0
      Add(Re_reduced0, Ge_Y0) -> Ad_C01
        Add(Ad_Addcst, Ad_C01) -> Ad_C0
          ArgMin(Ad_C0, axis=1, keepdims=0) -> label
          Sqrt(Ad_C0) -> scores
    output: name='label' type=dtype('int64') shape=['']
    output: name='scores' type=dtype('float32') shape=['', 3]

The same graphs with links.

<<<

import numpy
from sklearn.cluster import KMeans
from skl2onnx import to_onnx
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

x = numpy.random.randn(10, 3)
y = numpy.random.randn(10)
model = KMeans(3)
model.fit(x, y)
onx = to_onnx(model, x.astype(numpy.float32),
              target_opset=15)
text = onnx_simple_text_plot(onx, verbose=False, add_links=True)
print(text)

>>>

    opset: domain='' version=14  
    input: name='X' type=dtype('float32') shape=['', 3]  ----------------------------------------------------+-+
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,) -- array([1.672, 0.747, 3.31 ], dtype=float32)   |-|-----------+
    init: name='Ge_Gemmcst' type=dtype('float32') shape=(3, 3)  ------------------------------------+        | |           |
    init: name='Mu_Mulcst' type=dtype('float32') shape=(1,) -- array([0.], dtype=float32)  -----+   |        | |           |
    ReduceSumSquare(X, axes=[1], keepdims=1) -> Re_reduced0  <--+-------------------------------|-+-|--------+ |           |
      Mul(Re_reduced0, Mu_Mulcst) -> Mu_C0  <-------------------+-------------------------------+ | |          |           |
        Gemm(X, Ge_Gemmcst, Mu_C0, alpha=-2.00, transB=1) -> Ge_Y0  < ----------------------------|-+----------+           |
      Add(Re_reduced0, Ge_Y0) -> Ad_C01  <--------------------------------------------------------+                        |
        Add(Ad_Addcst, Ad_C01) -> Ad_C0  ----------------+-+---------------------------------------------------------------+
          ArgMin(Ad_C0, axis=1, keepdims=0) -> label  <--+-|--+
          Sqrt(Ad_C0) -> scores  <-------------------------+--|-----+
    output: name='label' type=dtype('int64') shape=['']  <----+     |
    output: name='scores' type=dtype('float32') shape=['', 3]  <----+

Visually, it looks like the following: