Movatterモバイル変換


[0]ホーム

URL:


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

Contents

More

Back to top

Light API for ONNX: everything in one line

It is inspired from thereverse Polish notation.Following example implements the euclidean distance.This API tries to keep it simple and intuitive to short functions.

<<<

importnumpyasnpfromonnx_array_api.light_apiimportstartfromonnx_array_api.plotting.text_plotimportonnx_simple_text_plotmodel=(start().vin("X").vin("Y").bring("X","Y").Sub().rename("dxy").cst(np.array([2],dtype=np.int64),"two").bring("dxy","two").Pow().ReduceSum().rename("Z").vout().to_onnx())print(onnx_simple_text_plot(model))

>>>

opset:domain=''version=22input:name='X'type=dtype('float32')shape=Noneinput:name='Y'type=dtype('float32')shape=Noneinit:name='two'type=int64shape=(1,)--array([2])Sub(X,Y)->dxyPow(dxy,two)->r1_0ReduceSum(r1_0,keepdims=1,noop_with_empty_axes=0)->Zoutput:name='Z'type=dtype('float32')shape=None

There are two kinds of methods, the graph methods, playing with the graph structure,and the methods for operators starting with an upper letter.

Graph methods

Any graph must start with functionstart.It is usually following byvin to add an input.

These methods are implemented in classonnx_array_api.light_api.var.BaseVar

Operator methods

They are described inONNX Operators and redefined in a stable APIso that the definition should not change depending on this opset.onnx_array_api.light_api.Var defines all operators taking only one input.onnx_array_api.light_api.Vars defines all other operators.

Numpy methods

Numpy users expect methods such asreshape, propertyshape oroperator+ to be available as well and that the case. They aredefined in classVar orVars depending on the number ofinputs they require. Their name starts with a lower letter.

Other domains

The following example uses operatorNormalizer from domainai.onnx.ml. The operator name is called with the syntax<domain>.<operator name>. The domain may have dots in its namebut it must follow the python definition of a variable.The operatorNormalizer becomesai.onnx.ml.Normalizer.

<<<

importnumpyasnpfromonnx_array_api.light_apiimportstartfromonnx_array_api.plotting.text_plotimportonnx_simple_text_plotmodel=(start(opset=19,opsets={"ai.onnx.ml":3}).vin("X").reshape((-1,1)).rename("USE").ai.onnx.ml.Normalizer(norm="MAX").rename("Y").vout().to_onnx())print(onnx_simple_text_plot(model))

>>>

opset:domain=''version=19opset:domain='ai.onnx.ml'version=3input:name='X'type=dtype('float32')shape=Noneinit:name='r'type=int64shape=(2,)--array([-1,1])Reshape(X,r)->USENormalizer(USE,norm=b'MAX')->Youtput:name='Y'type=dtype('float32')shape=None
On this page

[8]ページ先頭

©2009-2025 Movatter.jp