-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.py
217 lines (156 loc) · 6.66 KB
/
model.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
import torch
from torch.autograd import Variable
from torch import nn
from torch.nn.init import kaiming_uniform_, xavier_uniform_, normal
import torch.nn.functional as F
def linear(in_dim, out_dim, bias=True):
lin = nn.Linear(in_dim, out_dim, bias=bias)
xavier_uniform_(lin.weight)
if bias:
lin.bias.data.zero_()
return lin
class ControlUnit(nn.Module):
def __init__(self, dim, max_step):
super().__init__()
self.position_aware = nn.ModuleList()
for i in range(max_step):
self.position_aware.append(linear(dim * 2, dim))
self.control_question = linear(dim * 2, dim)
self.attn = linear(dim, 1)
self.dim = dim
def forward(self, step, context, question, control):
position_aware = self.position_aware[step](question)
control_question = torch.cat([control, position_aware], 1)
control_question = self.control_question(control_question)
control_question = control_question.unsqueeze(1)
context_prod = control_question * context
attn_weight = self.attn(context_prod)
attn = F.softmax(attn_weight, 1)
next_control = (attn * context).sum(1)
return next_control
class ReadUnit(nn.Module):
def __init__(self, dim):
super().__init__()
self.mem = linear(dim, dim)
self.concat = linear(dim * 2, dim)
self.attn = linear(dim, 1)
def forward(self, memory, know, control):
mem = self.mem(memory[-1]).unsqueeze(2)
concat = self.concat(torch.cat([mem * know, know], 1) \
.permute(0, 2, 1))
attn = concat * control[-1].unsqueeze(1)
attn = self.attn(attn).squeeze(2)
attn = F.softmax(attn, 1).unsqueeze(1)
read = (attn * know).sum(2)
return read
class WriteUnit(nn.Module):
def __init__(self, dim, self_attention=False, memory_gate=False):
super().__init__()
self.concat = linear(dim * 2, dim)
if self_attention:
self.attn = linear(dim, 1)
self.mem = linear(dim, dim)
if memory_gate:
self.control = linear(dim, 1)
self.self_attention = self_attention
self.memory_gate = memory_gate
def forward(self, memories, retrieved, controls):
prev_mem = memories[-1]
concat = self.concat(torch.cat([retrieved, prev_mem], 1))
next_mem = concat
if self.self_attention:
controls_cat = torch.stack(controls[:-1], 2)
attn = controls[-1].unsqueeze(2) * controls_cat
attn = self.attn(attn.permute(0, 2, 1))
attn = F.softmax(attn, 1).permute(0, 2, 1)
memories_cat = torch.stack(memories, 2)
attn_mem = (attn * memories_cat).sum(2)
next_mem = self.mem(attn_mem) + concat
if self.memory_gate:
control = self.control(controls[-1])
gate = F.sigmoid(control)
next_mem = gate * prev_mem + (1 - gate) * next_mem
return next_mem
class MACUnit(nn.Module):
def __init__(self, dim, max_step=12,
self_attention=False, memory_gate=False,
dropout=0.15):
super().__init__()
self.control = ControlUnit(dim, max_step)
self.read = ReadUnit(dim)
self.write = WriteUnit(dim, self_attention, memory_gate)
self.mem_0 = nn.Parameter(torch.zeros(1, dim))
self.control_0 = nn.Parameter(torch.zeros(1, dim))
self.dim = dim
self.max_step = max_step
self.dropout = dropout
def get_mask(self, x, dropout):
mask = torch.empty_like(x).bernoulli_(1 - dropout)
mask = mask / (1 - dropout)
return mask
def forward(self, context, question, knowledge):
b_size = question.size(0)
control = self.control_0.expand(b_size, self.dim)
memory = self.mem_0.expand(b_size, self.dim)
if self.training:
control_mask = self.get_mask(control, self.dropout)
memory_mask = self.get_mask(memory, self.dropout)
control = control * control_mask
memory = memory * memory_mask
controls = [control]
memories = [memory]
for i in range(self.max_step):
control = self.control(i, context, question, control)
if self.training:
control = control * control_mask
controls.append(control)
read = self.read(memories, knowledge, controls)
memory = self.write(memories, read, controls)
if self.training:
memory = memory * memory_mask
memories.append(memory)
return memory
class MACNetwork(nn.Module):
def __init__(self, n_vocab, dim, embed_hidden=300,
max_step=12, self_attention=False, memory_gate=False,
classes=28, dropout=0.15):
super().__init__()
self.conv = nn.Sequential(nn.Conv2d(1024, dim, 3, padding=1),
nn.ELU(),
nn.Conv2d(dim, dim, 3, padding=1),
nn.ELU())
self.embed = nn.Embedding(n_vocab, embed_hidden)
self.lstm = nn.LSTM(embed_hidden, dim,
batch_first=True, bidirectional=True)
self.lstm_proj = nn.Linear(dim * 2, dim)
self.mac = MACUnit(dim, max_step,
self_attention, memory_gate, dropout)
self.classifier = nn.Sequential(linear(dim * 3, dim),
nn.ELU(),
linear(dim, classes))
self.max_step = max_step
self.dim = dim
self.reset()
def reset(self):
self.embed.weight.data.uniform_(0, 1)
kaiming_uniform_(self.conv[0].weight)
self.conv[0].bias.data.zero_()
kaiming_uniform_(self.conv[2].weight)
self.conv[2].bias.data.zero_()
kaiming_uniform_(self.classifier[0].weight)
def forward(self, image, question, question_len, dropout=0.15):
b_size = question.size(0)
img = self.conv(image)
img = img.view(b_size, self.dim, -1)
embed = self.embed(question)
embed = nn.utils.rnn.pack_padded_sequence(embed, question_len,
batch_first=True)
lstm_out, (h, _) = self.lstm(embed)
lstm_out, _ = nn.utils.rnn.pad_packed_sequence(lstm_out,
batch_first=True)
lstm_out = self.lstm_proj(lstm_out)
h = h.permute(1, 0, 2).contiguous().view(b_size, -1)
memory = self.mac(lstm_out, h, img)
out = torch.cat([memory, h], 1)
out = self.classifier(out)
return out