PandA-2024.02
e1_onnx_build.py
Go to the documentation of this file.
1 #WARNING: this network has not been trained! The weights are random!
2 
3 import numpy as np
4 
5 import onnx
6 from onnx import helper, shape_inference, optimizer
7 from onnx import numpy_helper
8 from onnx import AttributeProto, TensorProto, GraphProto
9 
10 # Create graph input X
11 X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1,784])
12 
13 W_info = helper.make_tensor_value_info('W', TensorProto.FLOAT, [784,10])
14 W = np.random.randn(784,10).astype(np.float32)
15 W = numpy_helper.from_array(W, 'W')
16 
17 B_info = helper.make_tensor_value_info('B', TensorProto.FLOAT, [1,10])
18 B = np.ones([1,10]).astype(np.float32)
19 B = numpy_helper.from_array(B, 'B')
20 
21 # Create graph output Y
22 Z = helper.make_tensor_value_info('Z', TensorProto.FLOAT, [1,10])
23 
24 matmul = helper.make_node(
25  'MatMul', # name
26  ['X', 'W'], # inputs
27  ['Y'], # outputs
28  )
29 
30 bias = helper.make_node(
31  'Add',
32  ['Y', 'B'],
33  ['V'],
34  )
35 
36 softmax = helper.make_node(
37  'Softmax',
38  ['V'],
39  ['Z']
40  )
41 
42 
43 
44 graph_def = helper.make_graph(
45  nodes=[matmul, bias, softmax], # graph nodes
46  name= 'mlp_model', # graph name
47  inputs = [X, W_info, B_info], # graph inputs
48  outputs = [Z], # graph outputs
49  initializer = [W, B],
50  )
51 
52 model_def = helper.make_model(graph_def, producer_name='benchmarks')
53 
54 onnx.checker.check_model(model_def)
55 model_def = shape_inference.infer_shapes(model_def)
56 onnx.checker.check_model(model_def)
57 model_def = optimizer.optimize(model_def)
58 onnx.checker.check_model(model_def)
59 
60 onnx.save_model(model_def, 'e1_mlp.onnx')

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13