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

GraphBuilder: common API for ONNX

This is a very common way to build ONNX graph. There are someannoying steps while building an ONNX graph. The first one is togive unique names to every intermediate result in the graph. The secondis the conversion from numpy arrays to onnx tensors. Agraph builder,here implemented by classGraphBuilderusually makes these two frequent tasks easier.

<<<

importnumpyasnpfromonnx_array_api.graph_apiimportGraphBuilderfromonnx_array_api.plotting.text_plotimportonnx_simple_text_plotg=GraphBuilder()g.make_tensor_input("X",np.float32,(None,None))g.make_tensor_input("Y",np.float32,(None,None))r1=g.make_node("Sub",["X","Y"])# the name given to the output is given by the class,# it ensures the name is uniqueinit=g.make_initializer(np.array([2],dtype=np.int64))# the class automatically# converts the array to a tensorr2=g.make_node("Pow",[r1,init])g.make_node("ReduceSum",[r2],outputs=["Z"])# the output name is given because# the user wants to choose the nameg.make_tensor_output("Z",np.float32,(None,None))onx=g.to_onnx()# final conversion to onnxprint(onnx_simple_text_plot(onx))

>>>

opset:domain=''version=22input:name='X'type=dtype('float32')shape=['','']input:name='Y'type=dtype('float32')shape=['','']init:name='cst'type=int64shape=(1,)--array([2])Sub(X,Y)->_onx_sub0Pow(_onx_sub0,cst)->_onx_pow0ReduceSum(_onx_pow0)->Zoutput:name='Z'type=dtype('float32')shape=['','']

A more simple versions of the same code to produce the same graph.

<<<

importnumpyasnpfromonnx_array_api.graph_apiimportGraphBuilderfromonnx_array_api.plotting.text_plotimportonnx_simple_text_plotg=GraphBuilder()g.make_tensor_input("X",np.float32,(None,None))g.make_tensor_input("Y",np.float32,(None,None))r1=g.op.Sub("X","Y")# the method name indicates which operator to use,# this can be used when there is no ambiguity about the# number of outputsr2=g.op.Pow(r1,np.array([2],dtype=np.int64))g.op.ReduceSum(r2,outputs=["Z"])# the still wants the user to specify the nameg.make_tensor_output("Z",np.float32,(None,None))onx=g.to_onnx()print(onnx_simple_text_plot(onx))

>>>

opset:domain=''version=22input:name='X'type=dtype('float32')shape=['','']input:name='Y'type=dtype('float32')shape=['','']init:name='cst'type=int64shape=(1,)--array([2])Sub(X,Y)->_onx_sub0Pow(_onx_sub0,cst)->_onx_pow0ReduceSum(_onx_pow0)->Zoutput:name='Z'type=dtype('float32')shape=['','']

[8]
ページ先頭

©2009-2025 Movatter.jp