-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
305 lines (270 loc) · 10.8 KB
/
model.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import tensorflow as tf
from tensorflow.contrib.layers.python.layers import initializers
import tensorflow.contrib.rnn as rnn
from tensorflow.contrib.crf import crf_log_likelihood
from tensorflow.contrib.crf import viterbi_decode
import numpy as np
import data_utils
class Model(object):
def __init__(self, config):
self.config = config
self.lr = config['lr']
self.word_dim = config['word_dim']
self.lstm_dim = config['lstm_dim']
self.seg_dim = config['seg_dim']
self.num_tags = config['num_tags']
self.num_words = config['num_words']
self.num_sges = 4
self.global_step = tf.Variable(0, trainable=False)
self.best_dev_f1 = tf.Variable(0.0, trainable=False)
self.best_test_f1 = tf.Variable(0.0, trainable=False)
self.initializer = initializers.xavier_initializer()
# 申请占位符
self.word_inputs = tf.placeholder(dtype=tf.int32, shape=[None, None], name="wordInputs")
self.seg_inputs = tf.placeholder(dtype=tf.int32, shape=[None, None], name="SegInputs")
self.targets = tf.placeholder(dtype=tf.int32, shape=[None, None], name="Targets")
self.dropout = tf.placeholder(dtype=tf.float32, name="Dropout")
used = tf.sign(tf.abs(self.word_inputs))
length = tf.reduce_sum(used, reduction_indices=1)
self.lengths = tf.cast(length, tf.int32)
self.batch_size = tf.shape(self.word_inputs)[0]
self.num_setps = tf.shape(self.word_inputs)[-1]
# embedding层单词和分词信息
embedding = self.embedding_layer(self.word_inputs, self.seg_inputs, config)
# lstm输入层
lstm_inputs = tf.nn.dropout(embedding, self.dropout)
# lstm输出层
lstm_outputs = self.biLSTM_layer(lstm_inputs, self.lstm_dim, self.lengths)
# 投影层
self.logits = self.project_layer(lstm_outputs)
# 损失
self.loss = self.crf_loss_layer(self.logits, self.lengths)
with tf.variable_scope('optimizer'):
optimizer = self.config['optimizer']
if optimizer == "sgd":
self.opt = tf.train.GradientDescentOptimizer(self.lr)
elif optimizer == "adam":
self.opt = tf.train.AdamOptimizer(self.lr)
elif optimizer == "adgrad":
self.opt = tf.train.AdagradDAOptimizer(self.lr)
else:
raise Exception("优化器错误")
grad_vars = self.opt.compute_gradients(self.loss)
capped_grad_vars = [[tf.clip_by_value(g, -self.config['clip'], self.config['clip']), v] for g, v in
grad_vars]
self.train_op = self.opt.apply_gradients(capped_grad_vars, self.global_step)
# 保存模型
self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)
def embedding_layer(self, word_inputs, seg_inputs, config, name=None):
"""
:param word_inputs: one-hot编码
:param seg_inputs: 分词特征
:param config: 配置
:param name: 层的命名
:return:
"""
embedding = []
with tf.variable_scope("word_embedding" if not name else name), tf.device('/cpu:0'):
self.word_lookup = tf.get_variable(
name="word_embedding",
shape=[self.num_words, self.word_dim],
initializer=self.initializer
)
embedding.append(tf.nn.embedding_lookup(self.word_lookup, word_inputs))
if config['seg_dim']:
with tf.variable_scope("seg_embedding"), tf.device('/cpu:0'):
self.seg_lookup = tf.get_variable(
name="seg_embedding",
shape=[self.num_sges, self.seg_dim],
initializer=self.initializer
)
embedding.append(tf.nn.embedding_lookup(self.seg_lookup, seg_inputs))
embed = tf.concat(embedding, axis=-1)
return embed
def biLSTM_layer(self, lstm_inputs, lstm_dim, lengths, name=None):
"""
:param lstm_inputs: [batch_size, num_steps, emb_size]
:param lstm_dim:
:param name:
:return: [batch_size, num_steps, 2*lstm_dim]
"""
with tf.variable_scope("word_biLSTM" if not name else name):
lstm_cell = {}
for direction in ['forward', 'backward']:
with tf.variable_scope(direction):
lstm_cell[direction] = rnn.CoupledInputForgetGateLSTMCell(
lstm_dim,
use_peepholes=True,
initializer=self.initializer,
state_is_tuple=True
)
outputs, final_status = tf.nn.bidirectional_dynamic_rnn(
lstm_cell['forward'],
lstm_cell['backward'],
lstm_inputs,
dtype=tf.float32,
sequence_length=lengths
)
return tf.concat(outputs, axis=2)
def project_layer(self, lstm_outputs, name=None):
"""
:param lstm_outputs: [batch_size, num_steps, emb_size]
:param name:
:return: [btch_size,num_steps, num_tags]
"""
with tf.variable_scope('project_layer' if not name else name):
with tf.variable_scope('hidden_layer'):
W = tf.get_variable(
"W",
shape=[self.lstm_dim * 2, self.lstm_dim],
dtype=tf.float32,
initializer=self.initializer
)
b = tf.get_variable(
"b",
shape=[self.lstm_dim],
dtype=tf.float32,
initializer=tf.zeros_initializer()
)
out_put = tf.reshape(lstm_outputs, shape=[-1, self.lstm_dim * 2])
hidden = tf.tanh(tf.nn.xw_plus_b(out_put, W, b))
with tf.variable_scope('logits'):
W = tf.get_variable(
"W",
shape=[self.lstm_dim, self.num_tags],
dtype=tf.float32,
initializer=self.initializer
)
b = tf.get_variable(
"b",
shape=[self.num_tags],
dtype=tf.float32,
initializer=tf.zeros_initializer()
)
pred = tf.nn.xw_plus_b(hidden, W, b)
return tf.reshape(pred, [-1, self.num_setps, self.num_tags])
def crf_loss_layer(self, project_logits, lenghts, name=None):
"""
:param project_logits: [1, num_steps, num_tages
:param lenghts:
:param name:
:return: scalar loss
"""
with tf.variable_scope('crf_loss' if not name else name):
small_value = -10000.0
start_logits = tf.concat(
[
small_value *
tf.ones(shape=[self.batch_size, 1, self.num_tags]),
tf.zeros(shape=[self.batch_size, 1, 1])
],
axis=-1
)
pad_logits = tf.cast(
small_value *
tf.ones(shape=[self.batch_size, self.num_setps, 1]),
dtype=tf.float32
)
logits = tf.concat(
[project_logits, pad_logits],
axis=-1
)
logits = tf.concat(
[start_logits, logits],
axis=1
)
targets = tf.concat(
[tf.cast(
self.num_tags * tf.ones([self.batch_size, 1]),
tf.int32
),
self.targets
]
,
axis=-1
)
self.trans = tf.get_variable(
"transitions",
shape=[self.num_tags + 1, self.num_tags + 1],
initializer=self.initializer
)
log_likehood, self.trans = crf_log_likelihood(
inputs=logits,
tag_indices=targets,
transition_params=self.trans,
sequence_lengths=lenghts + 1
)
return tf.reduce_mean(-log_likehood)
def decode(self, logits, lengths, matrix):
"""
:param logits: [batch_size,num_steps, num_tags
:param lengths:
:param matrix:
:return:
"""
paths = []
small = -1000.0
start = np.asarray([[small] * self.num_tags + [0]])
for score, length in zip(logits, lengths):
score = score[:length]
pad = small * np.ones([length, 1])
logits = np.concatenate([score, pad], axis=1)
logits = np.concatenate([start, logits], axis=0)
path, _ = viterbi_decode(logits, matrix)
paths.append(path[1:])
return paths
def create_feed_dict(self, is_train, batch):
"""
:param is_train:
:param batch:
:return:
"""
_, words, segs, tags = batch
feed_dict = {
self.word_inputs: np.asarray(words),
self.seg_inputs: np.asarray(segs),
self.dropout: 1.0
}
if is_train:
feed_dict[self.targets] = np.asarray(tags)
feed_dict[self.dropout] = self.config['dropout_keep']
return feed_dict
def run_step(self, sess, is_train, batch):
"""
:param sess:
:param is_train:
:param bath:
:return:
"""
feed_dict = self.create_feed_dict(is_train, batch)
if is_train:
global_step, loss, _ = sess.run(
[self.global_step, self.loss, self.train_op], feed_dict
)
return global_step, loss
else:
lengths, logits = sess.run([self.lengths, self.logits], feed_dict)
return lengths, logits
def evaluate(self, sess, data_manager, id_to_tag):
"""
:param sess:
:param data_manager:
:param id_to_tag:
:return:
"""
results = []
trans = self.trans.eval()
for batch in data_manager.iter_batch():
strings = batch[0]
tags = batch[-1]
lengths, logits = self.run_step(sess, False, batch)
batch_paths = self.decode(logits, lengths, trans)
for i in range(len(strings)):
result = []
string = strings[i][:lengths[i]]
gold = data_utils.bioes_to_bio([id_to_tag[int(x)] for x in tags[i][:lengths[i]]])
pred = data_utils.bioes_to_bio([id_to_tag[int(x)] for x in batch_paths[i][:lengths[i]]])
for char, gold, pred in zip(string, gold, pred):
result.append(" ".join([char, gold, pred]))
results.append(result)
return results