-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathencoder_QI.py
executable file
·165 lines (119 loc) · 6.08 KB
/
encoder_QI.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import pdb
import math
import numpy as np
import torch.nn.functional as F
'''
class _netE(nn.Module):
"""Container module with an encoder, a recurrent module, and a decoder."""
def __init__(self, rnn_type, ninp, nhid, nlayers, dropout):
super(_netE, self).__init__()
self.d = dropout
self.rnn_type = rnn_type
self.nhid = nhid
self.nlayers = nlayers
self.nhid = nhid
self.ninp = ninp
self.img_embed = nn.Linear(4096, 512)
self.ques_rnn = getattr(nn, rnn_type)(ninp, nhid, nlayers, dropout=dropout)
self.fc1 = nn.Linear(self.nhid*2, self.nhid)
self.fc2 = nn.Linear(self.nhid, self.ninp)
def forward(self, ques_emb, img_raw, ques_hidden, rnd):
img_emb = F.dropout(F.tanh(self.img_embed(img_raw)), self.d, training=self.training)
ques_feat, ques_hidden = self.ques_rnn(ques_emb, ques_hidden)
ques_feat = F.dropout(ques_feat[-1], self.d, training=self.training)
concat_feat = torch.cat((ques_feat, img_emb),1)
encoder_feat = F.dropout(F.tanh(self.fc1(concat_feat)), self.d, training=self.training)
encoder_feat = F.dropout(self.fc2(encoder_feat), 0.3, training=self.training)
return encoder_feat, ques_hidden
def init_hidden(self, bsz):
weight = next(self.parameters()).data
if self.rnn_type == 'LSTM':
return (Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()),
Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()))
else:
return Variable(weight.new(self.nlayers, bsz, self.nhid).zero_())
'''
class _netE(nn.Module):
"""Container module with an encoder, a recurrent module, and a decoder."""
def __init__(self, rnn_type, ninp, nhid, nlayers, dropout):
super(_netE, self).__init__()
self.d = dropout
self.rnn_type = rnn_type
self.nhid = nhid
self.nlayers = nlayers
self.nhid = nhid
self.ninp = ninp
self.img_embed = nn.Linear(512, 512)
self.ques_rnn = getattr(nn, rnn_type)(ninp, nhid, nlayers, dropout=dropout)
self.Wq_2 = nn.Linear(self.nhid, self.nhid)
self.Wi_2 = nn.Linear(self.nhid, self.nhid)
self.Wa_2 = nn.Linear(self.nhid, 1)
self.fc1 = nn.Linear(self.nhid*2, self.ninp)
#self.fc2 = nn.Linear(self.nhid*2, self.ninp)
def forward(self, ques_emb, img_raw, ques_hidden, rnd):
img_emb = F.tanh(self.img_embed(img_raw))
ques_feat, ques_hidden = self.ques_rnn(ques_emb, ques_hidden)
#ques_feat = F.dropout(ques_feat[-1], self.d, training=self.training)
ques_feat = ques_feat[-1]
ques_emb_2 = self.Wq_2(ques_feat).view(-1, 1, self.nhid)
img_emb_2 = self.Wi_2(img_emb).view(-1, 49, self.nhid)
atten_emb_2 = F.tanh(img_emb_2 + ques_emb_2.expand_as(img_emb_2))
img_atten_weight = F.softmax(self.Wa_2(F.dropout(atten_emb_2, self.d, training=self.training
).view(-1, self.nhid)).view(-1, 49))
img_attn_feat = torch.bmm(img_atten_weight.view(-1, 1, 49),
img_emb.view(-1, 49, self.nhid))
concat_feat = F.dropout(torch.cat((img_attn_feat.view(-1, self.nhid), ques_feat), 1), self.d, training=self.training)
encoder_feat = F.tanh(self.fc1(concat_feat))
return encoder_feat, ques_hidden
def init_hidden(self, bsz):
weight = next(self.parameters()).data
if self.rnn_type == 'LSTM':
return (Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()),
Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()))
else:
return Variable(weight.new(self.nlayers, bsz, self.nhid).zero_())
'''
class _netE(nn.Module):
"""Container module with an encoder, a recurrent module, and a decoder."""
def __init__(self, rnn_type, ninp, nhid, nlayers, dropout):
super(_netE, self).__init__()
self.d = dropout
self.rnn_type = rnn_type
self.nhid = nhid
self.nlayers = nlayers
self.nhid = nhid
self.ninp = ninp
self.img_embed = nn.Linear(4096, 512)
self.ques_rnn = getattr(nn, rnn_type)(ninp, nhid, nlayers, dropout=dropout)
#self.Wq_2 = nn.Linear(self.nhid, self.nhid)
#self.Wi_2 = nn.Linear(self.nhid, self.nhid)
#self.Wa_2 = nn.Linear(self.nhid, 1)
#self.fc1 = nn.Linear(self.nhid, self.nhid)
self.fc2 = nn.Linear(self.nhid*2, self.ninp)
def forward(self, ques_emb, img_raw, ques_hidden, rnd):
img_emb = F.dropout(F.tanh(self.img_embed(img_raw)), self.d, training=self.training)
ques_feat, ques_hidden = self.ques_rnn(ques_emb, ques_hidden)
ques_feat = F.dropout(ques_feat[-1], self.d, training=self.training)
#ques_feat = ques_feat[-1]
#ques_emb_2 = self.Wq_2(ques_feat).view(-1, 1, self.nhid)
#img_emb_2 = self.Wi_2(img_emb).view(-1, 49, self.nhid)
#atten_emb_2 = F.tanh(img_emb_2 + ques_emb_2.expand_as(img_emb_2))
#img_atten_weight = F.softmax(self.Wa_2(F.dropout(atten_emb_2, self.d, training=self.training
# ).view(-1, self.nhid)).view(-1, 49))
#img_attn_feat = torch.bmm(img_atten_weight.view(-1, 1, 49),
# img_emb.view(-1, 49, self.nhid))
#encoder_feat = F.dropout(F.tanh(self.fc1(img_attn_feat.view(-1, self.nhid))), self.d, training=self.training)
concat_feat = torch.cat((img_emb, ques_feat), 1)
encoder_feat = F.dropout(F.tanh(self.fc2(concat_feat)), self.d, training=self.training)
return encoder_feat, ques_hidden
def init_hidden(self, bsz):
weight = next(self.parameters()).data
if self.rnn_type == 'LSTM':
return (Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()),
Variable(weight.new(self.nlayers, bsz, self.nhid).zero_()))
else:
return Variable(weight.new(self.nlayers, bsz, self.nhid).zero_())
'''