forked from chrischute/squad
-
Notifications
You must be signed in to change notification settings - Fork 73
/
layers.py
222 lines (175 loc) · 8.75 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
"""Assortment of layers for use in models.py.
Author:
Chris Chute ([email protected])
"""
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
class Embedding(nn.Module):
"""Embedding layer used by BiDAF, without the character-level component.
Word-level embeddings are further refined using a 2-layer Highway Encoder
(see `HighwayEncoder` class for details).
Args:
word_vectors (torch.Tensor): Pre-trained word vectors.
hidden_size (int): Size of hidden activations.
drop_prob (float): Probability of zero-ing out activations
"""
def __init__(self, word_vectors, hidden_size, drop_prob):
super(Embedding, self).__init__()
self.drop_prob = drop_prob
self.embed = nn.Embedding.from_pretrained(word_vectors)
self.proj = nn.Linear(word_vectors.size(1), hidden_size, bias=False)
self.hwy = HighwayEncoder(2, hidden_size)
def forward(self, x):
emb = self.embed(x) # (batch_size, seq_len, embed_size)
emb = F.dropout(emb, self.drop_prob, self.training)
emb = self.proj(emb) # (batch_size, seq_len, hidden_size)
emb = self.hwy(emb) # (batch_size, seq_len, hidden_size)
return emb
class HighwayEncoder(nn.Module):
"""Encode an input sequence using a highway network.
Based on the paper:
"Highway Networks"
by Rupesh Kumar Srivastava, Klaus Greff, Jürgen Schmidhuber
(https://arxiv.org/abs/1505.00387).
Args:
num_layers (int): Number of layers in the highway encoder.
hidden_size (int): Size of hidden activations.
"""
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, 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(8 * 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(8 * 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