-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
96 lines (69 loc) · 3.9 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
"""Top-level model classes.
"""
import layers
import torch
import torch.nn as nn
import torch.nn.functional as F
class BiDAF(nn.Module):
"""Baseline BiDAF model for SQuAD.
Based on the paper:
"Bidirectional Attention Flow for Machine Comprehension"
by Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, Hannaneh Hajishirzi
(https://arxiv.org/abs/1611.01603).
Follows a high-level structure commonly found in SQuAD models:
- Embedding layer: Embed word indices to get word vectors.
- Encoder layer: Encode the embedded sequence.
- Attention layer: Apply an attention mechanism to the encoded sequence.
- Model encoder layer: Encode the sequence again.
- Output layer: Simple layer (e.g., fc + softmax) to get final outputs.
Args:
word_vectors (torch.Tensor): Pre-trained word vectors.
hidden_size (int): Number of features in the hidden state at each layer.
drop_prob (float): Dropout probability.
"""
def __init__(self, word_vectors, hidden_size, char_vocab_size, drop_prob=0., bidaf_layers = 2):
super(BiDAF, self).__init__()
self.emb = layers.Embedding(word_vectors=word_vectors,
hidden_size=hidden_size,
char_vocab_size=char_vocab_size,
char_embedding_size=100,
kernel_size=5,
drop_prob=drop_prob)
self.enc = layers.RNNEncoder(input_size=2 * hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob)
self.att = layers.BiDAFAttention(hidden_size=2 * hidden_size,
drop_prob=drop_prob)
self.encs_att = nn.ModuleList([layers.RNNEncoder(input_size=8 * hidden_size,
hidden_size=hidden_size,
num_layers=1,
drop_prob=drop_prob) for _ in range(bidaf_layers)])
self.atts = nn.ModuleList([layers.BiDAFAttention(hidden_size=2 * hidden_size,
drop_prob=drop_prob) for _ in range(bidaf_layers)])
self.gates = nn.ModuleList([nn.Linear(8 * hidden_size, 8 * hidden_size) for _ in range(bidaf_layers)])
self.mod = layers.RNNEncoder(input_size=8 * hidden_size,
hidden_size=hidden_size,
num_layers=2,
drop_prob=drop_prob)
self.out = layers.BiDAFOutput(hidden_size=hidden_size,
drop_prob=drop_prob)
self.drop_out = nn.Dropout(drop_prob)
def forward(self, cw_idxs, qw_idxs, cw_char_idx, qw_char_idxs):
c_mask = torch.zeros_like(cw_idxs) != cw_idxs
q_mask = torch.zeros_like(qw_idxs) != qw_idxs
c_len, q_len = c_mask.sum(-1), q_mask.sum(-1)
c_emb = self.emb(cw_idxs, cw_char_idx) # (batch_size, c_len, hidden_size)
q_emb = self.emb(qw_idxs, qw_char_idxs) # (batch_size, q_len, hidden_size)
c_enc = self.enc(c_emb, c_len) # (batch_size, c_len, 2 * hidden_size)
q_enc = self.enc(q_emb, q_len) # (batch_size, q_len, 2 * hidden_size)
att = self.att(c_enc, q_enc,
c_mask, q_mask) # (batch_size, c_len, 8 * hidden_size)
for enc_att, att_func, gate in zip(self.encs_att, self.atts, self.gates):
att2 = enc_att(att, c_len)
att2 = att_func(att2, att2, c_mask, c_mask)
att = att + F.relu(gate(att2))
att = self.drop_out(att)
mod = self.mod(att, c_len) # (batch_size, c_len, 2 * hidden_size)
out = self.out(att, mod, c_mask) # 2 tensors, each (batch_size, c_len)
return out