forked from DongjunLee/conversation-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
325 lines (256 loc) · 13.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from __future__ import print_function
from hbconfig import Config
import tensorflow as tf
from tensorflow.contrib import layers
from tensorflow.python.layers import core as layers_core
from seq2seq.encoder import Encoder
class Conversation:
def __init__(self):
pass
def model_fn(self, mode, features, labels, params):
self.dtype = tf.float32
self.mode = mode
self.params = params
self._init_placeholder(features, labels)
self.build_graph()
if self.mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions={"prediction": self.prediction})
else:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=self.decoder_train_pred,
loss=self.loss,
train_op=self.train_op
)
def _init_placeholder(self, features, labels):
self.encoder_input = features
if type(features) == dict:
self.encoder_input = features["input_data"]
self.encoder_input_lengths = tf.reduce_sum(
tf.to_int32(tf.not_equal(self.encoder_input, Config.data.PAD_ID)), 1,
name="encoder_input_lengths")
if self.mode != tf.estimator.ModeKeys.PREDICT:
self.decoder_input = labels
self.decoder_input_lengths = tf.reduce_sum(
tf.to_int32(tf.not_equal(self.decoder_input, Config.data.PAD_ID)), 1,
name="decoder_input_lengths")
decoder_input_shift_1 = tf.slice(self.decoder_input,
[0, 1], [Config.train.batch_size, Config.data.max_seq_length-1])
pad_tokens = tf.zeros([Config.train.batch_size, 1], dtype=tf.int32)
self.targets = tf.concat([decoder_input_shift_1, pad_tokens], axis=1)
def build_graph(self):
self._build_embed()
self._build_encoder()
self._build_decoder()
if self.mode != tf.estimator.ModeKeys.PREDICT:
self._build_loss()
self._build_optimizer()
def _build_embed(self):
with tf.variable_scope ("embeddings", dtype=self.dtype) as scope:
if Config.model.embed_share:
embedding = tf.get_variable(
"embedding_share", [Config.data.vocab_size, Config.model.embed_dim], self.dtype)
self.embedding_encoder = embedding
self.embedding_decoder = embedding
else:
self.embedding_encoder = tf.get_variable(
"embedding_encoder", [Config.data.vocab_size, Config.model.embed_dim], self.dtype)
self.embedding_decoder = tf.get_variable(
"embedding_decoder", [Config.data.vocab_size, Config.model.embed_dim], self.dtype)
self.encoder_emb_inp = tf.nn.embedding_lookup(
self.embedding_encoder, self.encoder_input)
if self.mode != tf.estimator.ModeKeys.PREDICT:
self.decoder_emb_inp = tf.nn.embedding_lookup(
self.embedding_decoder, self.decoder_input)
def _build_encoder(self):
with tf.variable_scope('encoder'):
encoder = Encoder(
encoder_type=Config.model.encoder_type,
num_layers=Config.model.num_layers,
cell_type=Config.model.cell_type,
num_units=Config.model.num_units,
dropout=Config.model.dropout)
self.encoder_outputs, self.encoder_final_state = encoder.build(
input_vector=self.encoder_emb_inp,
sequence_length=self.encoder_input_lengths)
beam_width = Config.predict.get('beam_width', 0)
if self.mode == tf.estimator.ModeKeys.PREDICT and beam_width > 0:
self.encoder_outputs = tf.contrib.seq2seq.tile_batch(self.encoder_outputs, beam_width)
self.encoder_input_lengths = tf.contrib.seq2seq.tile_batch(self.encoder_input_lengths, beam_width)
def _build_projection(self):
with tf.variable_scope("decoder/output_projection"):
self.output_layer = layers_core.Dense(
Config.data.vocab_size, use_bias=False, name="output_projection")
def _create_attention_mechanism(self):
num_units = Config.model.num_units
if Config.model.encoder_type == "bi" and "luong" in Config.model.attention_mechanism:
num_units *= 2
memory = self.encoder_outputs
if Config.model.attention_mechanism == "bahdanau":
attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(
num_units,
memory,
memory_sequence_length=self.encoder_input_lengths)
elif Config.model.attention_mechanism == "normed_bahdanau":
attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(
num_units,
memory,
memory_sequence_length=self.encoder_input_lengths,
normalize=True)
elif Config.model.attention_mechanism == "luong":
attention_mechanism = tf.contrib.seq2seq.LuongAttention(
num_units * 2,
memory,
memory_sequence_length=self.encoder_input_lengths)
elif Config.model.attention_mechanism == "scaled_luong":
attention_mechanism = tf.contrib.seq2seq.LuongAttention(
num_units,
memory,
memory_sequence_length=self.encoder_input_lengths,
scale=True)
else:
raise ValueError(f"Unknown attention mechanism {Config.model.attention_mechanism}")
return attention_mechanism
def _build_decoder(self):
def decode(helper=None, scope="decode"):
with tf.variable_scope(scope):
attention_mechanism = self._create_attention_mechanism()
if Config.model.encoder_type == "UNI":
cells = self._build_rnn_cells(Config.model.num_units)
attention_layer_size = Config.model.num_units
elif Config.model.encoder_type == "BI":
cells = self._build_rnn_cells(Config.model.num_units * 2)
attention_layer_size = Config.model.num_units * 2
else:
raise ValueError(f"Unknown encoder_type {Config.model.encoder_type}")
beam_width = Config.predict.get('beam_width', 0)
alignment_history = (self.mode == tf.estimator.ModeKeys.PREDICT and beam_width == 0)
attn_cell = tf.contrib.seq2seq.AttentionWrapper(
cells,
attention_mechanism,
attention_layer_size=attention_layer_size,
alignment_history=alignment_history,
name="attention")
out_cell = tf.contrib.rnn.OutputProjectionWrapper(
attn_cell, Config.data.vocab_size)
if self.mode == tf.estimator.ModeKeys.PREDICT:
maximum_iterations = tf.round(tf.reduce_max(self.encoder_input_lengths) * 2)
if helper is None:
decoder_start_state = tf.contrib.seq2seq.tile_batch(self.encoder_final_state, beam_width)
decoder_initial_state = out_cell.zero_state(Config.train.batch_size * beam_width, self.dtype)
decoder_initial_state.clone(cell_state=decoder_start_state)
decoder = tf.contrib.seq2seq.BeamSearchDecoder(
cell=out_cell,
embedding=self.embedding_decoder,
start_tokens=tf.fill([Config.train.batch_size], Config.data.START_ID),
end_token=Config.data.EOS_ID,
initial_state=(decoder_initial_state),
beam_width=Config.predict.beam_width,
length_penalty_weight=Config.predict.length_penalty_weight)
outputs = tf.contrib.seq2seq.dynamic_decode(
decoder=decoder,
output_time_major=False,
impute_finished=False,
maximum_iterations=maximum_iterations)
else:
decoder_initial_state = out_cell.zero_state(Config.train.batch_size, self.dtype)
decoder_initial_state.clone(cell_state=self.encoder_final_state)
decoder = tf.contrib.seq2seq.BasicDecoder(
cell=out_cell,
helper=helper,
initial_state=(decoder_initial_state))
outputs = tf.contrib.seq2seq.dynamic_decode(
decoder=decoder,
output_time_major=False,
impute_finished=True,
maximum_iterations=maximum_iterations)
else:
decoder_initial_state = out_cell.zero_state(Config.train.batch_size, self.dtype)
decoder_initial_state.clone(cell_state=self.encoder_final_state)
decoder = tf.contrib.seq2seq.BasicDecoder(
cell=out_cell,
helper=helper,
initial_state=(decoder_initial_state))
outputs = tf.contrib.seq2seq.dynamic_decode(
decoder=decoder,
output_time_major=False,
swap_memory=True)
return outputs[0]
with tf.variable_scope('decoder'):
if self.mode == tf.estimator.ModeKeys.PREDICT:
beam_width = Config.predict.get('beam_width', 0)
if beam_width > 0:
self.decoder_pred_outputs = decode()
self.prediction = self.decoder_pred_outputs.predicted_ids
else:
self.pred_helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(
embedding=self.embedding_decoder,
start_tokens=tf.fill([Config.train.batch_size], Config.data.START_ID),
end_token=Config.data.EOS_ID)
self.decoder_pred_outputs = decode(helper=self.pred_helper)
self.prediction = self.decoder_pred_outputs.sample_id
else:
self.train_helper = tf.contrib.seq2seq.TrainingHelper(
inputs=self.decoder_emb_inp,
sequence_length=self.decoder_input_lengths)
self.decoder_train_outputs = decode(self.train_helper, 'decode')
self.decoder_train_logits = self.decoder_train_outputs.rnn_output
if self.mode != tf.estimator.ModeKeys.PREDICT:
self.decoder_train_pred = tf.argmax(self.decoder_train_logits[0], axis=1, name='train/pred_0')
def _build_rnn_cells(self, num_units, is_list=False):
stacked_rnn = []
for _ in range(Config.model.num_layers):
single_cell = self._single_cell(Config.model.cell_type, Config.model.dropout,num_units)
stacked_rnn.append(single_cell)
if is_list:
return stacked_rnn
else:
return tf.nn.rnn_cell.MultiRNNCell(
cells=stacked_rnn,
state_is_tuple=True)
def _single_cell(self, cell_type, dropout, num_units):
if cell_type == "GRU":
single_cell = tf.contrib.rnn.GRUCell(
num_units)
elif cell_type == "LSTM":
single_cell = tf.contrib.rnn.BasicLSTMCell(
num_units,
forget_bias=1.0)
elif cell_type == "LAYER_NORM_LSTM":
single_cell = tf.contrib.rnn.LayerNormBasicLSTMCell(
num_units,
forget_bias=1.0,
layer_norm=True)
elif cell_type == "NAS":
single_cell = tf.contrib.rnn.LayerNormBasicLSTMCell(
num_units)
else:
raise ValueError(f"Unknown rnn cell type. {cell_type}")
if dropout > 0.0:
single_cell = tf.contrib.rnn.DropoutWrapper(
cell=single_cell, input_keep_prob=(1.0 - dropout))
return single_cell
def _build_loss(self):
pad_num = Config.data.max_seq_length - tf.shape(self.decoder_train_logits)[1]
zero_padding = tf.zeros(
[Config.train.batch_size, pad_num, Config.data.vocab_size],
dtype=tf.float32)
zero_padding_logits = tf.concat([self.decoder_train_logits, zero_padding], axis=1)
weight_masks = tf.sequence_mask(
lengths=self.decoder_input_lengths,
maxlen=Config.data.max_seq_length,
dtype=tf.float32, name='masks')
self.loss = tf.contrib.seq2seq.sequence_loss(
logits=zero_padding_logits,
targets=self.targets,
weights=weight_masks,
name="loss")
def _build_optimizer(self):
self.train_op = layers.optimize_loss(
self.loss, tf.train.get_global_step(),
optimizer='Adam',
learning_rate=Config.train.learning_rate,
summaries=['loss', 'learning_rate'],
name="train_op")