-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRU_encoder_decoder.py
274 lines (224 loc) · 10.5 KB
/
GRU_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
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class GRU(nn.Module):
def __init__(
self,
input_size,
hidden_size
):
super(GRU, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.w_ir = nn.Parameter(torch.empty(hidden_size, input_size))
self.w_iz = nn.Parameter(torch.empty(hidden_size, input_size))
self.w_in = nn.Parameter(torch.empty(hidden_size, input_size))
self.b_ir = nn.Parameter(torch.empty(hidden_size))
self.b_iz = nn.Parameter(torch.empty(hidden_size))
self.b_in = nn.Parameter(torch.empty(hidden_size))
self.w_hr = nn.Parameter(torch.empty(hidden_size, hidden_size))
self.w_hz = nn.Parameter(torch.empty(hidden_size, hidden_size))
self.w_hn = nn.Parameter(torch.empty(hidden_size, hidden_size))
self.b_hr = nn.Parameter(torch.empty(hidden_size))
self.b_hz = nn.Parameter(torch.empty(hidden_size))
self.b_hn = nn.Parameter(torch.empty(hidden_size))
for param in self.parameters():
nn.init.uniform_(param, a=-(1/hidden_size)**0.5, b=(1/hidden_size)**0.5)
def forward(self, inputs, hidden_states):
"""GRU.
This is a Gated Recurrent Unit
Parameters
----------
inputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, input_size)`)
The input tensor containing the embedded sequences. input_size corresponds to embedding size.
hidden_states (`torch.FloatTensor` of shape `(1, batch_size, hidden_size)`)
The (initial) hidden state.
Returns
-------
outputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`)
A feature tensor encoding the input sentence.
hidden_states (`torch.FloatTensor` of shape `(1, batch_size, hidden_size)`)
The final hidden state.
"""
outputs = torch.empty(inputs.shape[0], inputs.shape[1], self.hidden_size, device=inputs.device)
for i in range(inputs.shape[1]):
xt = inputs[:, i, :]
rt = torch.sigmoid(xt @ torch.transpose(self.w_ir, 0, 1) + self.b_ir + hidden_states @ torch.transpose(self.w_hr, 0, 1) + self.b_hr)
zt = torch.sigmoid(xt @ torch.transpose(self.w_iz, 0, 1) + self.b_iz + hidden_states @ torch.transpose(self.w_hz, 0, 1) + self.b_hz)
nt = torch.tanh(xt @ torch.transpose(self.w_in, 0, 1) + self.b_in + rt * (hidden_states @ torch.transpose(self.w_hn, 0, 1) + self.b_hn))
ht = (1 - zt) * nt + zt * hidden_states
outputs[:, i, :] = ht
hidden_states = ht
return outputs, hidden_states
class Attn(nn.Module):
def __init__(
self,
hidden_size=256,
):
super(Attn, self).__init__()
self.hidden_size = hidden_size
self.W = nn.Linear(hidden_size*2, hidden_size)
self.V = nn.Linear(hidden_size, hidden_size)
self.tanh = nn.Tanh()
self.softmax = nn.Softmax(dim=1)
def forward(self, inputs, hidden_states, mask = None):
"""Soft Attention mechanism.
This is a one layer MLP network that implements Soft (i.e. Bahdanau) Attention with masking
Parameters
----------
inputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`)
The input tensor containing the embedded sequences.
hidden_states (`torch.FloatTensor` of shape `(num_layers, batch_size, hidden_size)`)
The (initial) hidden state.
mask ( optional `torch.LongTensor` of shape `(batch_size, sequence_length)`)
The masked tensor containing the location of padding in the sequences.
Returns
-------
outputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`)
A feature tensor encoding the input sentence with attention applied.
x_attn (`torch.FloatTensor` of shape `(batch_size, sequence_length, 1)`)
The attention vector.
"""
hidden_states = hidden_states.transpose(0, 1).repeat(1, inputs.shape[1], 1) # (batch_size, seq_len, hidden_size)
x_attn = torch.cat([inputs, hidden_states], dim=2) # (batch_size, seq_len, hidden_size*2)
x_attn = self.W(x_attn)
x_attn = self.tanh(x_attn)
x_attn = self.V(x_attn)
x_attn = torch.sum(x_attn, dim=-1, keepdim=True)
x_attn = self.softmax(x_attn)
if mask is not None:
x_attn = x_attn * mask.unsqueeze(-1)
x_attn_sum = x_attn.sum(dim=-2, keepdim=True)
x_attn = x_attn / x_attn_sum
outputs = x_attn * inputs
return outputs, x_attn
class Encoder(nn.Module):
def __init__(
self,
vocabulary_size=30522,
embedding_size=256,
hidden_size=256,
num_layers=1,
dropout=0.0
):
super(Encoder, self).__init__()
self.vocabulary_size = vocabulary_size
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.embedding = nn.Embedding(
vocabulary_size, embedding_size, padding_idx=0,
)
self.dropout = nn.Dropout(p=dropout)
self.rnn = nn.GRU(embedding_size, hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True)
def forward(self, inputs, hidden_states):
"""GRU Encoder.
This is a Bidirectional Gated Recurrent Unit Encoder network
Parameters
----------
inputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, vocabulary_size)`)
The input tensor containing the token sequences.
hidden_states(`torch.FloatTensor` of shape `(num_layers*2, batch_size, hidden_size)`)
The (initial) hidden state for the bidrectional GRU.
Returns
-------
outputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`)
A feature tensor encoding the input sentence.
hidden_states (`torch.FloatTensor` of shape `(num_layers, batch_size, hidden_size)`)
The final hidden state.
"""
x = self.embedding(inputs)
x = self.dropout(x)
x, hidden_states = self.rnn(x, hidden_states)
outputs = x[:, :, :self.hidden_size] + x[:, :, self.hidden_size:]
hidden_states = hidden_states.view(self.num_layers, 2, -1, self.hidden_size)
hidden_states = hidden_states[:, 0, :, :] + hidden_states[:, 1, :, :]
return outputs, hidden_states
def initial_states(self, batch_size, device=None):
if device is None:
device = next(self.parameters()).device
shape = (self.num_layers*2, batch_size, self.hidden_size)
# The initial state is a constant here, and is not a learnable parameter
h_0 = torch.zeros(shape, dtype=torch.float, device=device)
return h_0
class DecoderAttn(nn.Module):
def __init__(
self,
vocabulary_size=30522,
embedding_size=256,
hidden_size=256,
num_layers=1,
dropout=0.0,
):
super(DecoderAttn, self).__init__()
self.vocabulary_size = vocabulary_size
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.dropout = nn.Dropout(p=dropout)
self.rnn = nn.GRU(embedding_size, hidden_size, num_layers=num_layers, batch_first=True)
self.mlp_attn = Attn(hidden_size, dropout)
def forward(self, inputs, hidden_states, mask=None):
"""GRU Decoder network with Soft attention
This is a Unidirectional Gated Recurrent Unit Encoder network
Parameters
----------
inputs (`torch.LongTensor` of shape `(batch_size, sequence_length, hidden_size)`)
The input tensor containing the encoded input sequence.
hidden_states(`torch.FloatTensor` of shape `(num_layers*2, batch_size, hidden_size)`)
The (initial) hidden state for the bidrectional GRU.
mask ( optional `torch.LongTensor` of shape `(batch_size, sequence_length)`)
The masked tensor containing the location of padding in the sequences.
Returns
-------
outputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`)
A feature tensor encoding the input sentence.
hidden_states (`torch.FloatTensor` of shape `(num_layers, batch_size, hidden_size)`)
The final hidden state.
"""
x = self.dropout(inputs)
x, x_attn = self.mlp_attn(x, hidden_states)
outputs, hidden_states = self.rnn(x, hidden_states)
return outputs, hidden_states
class EncoderDecoder(nn.Module):
def __init__(
self,
vocabulary_size=30522,
embedding_size=256,
hidden_size=256,
num_layers=1,
dropout = 0.0,
encoder_only=False
):
super(EncoderDecoder, self).__init__()
self.encoder_only = encoder_only
self.encoder = Encoder(vocabulary_size, embedding_size, hidden_size,
num_layers, dropout=dropout)
if not encoder_only:
self.decoder = DecoderAttn(vocabulary_size, embedding_size, hidden_size, num_layers, dropout=dropout)
def forward(self, inputs, mask=None):
"""GRU Encoder-Decoder network with Soft attention.
This is a Gated Recurrent Unit network for Sentiment Analysis. This
module returns a decoded feature for classification.
Parameters
----------
inputs (`torch.LongTensor` of shape `(batch_size, sequence_length)`)
The input tensor containing the token sequences.
mask (`torch.LongTensor` of shape `(batch_size, sequence_length)`)
The masked tensor containing the location of padding in the sequences.
Returns
-------
x (`torch.FloatTensor` of shape `(batch_size, hidden_size)`)
A feature tensor encoding the input sentence.
hidden_states (`torch.FloatTensor` of shape `(num_layers, batch_size, hidden_size)`)
The final hidden state.
"""
hidden_states = self.encoder.initial_states(inputs.shape[0])
x, hidden_states = self.encoder(inputs, hidden_states)
if self.encoder_only:
x = x[:, 0]
return x, hidden_states
x, hidden_states = self.decoder(x, hidden_states, mask)
x = x[:, 0]
return x, hidden_states