-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
executable file
·272 lines (221 loc) · 11 KB
/
layers.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
from util import masked_softmax
import math
from torch.autograd import Variable
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) *
-(math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + Variable(self.pe[:, :x.size(1)],
requires_grad=False)
return self.dropout(x)
class LayerNorm(nn.Module):
def __init__(self, features, eps=1e-6):
super(LayerNorm, self).__init__()
self.a_2 = nn.Parameter(torch.ones(features))
self.b_2 = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.a_2 * (x - mean) / (std + self.eps) + self.b_2
class QAEmbedding(nn.Module):
def __init__(self,word_vectors,char_vectors,hidden_size,drop_prob):
super(QAEmbedding,self).__init__()
self.w_embed = nn.Embedding.from_pretrained(word_vectors,freeze=True)
self.c_embed = nn.Embedding.from_pretrained(char_vectors,freeze=False)
# add padding idx in both Embs later
self.e_char = char_vectors.size(1) # get char emb size
self.char_cnn = CharCNN(self.e_char,hidden_size)
self.e_word = word_vectors.size(1)
self.word_proj = ProjCNN(in_channels=self.e_word,out_channels=hidden_size)
self.proj = ProjCNN(in_channels=2*hidden_size,out_channels=hidden_size)
self.highway = HighwayEncoder(num_layers=2,hidden_size=hidden_size)
self.drop_prob = drop_prob
def forward(self,w_idxs,c_idxs):
w_emb = self.w_embed(w_idxs) # batch, c_len, emb_size
c_emb = self.c_embed(c_idxs)
c_emb = self.char_cnn(c_emb.permute(0,3,1,2))
w_emb = F.dropout(w_emb,self.drop_prob,training=self.training)
c_emb = F.dropout(c_emb,self.drop_prob,training=self.training)
w_emb = self.word_proj(w_emb.transpose(1,2))# as we have to map the emb dim to hidden
emb = torch.cat([w_emb,c_emb],dim=1)
emb = self.proj(emb)
hwy_out = self.highway(emb.transpose(1,2))
return hwy_out
class CharCNN(nn.Module):
def __init__(self,e_char,hidden_size,k=5,pad=0):
"""
@param e_char (int) : char embedding size. used as Cin in convolution.
@param hidden_size (int) : size of emb for a word
"""
super(CharCNN,self).__init__()
self.conv = nn.Conv2d(in_channels=e_char,out_channels=hidden_size,kernel_size=(1,k),padding=pad)
nn.init.kaiming_normal_(self.conv.weight)
def forward(self,x):
# convolve over the char embeddings to get word embedding for a word.
out = F.relu(self.conv(x))
out , _= out.max(dim=-1)
return out
class ProjCNN(nn.Module):
def __init__(self,in_channels,out_channels,k=1,relu=False):
super(ProjCNN,self).__init__()
self.proj = nn.Conv1d(in_channels=in_channels,out_channels=out_channels,kernel_size=k)
nn.init.kaiming_normal_(self.proj.weight,nonlinearity='relu')
self.relu = relu
def forward(self,x):
x = self.proj(x)
if self.relu:
x=F.relu(x)
return x
class HighwayEncoder(nn.Module):
"""Encode an input sequence using a highway network.
"""
def __init__(self, num_layers, hidden_size):
super(HighwayEncoder, self).__init__()
self.transforms = nn.ModuleList([nn.Linear(hidden_size, hidden_size)
for _ in range(num_layers)])
self.gates = nn.ModuleList([nn.Linear(hidden_size, hidden_size)
for _ in range(num_layers)])
def forward(self, x):
for gate, transform in zip(self.gates, self.transforms):
# Shapes of g, t, and x are all (batch_size, seq_len, hidden_size)
g = torch.sigmoid(gate(x))
t = F.relu(transform(x))
x = g * t + (1 - g) * x
return x
class RNNEncoder(nn.Module):
"""General-purpose layer for encoding a sequence using a bidirectional RNN.
Encoded output is the RNN's hidden state at each position, which
has shape `(batch_size, seq_len, hidden_size * 2)`.
Args:
input_size (int): Size of a single timestep in the input.
hidden_size (int): Size of the RNN hidden state.
num_layers (int): Number of layers of RNN cells to use.
drop_prob (float): Probability of zero-ing out activations.
"""
def __init__(self,
input_size,
hidden_size,
num_layers,
drop_prob=0.):
super(RNNEncoder, self).__init__()
self.drop_prob = drop_prob
self.rnn = nn.LSTM(input_size, hidden_size, num_layers,
batch_first=True,
bidirectional=True,
dropout=drop_prob if num_layers > 1 else 0.)
def forward(self, x, lengths):
# Save original padded length for use by pad_packed_sequence
orig_len = x.size(1)
# Sort by length and pack sequence for RNN
lengths, sort_idx = lengths.sort(0, descending=True)
x = x[sort_idx] # (batch_size, seq_len, input_size)
x = pack_padded_sequence(x, lengths.cpu(), batch_first=True)
# Apply RNN
x, _ = self.rnn(x) # (batch_size, seq_len, 2 * hidden_size)
# Unpack and reverse sort
x, _ = pad_packed_sequence(x, batch_first=True, total_length=orig_len)
_, unsort_idx = sort_idx.sort(0)
x = x[unsort_idx] # (batch_size, seq_len, 2 * hidden_size)
# Apply dropout (RNN applies dropout after all but the last layer)
x = F.dropout(x, self.drop_prob, self.training)
return x
class BiDAFAttention(nn.Module):
"""Bidirectional attention originally used by BiDAF.
Bidirectional attention computes attention in two directions:
The context attends to the query and the query attends to the context.
The output of this layer is the concatenation of [context, c2q_attention,
context * c2q_attention, context * q2c_attention]. This concatenation allows
the attention vector at each timestep, along with the embeddings from
previous layers, to flow through the attention layer to the modeling layer.
The output has shape (batch_size, context_len, 8 * hidden_size).
Args:
hidden_size (int): Size of hidden activations.
drop_prob (float): Probability of zero-ing out activations.
"""
def __init__(self, hidden_size, drop_prob=0.1):
super(BiDAFAttention, self).__init__()
self.drop_prob = drop_prob
self.c_weight = nn.Parameter(torch.zeros(hidden_size, 1))
self.q_weight = nn.Parameter(torch.zeros(hidden_size, 1))
self.cq_weight = nn.Parameter(torch.zeros(1, 1, hidden_size))
for weight in (self.c_weight, self.q_weight, self.cq_weight):
nn.init.xavier_uniform_(weight)
self.bias = nn.Parameter(torch.zeros(1))
def forward(self, c, q, c_mask, q_mask):
batch_size, c_len, _ = c.size()
q_len = q.size(1)
s = self.get_similarity_matrix(c, q) # (batch_size, c_len, q_len)
c_mask = c_mask.view(batch_size, c_len, 1) # (batch_size, c_len, 1)
q_mask = q_mask.view(batch_size, 1, q_len) # (batch_size, 1, q_len)
s1 = masked_softmax(s, q_mask, dim=2) # (batch_size, c_len, q_len)
s2 = masked_softmax(s, c_mask, dim=1) # (batch_size, c_len, q_len)
# (bs, c_len, q_len) x (bs, q_len, hid_size) => (bs, c_len, hid_size)
a = torch.bmm(s1, q)
# (bs, c_len, c_len) x (bs, c_len, hid_size) => (bs, c_len, hid_size)
b = torch.bmm(torch.bmm(s1, s2.transpose(1, 2)), c)
x = torch.cat([c, a, c * a, c * b], dim=2) # (bs, c_len, 4 * hid_size)
return x
def get_similarity_matrix(self, c, q):
"""Get the "similarity matrix" between context and query (using the
terminology of the BiDAF paper).
A naive implementation as described in BiDAF would concatenate the
three vectors then project the result with a single weight matrix. This
method is a more memory-efficient implementation of the same operation.
See Also:
Equation 1 in https://arxiv.org/abs/1611.01603
"""
c_len, q_len = c.size(1), q.size(1)
c = F.dropout(c, self.drop_prob, self.training) # (bs, c_len, hid_size)
q = F.dropout(q, self.drop_prob, self.training) # (bs, q_len, hid_size)
# Shapes: (batch_size, c_len, q_len)
s0 = torch.matmul(c, self.c_weight).expand([-1, -1, q_len])
s1 = torch.matmul(q, self.q_weight).transpose(1, 2)\
.expand([-1, c_len, -1])
s2 = torch.matmul(c * self.cq_weight, q.transpose(1, 2))
s = s0 + s1 + s2 + self.bias
return s
class BiDAFOutput(nn.Module):
"""Output layer used by BiDAF for question answering.
Computes a linear transformation of the attention and modeling
outputs, then takes the softmax of the result to get the start pointer.
A bidirectional LSTM is then applied the modeling output to produce `mod_2`.
A second linear+softmax of the attention output and `mod_2` is used
to get the end pointer.
Args:
hidden_size (int): Hidden size used in the BiDAF model.
drop_prob (float): Probability of zero-ing out activations.
"""
def __init__(self, hidden_size, drop_prob):
super(BiDAFOutput, self).__init__()
self.att_linear_1 = nn.Linear(4 * hidden_size, 1)
self.mod_linear_1 = nn.Linear(2 * hidden_size, 1)
self.rnn = RNNEncoder(input_size=2 * hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob)
self.att_linear_2 = nn.Linear(4 * hidden_size, 1)
self.mod_linear_2 = nn.Linear(2 * hidden_size, 1)
def forward(self, att, mod, mask):
# Shapes: (batch_size, seq_len, 1)
logits_1 = self.att_linear_1(att) + self.mod_linear_1(mod)
mod_2 = self.rnn(mod, mask.sum(-1))
logits_2 = self.att_linear_2(att) + self.mod_linear_2(mod_2)
# Shapes: (batch_size, seq_len)
log_p1 = masked_softmax(logits_1.squeeze(), mask, log_softmax=True)
log_p2 = masked_softmax(logits_2.squeeze(), mask, log_softmax=True)
return log_p1, log_p2