module plotting.text_plot#

Short summary#

module mlprodict.plotting.text_plot

Text representations of graphs.

source on GitHub

Functions#

function

truncated documentation

_append_succ_pred

_append_succ_pred_s

_get_shape

_get_type

get_hidden_inputs

Returns the list of hidden inputs used by subgraphs.

graph_predecessors_and_successors

Returns the successors and the predecessors within on ONNX graph.

onnx_simple_text_plot

Displays an ONNX graph into text.

onnx_text_plot

Uses onnx2bigraph() to convert the ONNX graph into text.

onnx_text_plot_io

Displays information about input and output types.

onnx_text_plot_tree

Gives a textual representation of a tree ensemble.

reorder_nodes_for_display

Reorders the node with breadth first seach (BFS).

Documentation#

Text representations of graphs.

source on GitHub

mlprodict.plotting.text_plot._append_succ_pred(subgraphs, successors, predecessors, node_map, node, prefix='', parent_node_name=None)#
mlprodict.plotting.text_plot._append_succ_pred_s(subgraphs, successors, predecessors, node_map, nodes, prefix='', parent_node_name=None, parent_graph=None)#
mlprodict.plotting.text_plot._get_shape(obj)#
mlprodict.plotting.text_plot._get_type(obj0)#
mlprodict.plotting.text_plot.get_hidden_inputs(nodes)#

Returns the list of hidden inputs used by subgraphs.

Parameters:

nodes – list of nodes

Returns:

list of names

source on GitHub

mlprodict.plotting.text_plot.graph_predecessors_and_successors(graph)#

Returns the successors and the predecessors within on ONNX graph.

source on GitHub

mlprodict.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)#

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 mlprodict.plotting.plotting import onnx_simple_text_plot
from mlprodict.onnx_conv import to_onnx

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=[None, 3]
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,) -- array([2.403, 2.898, 0.215], 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=[None]
    output: name='scores' type=dtype('float32') shape=[None, 3]

The same graphs with links.

<<<

import numpy
from sklearn.cluster import KMeans
from mlprodict.plotting.plotting import onnx_simple_text_plot
from mlprodict.onnx_conv import to_onnx

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=[None, 3]  --------------------------------------------------+-+
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,) -- array([0.973, 4.408, 2.592], 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=[None]  <----+     |
    output: name='scores' type=dtype('float32') shape=[None, 3]  <----+

Visually, it looks like the following:

source on GitHub

mlprodict.plotting.text_plot.onnx_text_plot(model_onnx, recursive=False, graph_type='basic', grid=5, distance=5)#

Uses onnx2bigraph to convert the ONNX graph into text.

Parameters:
Returns:

text

<<<

import numpy
from mlprodict.onnx_conv import to_onnx
from mlprodict import __max_supported_opset__ as opv
from mlprodict.plotting.plotting import onnx_text_plot
from mlprodict.npy.xop import loadop

OnnxAdd, OnnxSub = loadop('Add', 'Sub')

idi = numpy.identity(2).astype(numpy.float32)
A = OnnxAdd('X', idi, op_version=opv)
B = OnnxSub(A, 'W', output_names=['Y'], op_version=opv)
onx = B.to_onnx({'X': idi, 'W': idi})
print(onnx_text_plot(onx))

>>>

                          A  S                    
                          d  u                    
                          d  b                    
                                                  
                                                  
                                                  
                                                  
     Input-0                 I1          W        
     Input-1              I0             X        
        Init              I1             init     
       inout              O0 I0          out_add_0
    Output-0                 O0          Y        
                                                  
                                                  
                                                  
                                                  
                                                  
                          _  _                    
                          a  s                    
                          d  u                    
                          d  b

source on GitHub

mlprodict.plotting.text_plot.onnx_text_plot_io(model, verbose=False, att_display=None)#

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 mlprodict.plotting.plotting import onnx_text_plot_io
from mlprodict.onnx_conv import to_onnx

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=[None, 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=[None]
    output: name='scores' type=dtype('float32') shape=[None, 3]

source on GitHub

mlprodict.plotting.text_plot.onnx_text_plot_tree(node)#

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 mlprodict.onnx_conv import to_onnx
from mlprodict.plotting.plotting 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
     X3 <= 0.8
       F X3 <= 1.75
          F X2 <= 4.85
             F y=2.0 f=0 i=8
             T y=1.6666666 f=0 i=7
          T X2 <= 4.95
             F y=1.6666666 f=0 i=5
             T y=1.0208334 f=0 i=4
       T y=0.0 f=0 i=1

source on GitHub

mlprodict.plotting.text_plot.reorder_nodes_for_display(nodes, verbose=False)#

Reorders the node with breadth first seach (BFS).

Parameters:
  • nodes – list of ONNX nodes

  • verbose – dislay intermediate informations

Returns:

reordered list of nodes

source on GitHub