-
Notifications
You must be signed in to change notification settings - Fork 3
/
text_encoders.py
359 lines (268 loc) · 11.2 KB
/
text_encoders.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import torch
import torch.nn as nn
import torch.nn.init
import torchvision.models as models
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence
from torch.nn.utils.rnn import pad_packed_sequence
import torch.backends.cudnn as cudnn
from torch.nn.utils.clip_grad import clip_grad_norm
from collections import OrderedDict
from torch.nn import functional as F
from layers import l2norm
from layers import global_initializer
class SelfAttentiveEncoder(nn.Module):
'''
Self-attention module
'''
def __init__(self, nb_features=300, att_units=300, hops=30):
super(SelfAttentiveEncoder, self).__init__()
self.drop = nn.Dropout(0.0)
self.ws1 = nn.Linear(nb_features, att_units, bias=False)
self.ws2 = nn.Linear(att_units, hops, bias=False)
self.tanh = nn.Tanh()
self.softmax = nn.Softmax()
self.attention_hops = hops
def forward(self, inp, zero_mask=None):
size = inp.size() # [batch, len, nhid]
x = inp
hbar = self.tanh(self.ws1(self.drop(x))) # [batch*len, attention-unit]
alphas = self.ws2(hbar).view(size[0], size[1], -1) # [batch, len, hop]
alphas = torch.transpose(alphas, 1, 2).contiguous() # [batch, hop, len]
if zero_mask is not None:
# ignores zero padding
alphas = alphas + (
-10000 * zero_mask.float().unsqueeze(1))
# [batch, hop, len] + [batch, hop, len]
alphas = self.softmax(alphas.view(-1, size[1])) # [batch*hop, len]
alphas = alphas.view(size[0], self.attention_hops, size[1]) # [batch, hop, len]
return torch.bmm(alphas, inp), alphas
class GRUAttentiveTextEncoder(nn.Module):
'''
SEAM-G
'''
def __init__(self, vocab_size, word_dim, embed_size,
use_abs=False, att_units=200, hops=15,
gru_units=1024, num_layers=1, norm_words=None):
super(GRUAttentiveTextEncoder, self).__init__()
self.use_abs = use_abs
self.embed_size = embed_size
self.hops = hops
self.att_units = att_units
# word embedding
self.embed = nn.Embedding(vocab_size, word_dim)
self.norm_words = norm_words
# caption embedding
self.rnn = nn.GRU(word_dim, gru_units, num_layers, batch_first=True)
self.attention = SelfAttentiveEncoder(nb_features=gru_units,
att_units=att_units, hops=hops)
self.fc = nn.Linear(gru_units * hops, embed_size)
global_initializer(self)
def forward(self, inputs, lengths):
# Embed word ids to vectors
x = self.embed(inputs)
packed = pack_padded_sequence(x, lengths, batch_first=True)
# Forward propagate RNN
out, _ = self.rnn(packed)
# Reshape *final* output to (batch_size, hidden_size)
padded = pad_packed_sequence(out, batch_first=True)[0]
out, att_weights = self.attention(padded, (inputs == 0))
self.attention_weights = att_weights
out = out.view(inputs.size()[0], -1)
fc_out = self.fc(out)
# normalization in the joint embedding space
outnormed = l2norm(fc_out)
# take absolute value, used by order embeddings
if self.use_abs:
outnormed = torch.abs(outnormed)
return outnormed
class ConvAttentiveTextEncoder(nn.Module):
'''
SEAM-C
'''
def __init__(self, vocab_size, word_dim, embed_size,
use_abs=False, att_units=300, hops=30,
gru_units=None, num_layers=1, norm_words=None):
super(ConvAttentiveTextEncoder, self).__init__()
self.use_abs = use_abs
self.embed_size = embed_size
self.hops = hops
self.att_units = att_units
conv_filters = 100
# word embedding
self.embed = nn.Embedding(vocab_size, word_dim)
self.conv1 = ConvBlock(
in_channels=word_dim,
out_channels=conv_filters,
kernel_size=2,
padding=1,
activation='ReLU',
batchnorm=True,)
self.conv2 = ConvBlock(
in_channels=word_dim,
out_channels=conv_filters,
kernel_size=3,
padding=1,
activation='ReLU',
batchnorm=True,)
self.att_emb = SelfAttentiveEncoder(nb_features=word_dim,
att_units=att_units, hops=hops)
self.att_conv1 = SelfAttentiveEncoder(nb_features=conv_filters,
att_units=att_units, hops=hops)
self.att_conv2 = SelfAttentiveEncoder(nb_features=conv_filters,
att_units=att_units, hops=hops)
self.fc = nn.Linear((conv_filters * 2 + word_dim) * hops, embed_size)
global_initializer(self)
def forward(self, inputs, lengths):
# Embed word ids to vectors
x_embed = self.embed(inputs)
x = x_embed.permute(0, 2, 1) # [B, F, T]
conv1 = self.conv1(x)[:,:,:-1]
conv1a, conv1a_vis = self.att_conv1(conv1.permute(0, 2, 1),
(inputs == 0)) # 10 * 100 = 1000
conv1a = conv1a.view(conv1a.size()[0], -1)
conv2 = self.conv2(x)
conv2a, conv2a_vis = self.att_conv2(conv2.permute(0, 2, 1),
(inputs == 0)) # 10 * 100 = 1000
conv2a = conv2a.view(conv2a.size()[0], -1)
emb_att, emb_vis = self.att_emb(x_embed, (inputs == 0)) # 10 * 300 = 3000
self.attention_weights = emb_att
emb_att = emb_att.view(emb_att.size()[0], -1)
vectors = torch.cat([conv1a, conv2a, emb_att], 1) # [B, 5000]
fc_out = self.fc(vectors)
# normalization in the joint embedding space
outnormed = l2norm(fc_out)
# take absolute value, used by order embeddings
if self.use_abs:
outnormed = torch.abs(outnormed)
return outnormed
class AttentiveTextEncoder(nn.Module):
'''
SEAM-E
'''
def __init__(self, vocab_size, word_dim, embed_size,
use_abs=False, att_units=300, hops=30,
gru_units=None, num_layers=None, norm_words=False):
super(AttentiveTextEncoder, self).__init__()
self.use_abs = use_abs
self.embed_size = embed_size
self.hops = hops
self.att_units = att_units
# word embedding
self.embed = nn.Embedding(vocab_size, word_dim)
self.attention = SelfAttentiveEncoder(nb_features=word_dim,
att_units=att_units, hops=hops)
att_out_size = word_dim * hops
self.fc = False
if att_out_size != embed_size:
self.fc = nn.Linear(att_out_size, embed_size)
global_initializer(self)
def forward(self, inputs, lengths):
# Embed word ids to vectors
x = self.embed(inputs)
out, att_weights = self.attention(x, (inputs == 0))
self.attention_weights = att_weights
out = out.view(inputs.size()[0], -1)
if self.fc:
out = self.fc(out)
# normalization in the joint embedding space
outnormed = l2norm(out)
# take absolute value, used by order embeddings
if self.use_abs:
outnormed = torch.abs(outnormed)
return outnormed
# tutorials/08 - Language Model
# RNN Based Language Model
class GRUTextEncoder(nn.Module):
def __init__(self, vocab_size, word_dim, embed_size, num_layers,
use_abs=False, gru_units=1024):
super(GRUTextEncoder, self).__init__()
self.use_abs = use_abs
self.embed_size = embed_size
self.gru_units = gru_units
# word embedding
self.embed = nn.Embedding(vocab_size, word_dim)
# caption embedding
self.rnn = nn.GRU(word_dim, gru_units, num_layers, batch_first=True)
self.fc = None
if embed_size != gru_units:
self.fc = nn.Linear(gru_units, embed_size)
self.init_weights()
def init_weights(self):
self.embed.weight.data.uniform_(-0.1, 0.1)
if self.fc:
r = np.sqrt(6.) / np.sqrt(self.fc.in_features +
self.fc.out_features)
self.fc.weight.data.uniform_(-r, r)
self.fc.bias.data.fill_(0)
def forward(self, x, lengths):
"""Handles variable size captions
"""
# Embed word ids to vectors
x = self.embed(x)
packed = pack_padded_sequence(x, lengths, batch_first=True)
# Forward propagate RNN
out, _ = self.rnn(packed)
# Reshape *final* output to (batch_size, hidden_size)
padded = pad_packed_sequence(out, batch_first=True)
I = torch.LongTensor(lengths).view(-1, 1, 1)
I = Variable(I.expand(x.size(0), 1, self.gru_units)-1)
if torch.cuda.is_available():
I = I.cuda()
out = torch.gather(padded[0], 1, I).squeeze(1)
if self.fc:
out = self.fc(out)
# normalization in the joint embedding space
outnormed = l2norm(out)
# take absolute value, used by order embeddings
if self.use_abs:
outnormed = torch.abs(outnormed)
return outnormed
class ConvBlock(nn.Module):
def __init__(self, activation=None, batchnorm=False, **kwargs):
super(ConvBlock, self).__init__()
layers = OrderedDict()
layers['conv'] = nn.Conv1d(**kwargs)
if activation is not None:
layers['activation'] = eval('nn.{}'.format(activation))()
if batchnorm:
layers['bn'] = nn.BatchNorm1d(kwargs['out_channels'])
self.conv = nn.Sequential(layers)
def forward(self, x):
return self.conv(x)
text_encoders_alias = {
'gru': {'method': GRUTextEncoder, 'args': {}},
'seam-e': {'method': AttentiveTextEncoder, 'args': {}},
'seam-g': {'method': GRUAttentiveTextEncoder, 'args': {}},
'seam-c': {'method': ConvAttentiveTextEncoder, 'args': {}},
}
def get_text_encoder(encoder, opt):
encoder = encoder.lower()
vocab_size = opt.vocab_size
word_dim = opt.word_dim
num_layers = opt.num_layers
embed_size = opt.embed_size
use_abs = opt.use_abs
try:
gru_units = opt.gru_units
norm_words = opt.norm_words
except AttributeError:
gru_units = embed_size
norm_words = None
params = {
'vocab_size': vocab_size,
'word_dim': word_dim,
'gru_units': gru_units,
'embed_size': embed_size,
'num_layers': num_layers,
'use_abs': opt.use_abs,
}
if encoder.startswith('seam'):
params['att_units'] = opt.att_units
params['hops'] = opt.att_hops
try:
txt_enc = eval(encoder)(**params)
except NameError:
params.update(text_encoders_alias[encoder]['args'])
txt_enc = text_encoders_alias[encoder]['method'](**params)
return txt_enc