-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmodels.py
211 lines (196 loc) · 7.72 KB
/
models.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
import torch
import torch.nn as nn
from torch.nn.utils.rnn import pad_packed_sequence
from torch.nn.utils.rnn import pack_padded_sequence
import os
import constants
class StackingGRUCell(nn.Module):
"""
Multi-layer CRU Cell
"""
def __init__(self, input_size, hidden_size, num_layers, dropout):
super(StackingGRUCell, self).__init__()
self.num_layers = num_layers
self.grus = nn.ModuleList()
self.dropout = nn.Dropout(dropout)
self.grus.append(nn.GRUCell(input_size, hidden_size))
for i in range(1, num_layers):
self.grus.append(nn.GRUCell(hidden_size, hidden_size))
def forward(self, input, h0):
"""
Input:
input (batch, input_size): input tensor
h0 (num_layers, batch, hidden_size): initial hidden state
---
Output:
output (batch, hidden_size): the final layer output tensor
hn (num_layers, batch, hidden_size): the hidden state of each layer
"""
hn = []
output = input
for i, gru in enumerate(self.grus):
hn_i = gru(output, h0[i])
hn.append(hn_i)
if i != self.num_layers - 1:
output = self.dropout(hn_i)
else:
output = hn_i
hn = torch.stack(hn)
return output, hn
class GlobalAttention(nn.Module):
"""
$$a = \sigma((W_1 q)H)$$
$$c = \tanh(W_2 [a H, q])$$
"""
def __init__(self, hidden_size):
super(GlobalAttention, self).__init__()
self.L1 = nn.Linear(hidden_size, hidden_size, bias=False)
self.L2 = nn.Linear(2*hidden_size, hidden_size, bias=False)
self.softmax = nn.Softmax(dim=1)
self.tanh = nn.Tanh()
def forward(self, q, H):
"""
Input:
q (batch, hidden_size): query
H (batch, seq_len, hidden_size): context
---
Output:
c (batch, hidden_size)
"""
# (batch, hidden_size) => (batch, hidden_size, 1)
q1 = self.L1(q).unsqueeze(2)
# (batch, seq_len)
a = torch.bmm(H, q1).squeeze(2)
a = self.softmax(a)
# (batch, seq_len) => (batch, 1, seq_len)
a = a.unsqueeze(1)
# (batch, hidden_size)
c = torch.bmm(a, H).squeeze(1)
# (batch, hidden_size * 2)
c = torch.cat([c, q], 1)
return self.tanh(self.L2(c))
class Encoder(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, dropout,
bidirectional, embedding):
"""
embedding (vocab_size, input_size): pretrained embedding
"""
super(Encoder, self).__init__()
self.num_directions = 2 if bidirectional else 1
assert hidden_size % self.num_directions == 0
self.hidden_size = hidden_size // self.num_directions
self.num_layers = num_layers
self.embedding = embedding
self.rnn = nn.GRU(input_size, self.hidden_size,
num_layers=num_layers,
bidirectional=bidirectional,
dropout=dropout)
def forward(self, input, lengths, h0=None):
"""
Input:
input (seq_len, batch): padded sequence tensor
lengths (1, batch): sequence lengths
h0 (num_layers*num_directions, batch, hidden_size): initial hidden state
---
Output:
hn (num_layers*num_directions, batch, hidden_size):
the hidden state of each layer
output (seq_len, batch, hidden_size*num_directions): output tensor
"""
# (seq_len, batch) => (seq_len, batch, input_size)
embed = self.embedding(input)
lengths = lengths.data.view(-1).tolist()
if lengths is not None:
embed = pack_padded_sequence(embed, lengths)
output, hn = self.rnn(embed, h0)
if lengths is not None:
output = pad_packed_sequence(output)[0]
return hn, output
class Decoder(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, dropout, embedding):
super(Decoder, self).__init__()
self.embedding = embedding
self.rnn = StackingGRUCell(input_size, hidden_size, num_layers,
dropout)
self.attention = GlobalAttention(hidden_size)
self.dropout = nn.Dropout(dropout)
self.num_layers = num_layers
def forward(self, input, h, H, use_attention=True):
"""
Input:
input (seq_len, batch): padded sequence tensor
h (num_layers, batch, hidden_size): input hidden state
H (seq_len, batch, hidden_size): the context used in attention mechanism
which is the output of encoder
use_attention: If True then we use attention
---
Output:
output (seq_len, batch, hidden_size)
h (num_layers, batch, hidden_size): output hidden state,
h may serve as input hidden state for the next iteration,
especially when we feed the word one by one (i.e., seq_len=1)
such as in translation
"""
assert input.dim() == 2, "The input should be of (seq_len, batch)"
# (seq_len, batch) => (seq_len, batch, input_size)
embed = self.embedding(input)
output = []
# split along the sequence length dimension
for e in embed.split(1):
e = e.squeeze(0) # (1, batch, input_size) => (batch, input_size)
o, h = self.rnn(e, h)
if use_attention:
o = self.attention(o, H.transpose(0, 1))
o = self.dropout(o)
output.append(o)
output = torch.stack(output)
return output, h
class EncoderDecoder(nn.Module):
def __init__(self, vocab_size, embedding_size,
hidden_size, num_layers, dropout, bidirectional):
super(EncoderDecoder, self).__init__()
self.vocab_size = vocab_size
self.embedding_size = embedding_size
## the embedding shared by encoder and decoder
self.embedding = nn.Embedding(vocab_size, embedding_size,
padding_idx=constants.PAD)
self.encoder = Encoder(embedding_size, hidden_size, num_layers,
dropout, bidirectional, self.embedding)
self.decoder = Decoder(embedding_size, hidden_size, num_layers,
dropout, self.embedding)
self.num_layers = num_layers
def load_pretrained_embedding(path):
if os.path.isfile(path):
w = torch.load(path)
self.embedding.weight.data.copy_(w)
def encoder_hn2decoder_h0(self, h):
"""
Input:
h (num_layers * num_directions, batch, hidden_size): encoder output hn
---
Output:
h (num_layers, batch, hidden_size * num_directions): decoder input h0
"""
if self.encoder.num_directions == 2:
num_layers, batch, hidden_size = h.size(0)//2, h.size(1), h.size(2)
return h.view(num_layers, 2, batch, hidden_size)\
.transpose(1, 2).contiguous()\
.view(num_layers, batch, hidden_size * 2)
else:
return h
def forward(self, src, lengths, trg):
"""
Input:
src (src_seq_len, batch): source tensor
lengths (1, batch): source sequence lengths
trg (trg_seq_len, batch): target tensor, the `seq_len` in trg is not
necessarily the same as that in src
---
Output:
output (trg_seq_len, batch, hidden_size)
"""
encoder_hn, H = self.encoder(src, lengths)
decoder_h0 = self.encoder_hn2decoder_h0(encoder_hn)
## for target we feed the range [BOS:EOS-1] into decoder
output, decoder_hn = self.decoder(trg[:-1], decoder_h0, H)
return output