-
Notifications
You must be signed in to change notification settings - Fork 98
/
model.py
196 lines (174 loc) · 10.3 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
# coding=utf-8
# @author: cer
import tensorflow as tf
from tensorflow.contrib import layers
import numpy as np
from tensorflow.contrib.rnn import LSTMCell, LSTMStateTuple,DropoutWrapper
import sys
class Model:
def __init__(self, input_steps, embedding_size, hidden_size, vocab_size, slot_size,
intent_size, epoch_num, batch_size=16, n_layers=1):
self.input_steps = input_steps
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.n_layers = n_layers
self.batch_size = batch_size
self.vocab_size = vocab_size
self.slot_size = slot_size
self.intent_size = intent_size
self.epoch_num = epoch_num
self.encoder_inputs = tf.placeholder(tf.int32, [input_steps, batch_size],
name='encoder_inputs')
# 每句输入的实际长度,除了padding
self.encoder_inputs_actual_length = tf.placeholder(tf.int32, [batch_size],
name='encoder_inputs_actual_length')
self.decoder_targets = tf.placeholder(tf.int32, [batch_size, input_steps],
name='decoder_targets')
self.intent_targets = tf.placeholder(tf.int32, [batch_size],
name='intent_targets')
def build(self):
self.embeddings = tf.Variable(tf.random_uniform([self.vocab_size, self.embedding_size],
-0.1, 0.1), dtype=tf.float32, name="embedding")
self.encoder_inputs_embedded = tf.nn.embedding_lookup(self.embeddings, self.encoder_inputs)
# Encoder
# 使用单个LSTM cell
encoder_f_cell_0 = LSTMCell(self.hidden_size)
encoder_b_cell_0 = LSTMCell(self.hidden_size)
encoder_f_cell = DropoutWrapper(encoder_f_cell_0,output_keep_prob=0.5)
encoder_b_cell = DropoutWrapper(encoder_b_cell_0,output_keep_prob=0.5)
# encoder_inputs_time_major = tf.transpose(self.encoder_inputs_embedded, perm=[1, 0, 2])
# 下面四个变量的尺寸:T*B*D,T*B*D,B*D,B*D
(encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_final_state, encoder_bw_final_state) = \
tf.nn.bidirectional_dynamic_rnn(cell_fw=encoder_f_cell,
cell_bw=encoder_b_cell,
inputs=self.encoder_inputs_embedded,
sequence_length=self.encoder_inputs_actual_length,
dtype=tf.float32, time_major=True)
encoder_outputs = tf.concat((encoder_fw_outputs, encoder_bw_outputs), 2)
encoder_final_state_c = tf.concat(
(encoder_fw_final_state.c, encoder_bw_final_state.c), 1)
encoder_final_state_h = tf.concat(
(encoder_fw_final_state.h, encoder_bw_final_state.h), 1)
self.encoder_final_state = LSTMStateTuple(
c=encoder_final_state_c,
h=encoder_final_state_h
)
print("encoder_outputs: ", encoder_outputs)
print("encoder_outputs[0]: ", encoder_outputs[0])
print("encoder_final_state_c: ", encoder_final_state_c)
# Decoder
decoder_lengths = self.encoder_inputs_actual_length
self.slot_W = tf.Variable(tf.random_uniform([self.hidden_size * 2, self.slot_size], -1, 1),
dtype=tf.float32, name="slot_W")
self.slot_b = tf.Variable(tf.zeros([self.slot_size]), dtype=tf.float32, name="slot_b")
intent_W = tf.Variable(tf.random_uniform([self.hidden_size * 2, self.intent_size], -0.1, 0.1),
dtype=tf.float32, name="intent_W")
intent_b = tf.Variable(tf.zeros([self.intent_size]), dtype=tf.float32, name="intent_b")
# 求intent
intent_logits = tf.add(tf.matmul(encoder_final_state_h, intent_W), intent_b)
# intent_prob = tf.nn.softmax(intent_logits)
self.intent = tf.argmax(intent_logits, axis=1)
sos_time_slice = tf.ones([self.batch_size], dtype=tf.int32, name='SOS') * 2
sos_step_embedded = tf.nn.embedding_lookup(self.embeddings, sos_time_slice)
# pad_time_slice = tf.zeros([self.batch_size], dtype=tf.int32, name='PAD')
# pad_step_embedded = tf.nn.embedding_lookup(self.embeddings, pad_time_slice)
pad_step_embedded = tf.zeros([self.batch_size, self.hidden_size*2+self.embedding_size],
dtype=tf.float32)
def initial_fn():
initial_elements_finished = (0 >= decoder_lengths) # all False at the initial step
initial_input = tf.concat((sos_step_embedded, encoder_outputs[0]), 1)
return initial_elements_finished, initial_input
def sample_fn(time, outputs, state):
# 选择logit最大的下标作为sample
print("outputs", outputs)
# output_logits = tf.add(tf.matmul(outputs, self.slot_W), self.slot_b)
# print("slot output_logits: ", output_logits)
# prediction_id = tf.argmax(output_logits, axis=1)
prediction_id = tf.to_int32(tf.argmax(outputs, axis=1))
return prediction_id
def next_inputs_fn(time, outputs, state, sample_ids):
# 上一个时间节点上的输出类别,获取embedding再作为下一个时间节点的输入
pred_embedding = tf.nn.embedding_lookup(self.embeddings, sample_ids)
# 输入是h_i+o_{i-1}+c_i
next_input = tf.concat((pred_embedding, encoder_outputs[time]), 1)
elements_finished = (time >= decoder_lengths) # this operation produces boolean tensor of [batch_size]
all_finished = tf.reduce_all(elements_finished) # -> boolean scalar
next_inputs = tf.cond(all_finished, lambda: pad_step_embedded, lambda: next_input)
next_state = state
return elements_finished, next_inputs, next_state
my_helper = tf.contrib.seq2seq.CustomHelper(initial_fn, sample_fn, next_inputs_fn)
def decode(helper, scope, reuse=None):
with tf.variable_scope(scope, reuse=reuse):
memory = tf.transpose(encoder_outputs, [1, 0, 2])
attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(
num_units=self.hidden_size, memory=memory,
memory_sequence_length=self.encoder_inputs_actual_length)
cell = tf.contrib.rnn.LSTMCell(num_units=self.hidden_size * 2)
attn_cell = tf.contrib.seq2seq.AttentionWrapper(
cell, attention_mechanism, attention_layer_size=self.hidden_size)
out_cell = tf.contrib.rnn.OutputProjectionWrapper(
attn_cell, self.slot_size, reuse=reuse
)
decoder = tf.contrib.seq2seq.BasicDecoder(
cell=out_cell, helper=helper,
initial_state=out_cell.zero_state(
dtype=tf.float32, batch_size=self.batch_size))
# initial_state=encoder_final_state)
final_outputs, final_state, final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(
decoder=decoder, output_time_major=True,
impute_finished=True, maximum_iterations=self.input_steps
)
return final_outputs
outputs = decode(my_helper, 'decode')
print("outputs: ", outputs)
print("outputs.rnn_output: ", outputs.rnn_output)
print("outputs.sample_id: ", outputs.sample_id)
# weights = tf.to_float(tf.not_equal(outputs[:, :-1], 0))
self.decoder_prediction = outputs.sample_id
decoder_max_steps, decoder_batch_size, decoder_dim = tf.unstack(tf.shape(outputs.rnn_output))
self.decoder_targets_time_majored = tf.transpose(self.decoder_targets, [1, 0])
self.decoder_targets_true_length = self.decoder_targets_time_majored[:decoder_max_steps]
print("decoder_targets_true_length: ", self.decoder_targets_true_length)
# 定义mask,使padding不计入loss计算
self.mask = tf.to_float(tf.not_equal(self.decoder_targets_true_length, 0))
# 定义slot标注的损失
loss_slot = tf.contrib.seq2seq.sequence_loss(
outputs.rnn_output, self.decoder_targets_true_length, weights=self.mask)
# 定义intent分类的损失
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
labels=tf.one_hot(self.intent_targets, depth=self.intent_size, dtype=tf.float32),
logits=intent_logits)
loss_intent = tf.reduce_mean(cross_entropy)
self.loss = loss_slot + loss_intent
optimizer = tf.train.AdamOptimizer(name="a_optimizer")
self.grads, self.vars = zip(*optimizer.compute_gradients(self.loss))
print("vars for loss function: ", self.vars)
self.gradients, _ = tf.clip_by_global_norm(self.grads, 5) # clip gradients
self.train_op = optimizer.apply_gradients(zip(self.gradients, self.vars))
# self.train_op = optimizer.minimize(self.loss)
# train_op = layers.optimize_loss(
# loss, tf.train.get_global_step(),
# optimizer=optimizer,
# learning_rate=0.001,
# summaries=['loss', 'learning_rate'])
def step(self, sess, mode, trarin_batch):
""" perform each batch"""
if mode not in ['train', 'test']:
print >> sys.stderr, 'mode is not supported'
sys.exit(1)
unziped = list(zip(*trarin_batch))
# print(np.shape(unziped[0]), np.shape(unziped[1]),
# np.shape(unziped[2]), np.shape(unziped[3]))
if mode == 'train':
output_feeds = [self.train_op, self.loss, self.decoder_prediction,
self.intent, self.mask, self.slot_W]
feed_dict = {self.encoder_inputs: np.transpose(unziped[0], [1, 0]),
self.encoder_inputs_actual_length: unziped[1],
self.decoder_targets: unziped[2],
self.intent_targets: unziped[3]}
if mode in ['test']:
output_feeds = [self.decoder_prediction, self.intent]
feed_dict = {self.encoder_inputs: np.transpose(unziped[0], [1, 0]),
self.encoder_inputs_actual_length: unziped[1]}
results = sess.run(output_feeds, feed_dict=feed_dict)
return results