-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc_dec_utils.py
79 lines (63 loc) · 2.82 KB
/
enc_dec_utils.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
import tensorflow as tf
class Encoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, units, batch_size,
batch_norm=False):
super(Encoder, self).__init__()
self.batch_size = batch_size
self.units = units
self.batch_norm = batch_norm
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.norm = tf.keras.layers.BatchNormalization()
self.gru = tf.keras.layers.GRU(self.units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
def call(self, x, hidden):
x = self.embedding(x)
if self.batch_norm:
x = self.norm(x)
output, state = self.gru(x, initial_state=hidden)
return output, state
def initialize_hidden_state(self):
return tf.zeros((self.batch_size, self.units))
class Attention(tf.keras.layers.Layer):
def __init__(self, units):
super(Attention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
def call(self, query, values):
query_time = tf.expand_dims(query, axis=1)
score = self.V(tf.nn.tanh(
self.W1(query_time) + self.W2(values)
))
attention_weights = tf.nn.softmax(score, axis=1)
context_vector = attention_weights * values
context_vector = tf.reduce_sum(context_vector, axis=1)
return context_vector, attention_weights
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, units, batch_size,
batch_norm=False):
super(Decoder, self).__init__()
self.batch_size = batch_size
self.units = units
self.batch_norm = batch_norm
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.norm = tf.keras.layers.BatchNormalization()
self.gru = tf.keras.layers.GRU(self.units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
self.fc = tf.keras.layers.Dense(vocab_size)
self.attention = Attention(self.units)
def call(self, x, hidden, enc_output):
context_vector, attention_weights = self.attention(hidden, enc_output)
x = self.embedding(x)
if self.batch_norm:
x = self.norm(x)
x = tf.concat([tf.expand_dims(context_vector, axis=1), x],
axis=-1)
output, state = self.gru(x)
output = tf.reshape(output, (-1, output.shape[2]))
x = self.fc(output)
return x, state, attention_weights