module onnxrt.shape_object#

Inheritance diagram of mlprodict.onnxrt.shape_object

Short summary#

module mlprodict.onnxrt.shape_object

Shape object.

source on GitHub

Classes#

class

truncated documentation

BaseDimensionShape

Base class to DimensionObject, ShapeOperator, ShapeObject.

DimensionObject

One dimension of a shape.

ShapeBinaryFctOperator

Base class for shape binary operator defined by a function.

ShapeBinaryOperator

Base class for shape binary operator.

ShapeObject

Handles mathematical operations around shapes. It stores a type (numpy type), and a name to somehow have …

ShapeObjectFct

Computes a shape depending on a user defined function. See Conv for an example.

ShapeOperator

Base class for all shapes operator.

ShapeOperatorAdd

Shape addition.

ShapeOperatorGreater

Shape comparison.

ShapeOperatorMax

Best on each dimension.

ShapeOperatorMul

Shape multiplication.

Properties#

property

truncated documentation

dim

Returns the dimension.

dtype

Returns the stored dtype.

dtype

Returns the stored dtype.

shape

Returns the stored shape.

shape

Returns the stored shape.

Static Methods#

staticmethod

truncated documentation

_infer_merged_type

_infer_merged_type

_same_

Returns obj if obj is DimensionObject otherwise converts it.

einsum_shape

Computes einsum shapes. Not the most efficient one as it creates variables of the given shapes.

einsum_shape

Computes einsum shapes. Not the most efficient one as it creates variables of the given shapes.

gather_shape

Computes Gather shapes.

gather_shape

Computes Gather shapes.

Methods#

method

truncated documentation

__add__

usual

__eq__

usual

__eq__

Tests equality between two shapes.

__eq__

Tests equality between two shapes.

__getitem__

Extracts a specific dimension.

__getitem__

Extracts a specific dimension.

__gt__

usual

__gt__

Compares shapes. Operator >.

__gt__

Compares shapes. Operator >.

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__init__

__iter__

Iterators over dimensions.

__iter__

Iterators over dimensions.

__len__

Returns the number of dimensions.

__len__

Returns the number of dimensions.

__mul__

usual

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

usual

__repr__

Displays a string.

__repr__

Displays a string.

__repr__

Displays a string.

__repr__

Displays a string.

__setitem__

Changes a specific dimension.

__setitem__

Changes a specific dimension.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_evaluate_string_

Evalutes the operator assuming some of them are still strings.

_to_string1

_to_string1

_to_string1

_to_string1

_to_string1

_to_string1

_to_string2

_to_string2

_to_string2

_to_string2

_to_string2

_to_string2

_to_string2b

_to_string2b

_to_string2b

_to_string2b

_to_string2b

_to_string2b

_to_string3

_to_string3

_to_string3

_to_string3

_to_string3

_to_string3

append

Appends a dimension.

append

Appends a dimension.

broadcast

Computes the shape after a broadcast.

broadcast

Computes the shape after a broadcast.

concat_columns

Concatenates columns from shapes to this one along one axis.

concat_columns

Concatenates columns from shapes to this one along one axis.

copy

A copy not a deepcopy.

copy

A copy not a deepcopy.

drop_axis

Drops an axis.

drop_axis

Drops an axis.

evaluate

Evaluates the object, reduces the expression to a number or a string.

evaluate

Evalutes the dimension.

evaluate

Evalutes the operator.

evaluate

Evalutes the operator.

evaluate

Evaluates the shape.

evaluate

Evaluates the shape.

evaluate

Evalutes the operator.

evaluate

Evalutes the operator.

evaluate

Evalutes the operator.

evaluate

Evalutes the operator.

evaluate

Evalutes the operator.

insert

Inserts a dimension at position pos.

insert

Inserts a dimension at position pos.

product

Multiplies all the dimension.

product

Multiplies all the dimension.

reduce

Reduces the matrix. Removes one dimension.

reduce

Reduces the matrix. Removes one dimension.

reshape

Creates a new shape, checks the number of elements is the same.

reshape

Creates a new shape, checks the number of elements is the same.

squeeze

Removes one dimension.

squeeze

Removes one dimension.

to_string

Converts the object into a string.

to_string

Represents the dimension as a string.

to_string

Applies binary operator to a dimension.

to_string

Applies binary operator to a dimension.

to_string

Converts shapes into a string.

to_string

Converts shapes into a string.

to_string

Displays as a string.

to_string

Applies binary operator to a dimension.

to_string

Applies binary operator to a dimension.

to_string

Applies binary operator to a dimension.

to_string

Applies binary operator to a dimension.

transpose

Removes one dimension.

transpose

Removes one dimension.

unsqueeze

Adds dimensions.

unsqueeze

Adds dimensions.

Documentation#

Shape object.

source on GitHub

class mlprodict.onnxrt.shape_object.BaseDimensionShape#

Bases: object

Base class to DimensionObject, ShapeOperator, ShapeObject.

source on GitHub

evaluate(**kwargs)#

Evaluates the object, reduces the expression to a number or a string.

source on GitHub

to_string(use_x=True)#

Converts the object into a string.

source on GitHub

class mlprodict.onnxrt.shape_object.DimensionObject(obj)#

Bases: BaseDimensionShape

One dimension of a shape.

source on GitHub

Parameters:

obj – int or DimensionObject or None to specify something unknown

source on GitHub

__add__(obj)#

usual

source on GitHub

__eq__(v)#

usual

source on GitHub

__gt__(obj)#

usual

source on GitHub

__hash__ = None#
__init__(obj)#
Parameters:

obj – int or DimensionObject or None to specify something unknown

source on GitHub

__mul__(obj)#

usual

source on GitHub

__repr__()#

usual

source on GitHub

static _same_(obj)#

Returns obj if obj is DimensionObject otherwise converts it.

source on GitHub

property dim#

Returns the dimension.

source on GitHub

evaluate(**kwargs)#

Evalutes the dimension.

Parameters:

kwargs – value for the variables.

Returns:

string or integer

source on GitHub

to_string(use_x=True)#

Represents the dimension as a string.

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeBinaryFctOperator(name, fct, fct_string, x, y)#

Bases: ShapeBinaryOperator

Base class for shape binary operator defined by a function.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

_evaluate_string_(args, **kwargs)#

Evalutes the operator assuming some of them are still strings.

Parameters:
  • args – arguments extracted by method evaluate

  • kwargs – value for the variables.

Returns:

string or integer

source on GitHub

_to_string2(x, y)#
_to_string2b(x, y)#
_to_string3(x)#
class mlprodict.onnxrt.shape_object.ShapeBinaryOperator(name, fct, fct_string, x, y)#

Bases: ShapeOperator

Base class for shape binary operator.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__init__(name, fct, fct_string, x, y)#
Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

_evaluate_string_(args, **kwargs)#

Evalutes the operator assuming some of them are still strings.

Parameters:
  • args – arguments extracted by method evaluate

  • kwargs – value for the variables.

Returns:

string or integer

source on GitHub

_to_string1(x, y)#
_to_string2(x, y)#
_to_string2b(x, y)#
_to_string3(x)#
to_string(use_x=True)#

Applies binary operator to a dimension.

Parameters:

use_x – use ‘x’ if dimension is unknown

Returns:

a string

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeObject(shape, dtype=None, use_n1=False, name=None, subtype=None)#

Bases: BaseDimensionShape

Handles mathematical operations around shapes. It stores a type (numpy type), and a name to somehow have an idea of where the shape comes from in the ONNX graph. The shape itself is defined by a list of DimensionObject or ShapeOperator or None if the shape is unknown. A dimension is an integer or a variable encoded as a string. This variable is a way to tell the dimension may vary.

<<<

import numpy
from mlprodict.onnxrt.shape_object import ShapeObject

sh1 = ShapeObject((1, 2), dtype=numpy.float32)
sh2 = ShapeObject((45, 2), dtype=numpy.float32)
mx = max(sh1, sh2)
print(mx)

sh1 = ShapeObject((1, 2), dtype=numpy.float32)
sh2 = ShapeObject((None, 2), dtype=numpy.float32)
print(sh2)
mx = max(sh1, sh2)
print(mx.to_string())

sh1 = ShapeObject((1, 2), dtype=numpy.float32)
sh2 = ShapeObject(('n', 2), dtype=numpy.float32)
print(sh2)
mx = max(sh1, sh2)
print(mx.evaluate(n=4))

>>>

    ShapeObject((45, 2), dtype=numpy.float32)
    ShapeObject((DimensionObject('None'), 2), dtype=numpy.float32)
    (?, 2)
    ShapeObject((n, 2), dtype=numpy.float32)
    ShapeObject((4, 2), dtype=numpy.float32, name='None-EV')

source on GitHub

Parameters:
  • shape – tuple or numpy.array

  • dtype – dtype

  • use_n1 – use ‘n’ if the first dimension is unknown

  • name – optional, for debugging purposes

  • subtype – element type if this type is a list

source on GitHub

__eq__(a)#

Tests equality between two shapes.

source on GitHub

__getitem__(index)#

Extracts a specific dimension.

source on GitHub

__gt__(a)#

Compares shapes. Operator >.

source on GitHub

__hash__ = None#
__init__(shape, dtype=None, use_n1=False, name=None, subtype=None)#
Parameters:
  • shape – tuple or numpy.array

  • dtype – dtype

  • use_n1 – use ‘n’ if the first dimension is unknown

  • name – optional, for debugging purposes

  • subtype – element type if this type is a list

source on GitHub

__iter__()#

Iterators over dimensions.

source on GitHub

__len__()#

Returns the number of dimensions.

source on GitHub

__repr__()#

usual

source on GitHub

__setitem__(index, value)#

Changes a specific dimension.

source on GitHub

static _infer_merged_type(*args, use_dtype=True)#
append(dim)#

Appends a dimension.

source on GitHub

broadcast(a)#

Computes the shape after a broadcast.

source on GitHub

concat_columns(axis, *shapes)#

Concatenates columns from shapes to this one along one axis.

source on GitHub

copy(dtype=None, name=None)#

A copy not a deepcopy.

Parameters:
  • dtype – None or a value to rewrite the type.

  • name – overwrites the name

Returns:

ShapeObject

source on GitHub

drop_axis(axis)#

Drops an axis.

source on GitHub

property dtype#

Returns the stored dtype.

source on GitHub

static einsum_shape(equation, *inputs)#

Computes einsum shapes. Not the most efficient one as it creates variables of the given shapes.

source on GitHub

evaluate(**kwargs)#

Evaluates the shape.

source on GitHub

static gather_shape(input, indices, axis)#

Computes Gather shapes.

source on GitHub

insert(dim, pos=0)#

Inserts a dimension at position pos.

source on GitHub

product()#

Multiplies all the dimension.

Returns:

DimensionObject

source on GitHub

reduce(axis=1, keepdims=False, dtype=None)#

Reduces the matrix. Removes one dimension.

Parameters:
  • axis – axis

  • keepdims – keep dimensions, replaces the removed dimension by 1

  • dtype – if not None, changes the type

Returns:

new dimension

source on GitHub

reshape(shape)#

Creates a new shape, checks the number of elements is the same.

source on GitHub

property shape#

Returns the stored shape.

source on GitHub

squeeze(axis)#

Removes one dimension.

source on GitHub

to_string(use_x=False)#

Converts shapes into a string.

source on GitHub

transpose(perm)#

Removes one dimension.

source on GitHub

unsqueeze(axes)#

Adds dimensions.

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeObjectFct(fct, *shapes, dtype=None, name=None)#

Bases: ShapeObject

Computes a shape depending on a user defined function. See Conv for an example.

source on GitHub

Parameters:
  • fct – function

  • shapes – shapes sent to fct

  • dtype – dtype

  • name – optional, for debugging purposes

source on GitHub

__init__(fct, *shapes, dtype=None, name=None)#
Parameters:
  • fct – function

  • shapes – shapes sent to fct

  • dtype – dtype

  • name – optional, for debugging purposes

source on GitHub

evaluate(**kwargs)#

Evaluates the shape.

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeOperator(name, fct, fct_string, *args)#

Bases: BaseDimensionShape

Base class for all shapes operator.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • args – argument of the operator

source on GitHub

__init__(name, fct, fct_string, *args)#
Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • args – argument of the operator

source on GitHub

__repr__()#

usual

source on GitHub

_evaluate_string_(args, **kwargs)#

Evalutes the operator assuming some of them are still strings.

Parameters:
  • args – arguments extracted by method evaluate

  • kwargs – value for the variables.

Returns:

string or integer

source on GitHub

evaluate(**kwargs)#

Evalutes the operator.

Parameters:

kwargs – value for the variables.

Returns:

string or integer

source on GitHub

to_string(use_x=True)#

Displays as a string.

Returns:

a string

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeOperatorAdd(x, y)#

Bases: ShapeBinaryOperator

Shape addition.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__init__(x, y)#
Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__repr__()#

Displays a string.

Returns:

a string

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeOperatorGreater(x, y)#

Bases: ShapeBinaryOperator

Shape comparison.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__init__(x, y)#
Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__repr__()#

Displays a string.

Returns:

a string

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeOperatorMax(x, y)#

Bases: ShapeBinaryFctOperator

Best on each dimension.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__init__(x, y)#
Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__repr__()#

Displays a string.

Returns:

a string

source on GitHub

class mlprodict.onnxrt.shape_object.ShapeOperatorMul(x, y)#

Bases: ShapeBinaryOperator

Shape multiplication.

source on GitHub

Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__init__(x, y)#
Parameters:
  • name – display name of the operator

  • fct – function doing the operator if argument are numeric

  • fct_string – function represented as a string

  • x – first argument

  • y – second argument

source on GitHub

__repr__()#

Displays a string.

Returns:

a string

source on GitHub