-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.py
194 lines (148 loc) · 7.48 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
# -*- coding: utf-8 -*-
import tensorflow as tf
class Encoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, units, batch_size, dropout):
"""
Args:
vocab_size: input language vocabulary
embedding_dim: embeddig dimension
units: units
batch_size: batch size
Raises:
"""
super(Encoder, self).__init__()
self.batch_size = batch_size
self.enc_units = units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.lstm_1 = tf.keras.layers.LSTM(self.enc_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.lstm_2 = tf.keras.layers.LSTM(self.enc_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.lstm_3 = tf.keras.layers.LSTM(self.enc_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.lstm_4 = tf.keras.layers.LSTM(self.enc_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
def call(self, x, pre_state):
"""
Args:
x: input tensor
pre_state: initial state in LSTM
Returns:
output:
state: hidden state and cell state used for next steps
Raises:
"""
x = self.embedding(x)
x, state_h_1, state_c_1 = self.lstm_1(x, initial_state=pre_state[0])
x, state_h_2, state_c_2 = self.lstm_2(x, initial_state=pre_state[1])
x, state_h_3, state_c_3 = self.lstm_3(x, initial_state=pre_state[2])
output, state_h_4, state_c_4 = self.lstm_4(x, initial_state=pre_state[3])
state = [[state_h_1, state_c_1], [state_h_2, state_c_2], [state_h_3, state_c_3], [state_h_4, state_c_4]]
return output, state
def initialize_hidden_state(self):
return tf.zeros((self.batch_size, self.enc_units))
def initialize_cell_state(self):
return tf.zeros((self.batch_size, self.enc_units))
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, units, method, batch_size, dropout):
"""
Args:
vocab_size: target language vocabulary
embedding_dim: embeddig dimension
units: units
batch_size: batch size
"""
super(Decoder, self).__init__()
self.batch_size = batch_size
self.dec_units = units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.lstm_1 = tf.keras.layers.LSTM(self.dec_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.lstm_2 = tf.keras.layers.LSTM(self.dec_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.lstm_3 = tf.keras.layers.LSTM(self.dec_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.lstm_4 = tf.keras.layers.LSTM(self.dec_units,
return_sequences=True,
return_state=True,
dropout=dropout,
recurrent_initializer='glorot_uniform')
self.attention_layer = AttentionLayer(units, method)
self.W_c = tf.keras.layers.Dense(embedding_dim, activation='tanh')
self.W_s = tf.keras.layers.Dense(vocab_size)
def call(self, x, pre_state, enc_output, pre_h_t):
x = self.embedding(x)
# input_feeding shape == (batch_size, 1, word_embedding_dim + pre_h_t_embedding_dim)
x = tf.concat([x, pre_h_t], axis=-1)
x, state_h_1, state_c_1 = self.lstm_1(x, initial_state=pre_state[0])
x, state_h_2, state_c_2 = self.lstm_2(x, initial_state=pre_state[1])
x, state_h_3, state_c_3 = self.lstm_3(x, initial_state=pre_state[2])
# dec_output shape == (batch_size, 1, units)
dec_output, state_h_4, state_c_4 = self.lstm_4(x, initial_state=pre_state[3])
state = [[state_h_1, state_c_1], [state_h_2, state_c_2], [state_h_3, state_c_3], [state_h_4, state_c_4]]
context_vector = self.attention_layer(dec_output, enc_output)
# h_t shape == (batch_size, 1, embedding_dim)
h_t = self.W_c(tf.concat([tf.expand_dims(context_vector, 1), dec_output], axis=-1))
#h_t = self.W_c(tf.concat([context_vector, tf.squeeze(dec_output)], axis=-1))
# y_t shape == (batch_size, vocab_size)
y_t = tf.squeeze(self.W_s(h_t), axis=1)
return y_t, state, h_t
class AttentionLayer(tf.keras.Model):
def __init__(self, units, method='concat'):
super(AttentionLayer, self).__init__()
# TODO: Three types of score function
self.method = method
self.W_a = tf.keras.layers.Dense(units)
self.v_a = tf.keras.layers.Dense(1)
def call(self, dec_h_t, enc_h_s):
"""
Args:
dec_h_t: current target state (batch_size, 1, units)
enc_h_s: all source states (batch_size, seq_len, units)
Returns:
context_vector: (batch_size, units)
"""
# concat_h = tf.concat([dec_h_t, enc_h_s], axis=1)
# concat_h = tf.reshape(concat_h, [concat_h.shape[0] * concat_h.shape[1], concat_h.shape[2]])
# print('concat_h shape:', concat_h.shape)
# score shape == (batch_size, seq_len, 1)
if self.method == 'concat':
score = self.v_a(tf.nn.tanh(self.W_a(dec_h_t + enc_h_s)))
elif self.method == 'general':
score = tf.matmul(self.W_a(enc_h_s), dec_h_t, transpose_b=True)
elif self.method == 'dot':
score = tf.matmul(enc_h_s, dec_h_t, transpose_b=True)
# a_t shape == (batch_size, seq_len, 1)
a_t = tf.nn.softmax(score, axis=1)
# TODO: replace matmul operator with multiply operator
# tf.matmul(a_t, enc_h_s, transpose_a=True) -> a_t * enc_h_s
# result shape after * operation: (batch_size, seq_len, units)
# (batch_size, 1, units)
# context_vector shape == (batch_size, units)
context_vector = tf.reduce_sum(a_t * enc_h_s, axis=1)
return context_vector
def main():
pass
if __name__=='__main__':
main()