-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseq2seq.py
More file actions
281 lines (248 loc) · 10.1 KB
/
seq2seq.py
File metadata and controls
281 lines (248 loc) · 10.1 KB
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
import tensorflow as tf
import numpy as np
import time
import utils
from embedding_utils import get_GloVe_embeddings
from data import BEGIN_TAG, END_TAG
import beam_search
def gru(units):
if tf.test.is_gpu_available():
return tf.keras.layers.CuDNNGRU(units,
return_sequences=True,
return_state=True)
else:
return tf.keras.layers.GRU(units,
return_sequences=True,
return_state=True,
dropout=0.3,
recurrent_dropout=0.5)
def bilstm(units):
if tf.test.is_gpu_available():
return tf.keras.layers.CuDNNLSTM(units,
return_sequences=True,
return_state=True)
else:
return tf.keras.layers.LSTM(units,
return_sequences=True,
return_state=True,
dropout=0.3,
recurrent_dropout=0.3)
class GloVeEmbedding(tf.keras.Model):
def __init__(
self,
vocab,
embedding_dim=300,
trainable=True):
super(GloVeEmbedding, self).__init__()
self.GloVe = tf.Variable(
get_GloVe_embeddings(vocab, embedding_dim), dtype='float32',
trainable=trainable
)
self.embedding_dim = embedding_dim
def call(self, x):
return tf.nn.embedding_lookup(self.GloVe, x)
class Encoder(tf.keras.Model):
def __init__(
self,
vocab_size,
embedding_dim,
enc_units,
batch_sz,
use_GloVe=False,
inp_lang=None,
use_bilstm=False
):
super(Encoder, self).__init__()
self.batch_sz = batch_sz
self.enc_units = enc_units
if use_GloVe:
self.embedding = GloVeEmbedding(
inp_lang, embedding_dim, trainable=True)
else:
self.embedding = tf.keras.layers.Embedding(
vocab_size, embedding_dim)
self.use_bilstm = use_bilstm
self.encode_model = gru(self.enc_units)
if use_bilstm:
self.encode_model = bilstm(self.enc_units)
def call(self, x, hidden):
x = self.embedding(x)
if self.use_bilstm:
_, state_h, state_c = self.encode_model(x, initial_state=[hidden, hidden])
state = [state_h, state_c]
else:
_, state = self.encode_model(x, initial_state=hidden)
return state
class Decoder(tf.keras.Model):
def __init__(
self,
vocab_size,
embedding_dim,
dec_units,
batch_sz,
use_GloVe=False,
targ_lang=None,
use_bilstm=False
):
super(Decoder, self).__init__()
self.batch_sz = batch_sz
self.dec_units = dec_units
self.vocab_size = vocab_size
if use_GloVe:
self.embedding = GloVeEmbedding(
targ_lang, embedding_dim, trainable=True)
else:
self.embedding = tf.keras.layers.Embedding(
vocab_size, embedding_dim)
self.use_bilstm = use_bilstm
self.decode_model = gru(self.dec_units)
if use_bilstm:
self.decode_model = bilstm(self.dec_units)
self.dropout = tf.keras.layers.Dropout(0.3)
self.fc = tf.keras.layers.Dense(vocab_size)
def call(self, x, hidden):
x = self.embedding(x)
if self.use_bilstm:
output, state_h, state_c = self.decode_model(x, initial_state=hidden)
state = [state_h, state_c]
else:
output, state = self.decode_model(x, initial_state=hidden)
output = self.dropout(output)
x = self.fc(output)
predicts = tf.nn.softmax(x)
return predicts, state
TEACHER_FORCING = "TF"
BASIC = "B"
BEAM_SEARCH = "BS"
class Seq2Seq(tf.keras.Model):
def __init__(
self,
vocab_inp_size,
vocab_tar_size,
embedding_dim,
enc_units,
batch_sz,
inp_lang,
targ_lang,
max_length_tar=100,
use_GloVe=False,
mode=BEAM_SEARCH,
use_bilstm = False,
beam_size = 2
):
super(Seq2Seq, self).__init__()
self.vocab_inp_size = vocab_inp_size
self.vocab_tar_size = vocab_tar_size
self.embedding_dim = embedding_dim
self.batch_sz = batch_sz
self.enc_units = enc_units
self.targ_lang = targ_lang
self.encoder = Encoder(vocab_inp_size, embedding_dim,
enc_units, batch_sz, use_GloVe, inp_lang.vocab, use_bilstm=use_bilstm)
self.decoder = Decoder(vocab_tar_size, embedding_dim,
enc_units, batch_sz, use_GloVe, targ_lang.vocab, use_bilstm=use_bilstm)
self.hidden = tf.zeros((batch_sz, enc_units))
self.max_length_tar = max_length_tar
self.mode = mode
self.beam_size = beam_size
self.use_bilstm = use_bilstm
self.bs = beam_search.BeamSearch(self.beam_size,
self.targ_lang.word2idx[BEGIN_TAG],
self.targ_lang.word2idx[END_TAG],
self.targ_lang,
self.max_length_tar,
self.batch_sz,
self.decoder)
def loss_function(self, real, pred):
# if it's PAD, loss is 0
mask = 1 - np.equal(real, 0)
loss_ = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=real, logits=pred) * mask
return tf.reduce_mean(loss_)
def call(self, inp, targ):
loss = 0
enc_hidden = self.encoder(inp, self.hidden)
dec_hidden = enc_hidden
if self.mode == BEAM_SEARCH:
dec_input = tf.expand_dims(
[self.targ_lang.word2idx[BEGIN_TAG]]*self.batch_sz, 1)
dec_hidden_copy = dec_hidden
for t in range(1, targ.shape[1]):
predictions, dec_hidden = self.decoder(dec_input, dec_hidden)
predictions = tf.squeeze(predictions, axis=1)
labels = []
for i in range(self.batch_sz):
new_input = tf.reshape(dec_input[i], (1, 1))
if self.use_bilstm:
new_dec_hidden = [
tf.reshape(dec_hidden_copy[0][i], (1, self.enc_units)),
tf.reshape(dec_hidden_copy[1][i], (1, self.enc_units))]
best_beam = self.bs.beam_search(new_input, new_dec_hidden, lstm=True)
else:
new_dec_hidden = tf.reshape(dec_hidden_copy[i], (1, self.enc_units))
best_beam = self.bs.beam_search(new_input, new_dec_hidden)
label = best_beam.tokens[1]
labels.append(label)
dec_input = tf.expand_dims(labels, 1)
loss += self.loss_function(targ[:, t], predictions)
return loss
if self.mode == BASIC:
dec_input = tf.expand_dims(
[self.targ_lang.word2idx[BEGIN_TAG]]*self.batch_sz, 1)
for t in range(1, targ.shape[1]):
predictions, dec_hidden = self.decoder(dec_input, dec_hidden)
predictions = tf.squeeze(predictions, axis=1)
dec_input = tf.reshape(tf.argmax(predictions, axis=1), (self.batch_sz, 1))
loss += self.loss_function(targ[:, t], predictions)
return loss
elif self.mode == TEACHER_FORCING:
dec_input = tf.expand_dims(
[self.targ_lang.word2idx[BEGIN_TAG]] * self.batch_sz, 1)
for t in range(1, targ.shape[1]):
# Teacher forcing - feeding the target as the next input
predictions, dec_hidden = self.decoder(dec_input, dec_hidden)
dec_input = tf.expand_dims(targ[:, t], 1)
predictions = tf.squeeze(predictions, axis=1)
loss += self.loss_function(targ[:, t], predictions)
return loss
def evaluate(model: Seq2Seq, eval_dataset):
"""evaluate an epoch."""
total_loss = 0
model.display_result = True
for (batch, (inp, targ)) in enumerate(eval_dataset):
loss = model(inp, targ)
batch_loss = (loss / int(targ.shape[1]))
total_loss += batch_loss
if batch % 100 == 0:
print('batch {} eval loss {:.4f}'.format(batch, total_loss.numpy()))
enc_hidden = model.encoder(inp, model.hidden)
dec_hidden = enc_hidden
dec_input = tf.expand_dims(
[model.targ_lang.word2idx[BEGIN_TAG]] * model.batch_sz , 1)
result = ""
for t in range(model.max_length_tar):
predictions, dec_hidden = model.decoder(dec_input, dec_hidden)
predictions = tf.squeeze(predictions, axis=1)
predicted_id = np.argmax(predictions[0])
result += model.targ_lang.idx2word[predicted_id] + ' '
if model.targ_lang.idx2word[predicted_id] == END_TAG:
print("result: ", result.replace(END_TAG, ""))
#else:
# print(result)
dec_input = tf.expand_dims([predicted_id] * model.batch_sz, 1)
return total_loss
def train(model: Seq2Seq, optimizer, train_dataset):
"""training an epoch."""
total_loss = 0
for (batch, (inp, targ)) in enumerate(train_dataset):
with tf.GradientTape() as tape:
loss = model(inp, targ)
batch_loss = (loss / int(targ.shape[1]))
total_loss += batch_loss
variables = model.encoder.variables + model.decoder.variables
gradients = tape.gradient(loss, variables)
optimizer.apply_gradients(zip(gradients, variables))
if batch % 100 == 0:
print('batch {} training loss {:.4f}'.format(
batch, total_loss.numpy()))
return total_loss