-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder_decoder.py
249 lines (224 loc) · 9 KB
/
encoder_decoder.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
import tensorflow as tf
#import matplotlib.pyplot as plt
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,
recurrent_initializer='glorot_uniform')
else:
return tf.keras.layers.GRU(units,
return_sequences=True,
return_state=True,
recurrent_activation='sigmoid',
recurrent_initializer='glorot_uniform')
def bilstm(units):
if tf.test.is_gpu_available():
return tf.keras.layers.Bidirectional(tf.keras.layers.CuDNNLSTM(units,
return_sequences=True,
return_state=True,
dropout=0.25,
recurrent_dropout=0.25))
else:
return tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units,
return_sequences=True,
return_state=True,
dropout=0.25,
recurrent_dropout=0.25,
recurrent_activation='sigmoid'))
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.gru = gru(self.enc_units)
if use_bilstm:
self.bilstm = bilstm(self.enc_units)
def call(self, x, hidden):
x = self.embedding(x)
if self.use_bilstm:
_, forward_h, forward_c, backward_h, backward_c = self.bilstm(
x, initial_state=hidden)
state_h = tf.keras.layers.Concatenate([forward_h, backward_h])
state_c = tf.keras.layers.Concatenate([forward_c, backward_c])
state = [state_h, state_c]
else:
_, state = self.gru(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.gru = gru(self.dec_units)
self.gru2 = gru(self.dec_units)
if use_bilstm:
self.bilstm = bilstm(self.dec_units)
self.fc = tf.keras.layers.Dense(vocab_size)
def call(self, x, hidden):
x = self.embedding(x)
if self.use_bilstm:
output, forward_h, forward_c, backward_h, backward_c = self.bilstm(
x, initial_state=hidden)
state_h = tf.keras.layers.Concatenate([forward_h, backward_h])
state_c = tf.keras.layers.Concatenate([forward_c, backward_c])
state = [state_h, state_c]
else:
output, state = self.gru(x, initial_state=hidden)
output, state = self.gru(x, initial_state=hidden)
x = self.fc(output)
x = tf.reshape(x, [x.shape[0], self.vocab_size])
# predicts = x
predicts = tf.nn.softmax(x)
return predicts, state
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,
use_GloVe=False,
display_result=False,
beam_size=3,
use_beam_search=False
):
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)
self.decoder = Decoder(vocab_tar_size, embedding_dim,
enc_units, batch_sz, use_GloVe, targ_lang.vocab)
self.hidden = tf.zeros((batch_sz, enc_units))
self.max_length_tar = max_length_tar
self.display_result = display_result
self.use_beam_search = use_beam_search
self.beam_size = beam_size
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
dec_input = tf.expand_dims(
[self.targ_lang.word2idx[BEGIN_TAG]] * self.batch_sz, 1)
result = ''
if self.use_beam_search:
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)
for t in range(1, targ.shape[1]):
if self.use_beam_search:
predictions, _ = self.decoder(dec_input, dec_hidden)
best_beam = bs.beam_search(dec_input, dec_hidden)
loss += tf.reduce_mean(best_beam.log_prob)
predicted_id = tf.argmax(predictions[0]).numpy()
else:
# Teacher forcing - feeding the target as the next input
predictions, dec_hidden = self.decoder(dec_input, dec_hidden)
predicted_id = tf.argmax(predictions[0]).numpy()
loss += self.loss_function(targ[:, t], predictions)
dec_input = tf.expand_dims(targ[:, t], 1)
if self.display_result and self.targ_lang.idx2word[predicted_id] == END_TAG:
print("result: ", result)
if self.targ_lang.idx2word[predicted_id] == END_TAG:
return loss
result += ' ' + self.targ_lang.idx2word[predicted_id]
#print(result)
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()))
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