-
Notifications
You must be signed in to change notification settings - Fork 38
/
m_reader.py
165 lines (142 loc) · 6.93 KB
/
m_reader.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
#!/usr/bin/env python3
# Copyright 2018-present, HKUST-KnowComp.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""Implementation of the Mnemonic Reader."""
import torch
import torch.nn as nn
import torch.nn.functional as F
import layers
from torch.autograd import Variable
# ------------------------------------------------------------------------------
# Network
# ------------------------------------------------------------------------------
class MnemonicReader(nn.Module):
RNN_TYPES = {'lstm': nn.LSTM, 'gru': nn.GRU, 'rnn': nn.RNN}
CELL_TYPES = {'lstm': nn.LSTMCell, 'gru': nn.GRUCell, 'rnn': nn.RNNCell}
def __init__(self, args, normalize=True):
super(MnemonicReader, self).__init__()
# Store config
self.args = args
# Word embeddings (+1 for padding)
self.embedding = nn.Embedding(args.vocab_size,
args.embedding_dim,
padding_idx=0)
# Char embeddings (+1 for padding)
self.char_embedding = nn.Embedding(args.char_size,
args.char_embedding_dim,
padding_idx=0)
# Char rnn to generate char features
self.char_rnn = layers.StackedBRNN(
input_size=args.char_embedding_dim,
hidden_size=args.char_hidden_size,
num_layers=1,
dropout_rate=args.dropout_rnn,
dropout_output=args.dropout_rnn_output,
concat_layers=False,
rnn_type=self.RNN_TYPES[args.rnn_type],
padding=False,
)
doc_input_size = args.embedding_dim + args.char_hidden_size * 2 + args.num_features
# Encoder
self.encoding_rnn = layers.StackedBRNN(
input_size=doc_input_size,
hidden_size=args.hidden_size,
num_layers=1,
dropout_rate=args.dropout_rnn,
dropout_output=args.dropout_rnn_output,
concat_layers=False,
rnn_type=self.RNN_TYPES[args.rnn_type],
padding=args.rnn_padding,
)
doc_hidden_size = 2 * args.hidden_size
# Interactive aligning, self aligning and aggregating
self.interactive_aligners = nn.ModuleList()
self.interactive_SFUs = nn.ModuleList()
self.self_aligners = nn.ModuleList()
self.self_SFUs = nn.ModuleList()
self.aggregate_rnns = nn.ModuleList()
for i in range(args.hop):
# interactive aligner
self.interactive_aligners.append(layers.SeqAttnMatch(doc_hidden_size, identity=True))
self.interactive_SFUs.append(layers.SFU(doc_hidden_size, 3 * doc_hidden_size))
# self aligner
self.self_aligners.append(layers.SelfAttnMatch(doc_hidden_size, identity=True, diag=False))
self.self_SFUs.append(layers.SFU(doc_hidden_size, 3 * doc_hidden_size))
# aggregating
self.aggregate_rnns.append(
layers.StackedBRNN(
input_size=doc_hidden_size,
hidden_size=args.hidden_size,
num_layers=1,
dropout_rate=args.dropout_rnn,
dropout_output=args.dropout_rnn_output,
concat_layers=False,
rnn_type=self.RNN_TYPES[args.rnn_type],
padding=args.rnn_padding,
)
)
# Memmory-based Answer Pointer
self.mem_ans_ptr = layers.MemoryAnsPointer(
x_size=2*args.hidden_size,
y_size=2*args.hidden_size,
hidden_size=args.hidden_size,
hop=args.hop,
dropout_rate=args.dropout_rnn,
normalize=normalize
)
def forward(self, x1, x1_c, x1_f, x1_mask, x2, x2_c, x2_f, x2_mask):
"""Inputs:
x1 = document word indices [batch * len_d]
x1_c = document char indices [batch * len_d]
x1_f = document word features indices [batch * len_d * nfeat]
x1_mask = document padding mask [batch * len_d]
x2 = question word indices [batch * len_q]
x2_c = document char indices [batch * len_d]
x1_f = document word features indices [batch * len_d * nfeat]
x2_mask = question padding mask [batch * len_q]
"""
# Embed both document and question
x1_emb = self.embedding(x1)
x2_emb = self.embedding(x2)
x1_c_emb = self.char_embedding(x1_c)
x2_c_emb = self.char_embedding(x2_c)
# Dropout on embeddings
if self.args.dropout_emb > 0:
x1_emb = F.dropout(x1_emb, p=self.args.dropout_emb, training=self.training)
x2_emb = F.dropout(x2_emb, p=self.args.dropout_emb, training=self.training)
x1_c_emb = F.dropout(x1_c_emb, p=self.args.dropout_emb, training=self.training)
x2_c_emb = F.dropout(x2_c_emb, p=self.args.dropout_emb, training=self.training)
# Generate char features
x1_c_features = self.char_rnn(
x1_c_emb.reshape((x1_c_emb.size(0) * x1_c_emb.size(1), x1_c_emb.size(2), x1_c_emb.size(3))),
x1_mask.unsqueeze(2).repeat(1, 1, x1_c_emb.size(2)).reshape((x1_c_emb.size(0) * x1_c_emb.size(1), x1_c_emb.size(2)))
).reshape((x1_c_emb.size(0), x1_c_emb.size(1), x1_c_emb.size(2), -1))[:,:,-1,:]
x2_c_features = self.char_rnn(
x2_c_emb.reshape((x2_c_emb.size(0) * x2_c_emb.size(1), x2_c_emb.size(2), x2_c_emb.size(3))),
x2_mask.unsqueeze(2).repeat(1, 1, x2_c_emb.size(2)).reshape((x2_c_emb.size(0) * x2_c_emb.size(1), x2_c_emb.size(2)))
).reshape((x2_c_emb.size(0), x2_c_emb.size(1), x2_c_emb.size(2), -1))[:,:,-1,:]
# Combine input
crnn_input = [x1_emb, x1_c_features]
qrnn_input = [x2_emb, x2_c_features]
# Add manual features
if self.args.num_features > 0:
crnn_input.append(x1_f)
qrnn_input.append(x2_f)
# Encode document with RNN
c = self.encoding_rnn(torch.cat(crnn_input, 2), x1_mask)
# Encode question with RNN
q = self.encoding_rnn(torch.cat(qrnn_input, 2), x2_mask)
# Align and aggregate
c_check = c
for i in range(self.args.hop):
q_tilde = self.interactive_aligners[i].forward(c_check, q, x2_mask)
c_bar = self.interactive_SFUs[i].forward(c_check, torch.cat([q_tilde, c_check * q_tilde, c_check - q_tilde], 2))
c_tilde = self.self_aligners[i].forward(c_bar, x1_mask)
c_hat = self.self_SFUs[i].forward(c_bar, torch.cat([c_tilde, c_bar * c_tilde, c_bar - c_tilde], 2))
c_check = self.aggregate_rnns[i].forward(c_hat, x1_mask)
# Predict
start_scores, end_scores = self.mem_ans_ptr.forward(c_check, q, x1_mask, x2_mask)
return start_scores, end_scores