-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
131 lines (105 loc) · 3.71 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import onnx
import numpy as np
from builder import *
def make_lstm_cell(x_in, h_in, c_in, W, R, B=None):
Wi, Wo, Wf, Wc = W
Ri, Ro, Rf, Rc = R
Bi, Bo, Bf, Bc = B or None, None, None, None
i = MatMul(Wi, x_in)+MatMul(Ri, h_in)
if Bi:
i = i + Bi
i = Sigmoid(i)
o = MatMul(Wo, x_in) + MatMul(Ro, h_in)
if Bo:
o = o + Bo
o = Sigmoid(o)
f = MatMul(Wf, x_in) + MatMul(Rf, h_in)
if Bf:
f = f + Bf
f = Sigmoid(f)
c = MatMul(Wc, x_in) + MatMul(Rc, h_in)
if Bc:
c = c + BatchNormalization
c = Tanh(c)
c_out = f*c_in + i*c
h_out = o*Tanh(c_out)
return h_out, c_out
def lstm_cell():
N = 4 # Batch size
C = 32 # Input word size
H = 16 # Hidden size
exporter = Exporter()
x_in = Input(exporter, 'x_in', np.float32, [N, C])
h_in = Input(exporter, 'h_in', np.float32, [N, H])
c_in = Input(exporter, 'c_in', np.float32, [N, H])
Wi = Input(exporter, 'Wi', np.float32, [H, C])
Wo = Input(exporter, 'Wo', np.float32, [H, C])
Wf = Input(exporter, 'Wf', np.float32, [H, C])
Wc = Input(exporter, 'Wc', np.float32, [H, C])
Ri = Input(exporter, 'Ri', np.float32, [H, H])
Ro = Input(exporter, 'Ro', np.float32, [H, H])
Rf = Input(exporter, 'Rf', np.float32, [H, H])
Rc = Input(exporter, 'Rc', np.float32, [H, H])
h_out, c_out = make_lstm_cell(
x_in, h_in, c_in, (Wi, Wo, Wf, Wc), (Ri, Ro, Rf, Rc))
exporter.add_graph_output('h_out', h_out, np.float32)
exporter.add_graph_output('c_out', c_out, np.float32)
md = exporter.export('LSTM cell')
onnx.checker.check_model(md)
onnx.save(md, 'lstm_cell.onnx')
def a_plus_b():
# The exporter will convert a graph to ONNX
exporter = Exporter()
# Add two inputs
a = Input(exporter, 'a', np.float32, [32, 32])
b = Input(exporter, 'b', np.float32, [32, 32])
# Add one output
exporter.add_graph_output('output', Abs(a)+b, np.float32)
# Export as ONNX
md = exporter.export('a plus b')
onnx.checker.check_model(md)
onnx.save(md, 'a_plus_b.onnx')
def test_concat():
exporter = Exporter()
# Add two inputs
a = Input(exporter, 'a', np.float32, [32, 32])
b = Input(exporter, 'b', np.float32, [32, 32])
# Add one output
exporter.add_graph_output('output', Concat(a, b, a, a, b, axis=1), np.float32)
# Export as ONNX
md = exporter.export('concat')
onnx.checker.check_model(md)
onnx.save(md, 'concat.onnx')
def run():
a_plus_b()
test_concat()
lstm_cell()
b = Exporter()
N = 4
T = 140
X = Input(b, 'input', np.float32, [N, 32, 32, 3])
mean = Input(b, 'mean', np.float32, [32, 32, 3])
var = Input(b, 'var',np.float32, [32, 32, 3])
BX = BatchNormalization(X,
np.asarray([1.0, 1.0, 1.0], dtype=np.float32),
np.asarray([0.0, 0.0, 0.0], dtype=np.float32),
mean, var)
vpad = Pad(BX, np.asarray([0, 2, 0, 0], dtype=np.int64))
sum = Pad(vpad + Abs(vpad), np.asarray([3, 3, 3, 3], dtype=np.int64))
S = Input(b, 'sentences', np.float32, [N, T, 128])
lens = Input(b, 'seqlen', np.int32, [N, T])
CW = np.ones([1, 64, 128], dtype=np.float32)
CR = np.ones([1, 64, 16], dtype=np.float32)
CB = np.zeros([1, 128], dtype=np.float32)
L = LSTM(S, CW, CR, CB, lens)
b.add_graph_output('output', sum, np.float32)
b.add_graph_output('running_mean', BX.running_mean, np.float32)
b.add_graph_output('Y_h', L.Y_h, np.float32)
md = b.export('test-model')
print(f'The graph in model:\n{md.graph}')
onnx.checker.check_model(md)
print("Checked")
onnx.save(md, 'test-model.onnx')
return
if __name__ == "__main__":
run()