-
Notifications
You must be signed in to change notification settings - Fork 8
/
attention.py
197 lines (169 loc) · 6.92 KB
/
attention.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
import torch
import torch.nn as nn
from torch.nn.utils.weight_norm import weight_norm
from fc import FCNet, GTH, get_norm
# Default concat, 1 layer, output layer
class Att_0(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_0, self).__init__()
norm_layer = get_norm(norm)
self.nonlinear = FCNet([v_dim + q_dim, num_hid], dropout= dropout, norm= norm, act= act)
self.linear = norm_layer(nn.Linear(num_hid, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.softmax(logits, 1)
return w
def logits(self, v, q):
num_objs = v.size(1)
q = q.unsqueeze(1).repeat(1, num_objs, 1)
vq = torch.cat((v, q), 2)
joint_repr = self.nonlinear(vq)
logits = self.linear(joint_repr)
return logits
# concat, 2 layer, output layer
class Att_1(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_1, self).__init__()
norm_layer = get_norm(norm)
self.nonlinear = FCNet([v_dim + q_dim, num_hid, num_hid], dropout= dropout, norm= norm, act= act)
self.linear = norm_layer(nn.Linear(num_hid, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.softmax(logits, 1)
return w
def logits(self, v, q):
num_objs = v.size(1)
q = q.unsqueeze(1).repeat(1, num_objs, 1)
vq = torch.cat((v, q), 2)
joint_repr = self.nonlinear(vq)
logits = self.linear(joint_repr)
return logits
# 1 layer seperate, element-wise *, output layer
class Att_2(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_2, self).__init__()
norm_layer = get_norm(norm)
self.v_proj = FCNet([v_dim, num_hid], dropout= dropout, norm= norm, act= act)
self.q_proj = FCNet([q_dim, num_hid], dropout= dropout, norm= norm, act= act)
self.linear = norm_layer(nn.Linear(q_dim, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.softmax(logits, 1)
return w
def logits(self, v, q):
batch, k, _ = v.size()
v_proj = self.v_proj(v) # [batch, k, num_hid]
q_proj = self.q_proj(q).unsqueeze(1).repeat(1, k, 1) # [batch, k, num_hid]
joint_repr = v_proj * q_proj
logits = self.linear(joint_repr)
return logits
# 1 layer seperate, element-wise *, 1 layer seperate, output layer
class Att_3(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_3, self).__init__()
norm_layer = get_norm(norm)
self.v_proj = FCNet([v_dim, num_hid], dropout= dropout, norm= norm, act= act)
self.q_proj = FCNet([q_dim, num_hid], dropout= dropout, norm= norm, act= act)
self.nonlinear = FCNet([num_hid, num_hid], dropout= dropout, norm= norm, act= act)
self.linear = norm_layer(nn.Linear(num_hid, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.softmax(logits, 1)
return w
def logits(self, v, q):
batch, k, _ = v.size()
v_proj = self.v_proj(v) # [batch, k, num_hid]
q_proj = self.q_proj(q).unsqueeze(1).repeat(1, k, 1) # [batch, k, num_hid]
joint_repr = v_proj * q_proj
joint_repr = self.nonlinear(joint_repr)
logits = self.linear(joint_repr)
return logits
# 1 layer seperate, element-wise *, 1 layer seperate, output layer
class Att_3S(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_3S, self).__init__()
norm_layer = get_norm(norm)
self.v_proj = FCNet([v_dim, num_hid], dropout=dropout, norm=norm, act=act)
self.q_proj = FCNet([q_dim, num_hid], dropout=dropout, norm=norm, act=act)
self.nonlinear = FCNet([num_hid, num_hid], dropout=dropout, norm=norm, act=act)
self.linear = norm_layer(nn.Linear(num_hid, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.sigmoid(logits)
#w = nn.functional.leaky_relu(logits)
return w
def logits(self, v, q):
batch, k, _ = v.size()
v_proj = self.v_proj(v) # [batch, k, num_hid]
q_proj = self.q_proj(q).unsqueeze(1).repeat(1, k, 1) # [batch, k, num_hid]
joint_repr = v_proj * q_proj
joint_repr = self.nonlinear(joint_repr)
logits = self.linear(joint_repr)
return logits
# concat w/ 2 layer seperate, element-wise *, output layer
class Att_PD(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_PD, self).__init__()
norm_layer = get_norm(norm)
self.nonlinear = FCNet([v_dim + q_dim, num_hid, num_hid], dropout= dropout, norm= norm, act= act)
self.nonlinear_gate = FCNet([v_dim + q_dim, num_hid, num_hid], dropout= dropout, norm= norm, act= 'Sigmoid')
self.linear = norm_layer(nn.Linear(num_hid, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.softmax(logits, 1)
return w
def logits(self, v, q):
num_objs = v.size(1)
q = q.unsqueeze(1).repeat(1, num_objs, 1)
vq = torch.cat((v, q), 2)
joint_repr = self.nonlinear(vq)
gate = self.nonlinear_gate(vq)
logits = joint_repr*gate
logits = self.linear(logits)
return logits
# concat w/ 1 layer seperate, element-wise *, output layer
class Att_P(nn.Module):
def __init__(self, v_dim, q_dim, num_hid, norm, act, dropout=0.0):
super(Att_P, self).__init__()
norm_layer = get_norm(norm)
self.gated_tanh = GTH( in_dim= v_dim + q_dim, out_dim= num_hid, dropout= dropout, norm= norm, act= act)
self.linear = norm_layer(nn.Linear(num_hid, 1), dim=None)
def forward(self, v, q):
"""
v: [batch, k, vdim]
q: [batch, qdim]
"""
logits = self.logits(v, q)
w = nn.functional.softmax(logits, 1)
return w
def logits(self, v, q):
num_objs = v.size(1)
q = q.unsqueeze(1).repeat(1, num_objs, 1)
vq = torch.cat((v, q), 2)
joint_repr = self.gated_tanh(vq)
logits = self.linear(joint_repr)
return logits