willsonlincake 发表于 2022-4-4 20:53:18

ONNX:机器学习模型通用格式

Python示例:
import numpy
from onnx import numpy_helper, TensorProto
from onnx.helper import (
    make_model, make_node, set_model_props, make_tensor,
    make_graph, make_tensor_value_info)
from mlprodict.plotting.text_plot import onnx_simple_text_plot

# inputs

# 'X' is the name, TensorProto.FLOAT the type, the shape
X = make_tensor_value_info('X', TensorProto.FLOAT, )
A = make_tensor_value_info('A', TensorProto.FLOAT, )
B = make_tensor_value_info('B', TensorProto.FLOAT, )

# outputs, the shape is left undefined

Y = make_tensor_value_info('Y', TensorProto.FLOAT, None)

# nodes

# It creates a node defined by the operator type MatMul,
# 'X', 'A' are the inputs of the node, 'XA' the output.
node1 = make_node('MatMul', ['X', 'A'], ['XA'])
node2 = make_node('Add', ['XA', 'B'], ['Y'])

# from nodes to graph
# the graph is built from the list of nodes, the list of inputs,
# the list of outputs and a name.

graph = make_graph(,# nodes
                   'lr',# a name
                   ,# inputs
                   )# outputs

# onnx graph
# there is no metata in this case.

onnx_model = make_model(graph)

# the work is done, let's display it...
print(onnx_simple_text_plot(onnx_model))

willsonlincake 发表于 2022-4-4 20:54:02

官网:https://pypi.org/project/onnx/
页: [1]
查看完整版本: ONNX:机器学习模型通用格式