module plotting.text_plot#

Short summary#

module mlprodict.plotting.text_plot

Text representations of graphs.

source on GitHub

Functions#

function

truncated documentation

_get_shape

_get_type

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._get_shape(obj)#
mlprodict.plotting.text_plot._get_type(obj0)#
mlprodict.plotting.text_plot.onnx_simple_text_plot(model, verbose=False, att_display=None, add_links=False, recursive=False, functions=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

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=(0, 3)
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,) -- array([0.333, 1.811, 7.144], dtype=float32)
    init: name='Ge_Gemmcst' type=dtype('float32') shape=(9,)
    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
          Sqrt(Ad_C0) -> scores
          ArgMin(Ad_C0, axis=1, keepdims=0) -> label
    output: name='label' type=dtype('int64') shape=(0,)
    output: name='scores' type=dtype('float32') shape=(0, 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=(0, 3)  -----------------------------------------------------+-+
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,) -- array([7.096, 0.733, 2.095], dtype=float32)   |-|-----------+
    init: name='Ge_Gemmcst' type=dtype('float32') shape=(9,)  --------------------------------------+        | |           |
    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  ---+--------------+---------------------------------------------------------------+
          Sqrt(Ad_C0) -> scores  <----------+--------------|---------+
          ArgMin(Ad_C0, axis=1, keepdims=0) -> label  <----+         |
    output: name='label' type=dtype('int64') shape=(0,)              |
    output: name='scores' type=dtype('float32') shape=(0, 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=(0, 3)
    init: name='Ad_Addcst' type=dtype('float32') shape=(3,)
    init: name='Ge_Gemmcst' type=dtype('float32') shape=(9,)
    init: name='Mu_Mulcst' type=dtype('float32') shape=(1,)
    output: name='label' type=dtype('int64') shape=(0,)
    output: name='scores' type=dtype('float32') shape=(0, 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
     X2 <= 2.4499998
       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