-
Notifications
You must be signed in to change notification settings - Fork 0
/
hier_lstm.py
280 lines (243 loc) · 12.6 KB
/
hier_lstm.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import sys
import mxnet as mx
import numpy as np
from collections import namedtuple
from copy import copy
LSTMState = namedtuple("LSTMState", ["c", "h"])
LSTMParam = namedtuple("LSTMParam", ["i2h_weight", "i2h_bias",
"h2h_weight", "h2h_bias"])
LSTMModel = namedtuple("LSTMModel", ["rnn_exec", "symbol",
"init_states", "last_states",
"seq_data", "seq_labels", "seq_outputs",
"param_blocks"])
HyperPara = namedtuple('HyperPara', ['num_lstm_layer', 'seq_len', 'input_size',
'num_hidden', 'num_embed', 'num_label',
'dropout'])
def lstm(num_hidden, indata, prev_state, param, seqidx, layeridx, dropout=0.):
"""LSTM Cell symbol"""
if dropout > 0.:
indata = mx.sym.Dropout(data=indata, p=dropout)
i2h = mx.sym.FullyConnected(data=indata,
weight=param.i2h_weight,
bias=param.i2h_bias,
num_hidden=num_hidden * 4,
name="t%d_l%d_i2h" % (seqidx, layeridx))
h2h = mx.sym.FullyConnected(data=prev_state.h,
weight=param.h2h_weight,
bias=param.h2h_bias,
num_hidden=num_hidden * 4,
name="t%d_l%d_h2h" % (seqidx, layeridx))
gates = i2h + h2h
slice_gates = mx.sym.SliceChannel(gates, num_outputs=4,
name="t%d_l%d_slice" % (seqidx, layeridx))
in_gate = mx.sym.Activation(slice_gates[0], act_type="sigmoid")
in_transform = mx.sym.Activation(slice_gates[1], act_type="tanh")
forget_gate = mx.sym.Activation(slice_gates[2], act_type="sigmoid")
out_gate = mx.sym.Activation(slice_gates[3], act_type="sigmoid")
next_c = (forget_gate * prev_state.c) + (in_gate * in_transform)
next_h = out_gate * mx.sym.Activation(next_c, act_type="tanh")
return LSTMState(c=next_c, h=next_h)
def sentence_lstm(indata, sent_idx, param_cells, num_lstm_layer, seq_len, num_hidden, dropout=0.):
#multilayer lstm
last_states = []
for i in range(num_lstm_layer):
with mx.AttrScope(ctx_group='sent_layers'):
state = LSTMState(c=mx.sym.Variable("sent%d_l%d_init_c" % (sent_idx, i)),
h=mx.sym.Variable("sent%d_l%d_init_h" % (sent_idx, i)))
last_states.append(state)
for seqidx in range(seq_len):
hidden = indata[seqidx]
# stack LSTM
for i in range(num_lstm_layer):
if i == 0:
dp_ratio = 0.
else:
dp_ratio = dropout
with mx.AttrScope(ctx_group='sent_layers'):
next_state = lstm(num_hidden, indata=hidden,
prev_state=last_states[i],
param=param_cells[i],
seqidx=seqidx, layeridx=i, dropout=dp_ratio)
hidden = next_state.h
last_states[i] = next_state
# decoder
if dropout > 0.:
hidden = mx.sym.Dropout(data=hidden, p=dropout)
return last_states[-1]
def document_lstm(indata, num_lstm_layer, seq_len, num_hidden, dropout=0.):
param_cells = []
last_states = []
for i in range(num_lstm_layer):
with mx.AttrScope(ctx_group='doc_layers'):
param_cells.append(LSTMParam(i2h_weight=mx.sym.Variable("doc_l%d_i2h_weight" % i),
i2h_bias=mx.sym.Variable("doc_l%d_i2h_bias" % i),
h2h_weight=mx.sym.Variable("doc_l%d_h2h_weight" % i),
h2h_bias=mx.sym.Variable("doc_l%d_h2h_bias" % i)))
state = LSTMState(c=mx.sym.Variable("doc_l%d_init_c" % i),
h=mx.sym.Variable("doc_l%d_init_h" % i))
last_states.append(state)
assert(len(last_states) == num_lstm_layer)
for seqidx in range(seq_len):
hidden = indata[seqidx]
# stack LSTM
for i in range(num_lstm_layer):
if i == 0:
dp_ratio = 0.
else:
dp_ratio = dropout
with mx.AttrScope(ctx_group='doc_layers'):
next_state = lstm(num_hidden, indata=hidden,
prev_state=last_states[i],
param=param_cells[i],
seqidx=seqidx, layeridx=i, dropout=dp_ratio)
hidden = next_state.h
last_states[i] = next_state
# decoder
if dropout > 0.:
hidden = mx.sym.Dropout(data=hidden, p=dropout)
return last_states
def hier_lstm(indata, level1_para, level2_para):
with mx.AttrScope(ctx_group='preproc'):
para = mx.sym.SliceChannel(data=indata, num_outputs=level2_para.seq_len)
with mx.AttrScope(ctx_group='embed'):
embed_weight = mx.sym.Variable("embed_weight")
#sent_level
param_cells = []
for i in range(level1_para.num_lstm_layer):
with mx.AttrScope(ctx_group='sent_layers'):
param_cells.append(LSTMParam(i2h_weight=mx.sym.Variable("sent_l%d_i2h_weight" % i),
i2h_bias=mx.sym.Variable("sent_l%d_i2h_bias" % i),
h2h_weight=mx.sym.Variable("sent_l%d_h2h_weight" % i),
h2h_bias=mx.sym.Variable("sent_l%d_h2h_bias" % i)))
sentence_vecs = []
for i in range(level2_para.seq_len):
# embeding layer
with mx.AttrScope(ctx_group='embed'):
embed = mx.sym.Embedding(data=para[i], input_dim=level1_para.input_size,
weight=embed_weight, output_dim=level1_para.num_embed,
name='embed')
wordvec = mx.sym.SliceChannel(data=embed, num_outputs=level1_para.seq_len,
squeeze_axis=1)
vec = sentence_lstm(wordvec, i, param_cells, level1_para.num_lstm_layer,
level1_para.seq_len, level1_para.num_hidden, level1_para.dropout)
sentence_vecs.append(vec.h)
#doc level
final_state = document_lstm(sentence_vecs, level2_para.num_lstm_layer, level2_para.seq_len,
level2_para.num_hidden, level2_para.dropout)
return final_state
def lstm_decoder(in_lstm_state, num_lstm_layer, seq_len, num_hidden, num_label, dropout=0.):
# with mx.AttrScope(ctx_group='embed'):
# embed_weight=mx.sym.Variable("embed_weight")
with mx.AttrScope(ctx_group='decode'):
cls_weight = mx.sym.Variable("cls_weight")
cls_bias = mx.sym.Variable("cls_bias")
param_cells = []
last_states = []
for i in range(num_lstm_layer):
with mx.AttrScope(ctx_group='dec_layers'):
param_cells.append(LSTMParam(i2h_weight=mx.sym.Variable("dec_l%d_i2h_weight" % i),
i2h_bias=mx.sym.Variable("dec_l%d_i2h_bias" % i),
h2h_weight=mx.sym.Variable("dec_l%d_h2h_weight" % i),
h2h_bias=mx.sym.Variable("dec_l%d_h2h_bias" % i)))
state = LSTMState(c=mx.sym.Variable("dec_l%d_init_c" % i),
h=mx.sym.Variable("dec_l%d_init_h" % i))
last_states.append(state)
hidden_all = []
hidden = in_lstm_state.h
for seqidx in range(seq_len):
# stack LSTM
for i in range(num_lstm_layer):
if i == 0:
dp_ratio = 0.
else:
dp_ratio = dropout
with mx.AttrScope(ctx_group='dec_layers'):
next_state = lstm(num_hidden, indata=hidden,
prev_state=last_states[i],
param=param_cells[i],
seqidx=seqidx, layeridx=i, dropout=dp_ratio)
hidden = next_state.h
last_states[i] = next_state
# decoder
if dropout > 0.:
hidden = mx.sym.Dropout(data=hidden, p=dropout)
hidden_all.append(hidden)
with mx.AttrScope(ctx_group='decode'):
hidden_concat = mx.sym.Concat(*hidden_all, dim=0)
pred = mx.sym.FullyConnected(data=hidden_concat, num_hidden=num_label,
weight=cls_weight, bias=cls_bias, name='pred')
return pred
def lstm_decoder_2(in_lstm_state, num_lstm_layer, seq_len, num_hidden, num_label, dropout=0.):
# pass the state
with mx.AttrScope(ctx_group='decode'):
cls_weight = mx.sym.Variable("cls_weight")
cls_bias = mx.sym.Variable("cls_bias")
param_cells = []
last_states = copy(in_lstm_state)
for i in range(num_lstm_layer):
with mx.AttrScope(ctx_group='dec_layers'):
param_cells.append(LSTMParam(i2h_weight=mx.sym.Variable("dec_l%d_i2h_weight" % i),
i2h_bias=mx.sym.Variable("dec_l%d_i2h_bias" % i),
h2h_weight=mx.sym.Variable("dec_l%d_h2h_weight" % i),
h2h_bias=mx.sym.Variable("dec_l%d_h2h_bias" % i)))
hidden_all = []
hidden = mx.sym.Variable('dec_start')
for seqidx in range(seq_len):
# stack LSTM
for i in range(num_lstm_layer):
if i == 0:
dp_ratio = 0.
else:
dp_ratio = dropout
with mx.AttrScope(ctx_group='dec_layers'):
next_state = lstm(num_hidden, indata=hidden,
prev_state=last_states[i],
param=param_cells[i],
seqidx=seqidx, layeridx=i, dropout=dp_ratio)
hidden = next_state.h
last_states[i] = next_state
# decoder
if dropout > 0.:
hidden = mx.sym.Dropout(data=hidden, p=dropout)
hidden_all.append(hidden)
with mx.AttrScope(ctx_group='decode'):
hidden_concat = mx.sym.Concat(*hidden_all, dim=0)
pred = mx.sym.FullyConnected(data=hidden_concat, num_hidden=num_label,
weight=cls_weight, bias=cls_bias, name='pred')
return pred
def seq_cross_entropy(label, pred):
with mx.AttrScope(ctx_group='loss'):
label = mx.sym.transpose(data=label)
label = mx.sym.Reshape(data=label, target_shape=(0,))
sm = mx.sym.SoftmaxOutput(data=pred, label=label, name='softmax')
#sm = mx.sym.softmax_cross_entropy(lhs=pred, rhs=label, name='softmax')
return sm
def hier_lstm_model(data_name, label_name,
sent_enc_para, doc_enc_para, dec_para):
data = mx.sym.Variable(data_name)
label = mx.sym.Variable(label_name)
doc_state = hier_lstm(data, sent_enc_para, doc_enc_para)
# pred = lstm_decoder(doc_state[-1], dec_para.num_lstm_layer, dec_para.seq_len,
# dec_para.num_hidden, dec_para.num_label, dec_para.dropout)
pred = lstm_decoder_2(doc_state, dec_para.num_lstm_layer, dec_para.seq_len,
dec_para.num_hidden, dec_para.num_label, dec_para.dropout)
loss = seq_cross_entropy(label, pred)
return loss
def get_hier_input_shapes(sent_enc_para, doc_enc_para, dec_para, batch_size):
# input_shapes = {}
# input_shapes['data'] = (batch_size, sent_enc_para.seq_len * 3)
# input_shapes['softmax_label'] = (batch_size, dec_para.seq_len)
init_state_shapes = {}
arg_shapes = {}
for i in range(doc_enc_para.seq_len):
for j in range(sent_enc_para.num_lstm_layer):
init_state_shapes['sent{}_l{}_init_h'.format(i, j)] = (batch_size, sent_enc_para.num_hidden)
init_state_shapes['sent{}_l{}_init_c'.format(i, j)] = (batch_size, sent_enc_para.num_hidden)
for i in range(doc_enc_para.num_lstm_layer):
init_state_shapes['doc_l{}_init_h'.format(i)] = (batch_size, doc_enc_para.num_hidden)
init_state_shapes['doc_l{}_init_c'.format(i)] = (batch_size, doc_enc_para.num_hidden)
# for i in range(dec_para.num_lstm_layer):
# init_state_shapes['dec_l{}_init_h'.format(i)] = (batch_size, dec_para.num_hidden)
# init_state_shapes['dec_l{}_init_c'.format(i)] = (batch_size, dec_para.num_hidden)
init_state_shapes['dec_start'] = (batch_size, dec_para.num_hidden)
return init_state_shapes