File size: 9,902 Bytes
ff43e05
 
 
 
 
 
 
 
 
 
 
 
c731c61
 
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
c731c61
ff43e05
 
 
 
 
c731c61
ff43e05
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
c731c61
ff43e05
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
c731c61
ff43e05
c731c61
ff43e05
 
 
c731c61
ff43e05
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff43e05
 
 
c731c61
 
 
 
ff43e05
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
 
 
 
 
ff43e05
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
c731c61
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
 
ff43e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c731c61
ff43e05
c731c61
ff43e05
c731c61
ff43e05
 
 
 
 
 
c731c61
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import math
import torch
import torch.nn as nn
import torch.nn.functional as F

from layers.fc import MLP
from layers.layer_norm import LayerNorm

# ------------------------------------
# ---------- Masking sequence --------
# ------------------------------------
def make_mask(feature):
    return (torch.sum(torch.abs(feature), dim=-1) == 0).unsqueeze(1).unsqueeze(2)


# ------------------------------
# ---------- Flattening --------
# ------------------------------


class AttFlat(nn.Module):
    def __init__(self, args, flat_glimpse, merge=False):
        super(AttFlat, self).__init__()
        self.args = args
        self.merge = merge
        self.flat_glimpse = flat_glimpse
        self.mlp = MLP(
            in_size=args.hidden_size,
            mid_size=args.ff_size,
            out_size=flat_glimpse,
            dropout_r=args.dropout_r,
            use_relu=True,
        )

        if self.merge:
            self.linear_merge = nn.Linear(
                args.hidden_size * flat_glimpse, args.hidden_size * 2
            )

    def forward(self, x, x_mask):
        att = self.mlp(x)
        if x_mask is not None:
            att = att.masked_fill(x_mask.squeeze(1).squeeze(1).unsqueeze(2), -1e9)
        att = F.softmax(att, dim=1)

        att_list = []
        for i in range(self.flat_glimpse):
            att_list.append(torch.sum(att[:, :, i : i + 1] * x, dim=1))

        if self.merge:
            x_atted = torch.cat(att_list, dim=1)
            x_atted = self.linear_merge(x_atted)

            return x_atted

        return torch.stack(att_list).transpose_(0, 1)


# ------------------------
# ---- Self Attention ----
# ------------------------


class SA(nn.Module):
    def __init__(self, args):
        super(SA, self).__init__()

        self.mhatt = MHAtt(args)
        self.ffn = FFN(args)

        self.dropout1 = nn.Dropout(args.dropout_r)
        self.norm1 = LayerNorm(args.hidden_size)

        self.dropout2 = nn.Dropout(args.dropout_r)
        self.norm2 = LayerNorm(args.hidden_size)

    def forward(self, y, y_mask):
        y = self.norm1(y + self.dropout1(self.mhatt(y, y, y, y_mask)))

        y = self.norm2(y + self.dropout2(self.ffn(y)))

        return y


# -------------------------------
# ---- Self Guided Attention ----
# -------------------------------


class SGA(nn.Module):
    def __init__(self, args):
        super(SGA, self).__init__()

        self.mhatt1 = MHAtt(args)
        self.mhatt2 = MHAtt(args)
        self.ffn = FFN(args)

        self.dropout1 = nn.Dropout(args.dropout_r)
        self.norm1 = LayerNorm(args.hidden_size)

        self.dropout2 = nn.Dropout(args.dropout_r)
        self.norm2 = LayerNorm(args.hidden_size)

        self.dropout3 = nn.Dropout(args.dropout_r)
        self.norm3 = LayerNorm(args.hidden_size)

    def forward(self, x, y, x_mask, y_mask):
        x = self.norm1(x + self.dropout1(self.mhatt1(v=x, k=x, q=x, mask=x_mask)))

        x = self.norm2(x + self.dropout2(self.mhatt2(v=y, k=y, q=x, mask=y_mask)))

        x = self.norm3(x + self.dropout3(self.ffn(x)))

        return x


# ------------------------------
# ---- Multi-Head Attention ----
# ------------------------------


class MHAtt(nn.Module):
    def __init__(self, args):
        super(MHAtt, self).__init__()
        self.args = args

        self.linear_v = nn.Linear(args.hidden_size, args.hidden_size)
        self.linear_k = nn.Linear(args.hidden_size, args.hidden_size)
        self.linear_q = nn.Linear(args.hidden_size, args.hidden_size)
        self.linear_merge = nn.Linear(args.hidden_size, args.hidden_size)

        self.dropout = nn.Dropout(args.dropout_r)

    def forward(self, v, k, q, mask):
        n_batches = q.size(0)
        v = (
            self.linear_v(v)
            .view(
                n_batches,
                -1,
                self.args.multi_head,
                int(self.args.hidden_size / self.args.multi_head),
            )
            .transpose(1, 2)
        )

        k = (
            self.linear_k(k)
            .view(
                n_batches,
                -1,
                self.args.multi_head,
                int(self.args.hidden_size / self.args.multi_head),
            )
            .transpose(1, 2)
        )

        q = (
            self.linear_q(q)
            .view(
                n_batches,
                -1,
                self.args.multi_head,
                int(self.args.hidden_size / self.args.multi_head),
            )
            .transpose(1, 2)
        )

        atted = self.att(v, k, q, mask)

        atted = (
            atted.transpose(1, 2)
            .contiguous()
            .view(n_batches, -1, self.args.hidden_size)
        )
        atted = self.linear_merge(atted)

        return atted

    def att(self, value, key, query, mask):
        d_k = query.size(-1)

        scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)

        if mask is not None:
            scores = scores.masked_fill(mask, -1e9)

        att_map = F.softmax(scores, dim=-1)
        att_map = self.dropout(att_map)

        return torch.matmul(att_map, value)


# ---------------------------
# ---- Feed Forward Nets ----
# ---------------------------


class FFN(nn.Module):
    def __init__(self, args):
        super(FFN, self).__init__()

        self.mlp = MLP(
            in_size=args.hidden_size,
            mid_size=args.ff_size,
            out_size=args.hidden_size,
            dropout_r=args.dropout_r,
            use_relu=True,
        )

    def forward(self, x):
        return self.mlp(x)


# ---------------------------
# ---- FF + norm  -----------
# ---------------------------
class FFAndNorm(nn.Module):
    def __init__(self, args):
        super(FFAndNorm, self).__init__()

        self.ffn = FFN(args)
        self.norm1 = LayerNorm(args.hidden_size)
        self.dropout2 = nn.Dropout(args.dropout_r)
        self.norm2 = LayerNorm(args.hidden_size)

    def forward(self, x):
        x = self.norm1(x)
        x = self.norm2(x + self.dropout2(self.ffn(x)))
        return x


class Block(nn.Module):
    def __init__(self, args, i):
        super(Block, self).__init__()
        self.args = args
        self.sa1 = SA(args)
        self.sa2 = SGA(args)
        self.sa3 = SGA(args)

        self.last = i == args.layer - 1
        if not self.last:
            self.att_lang = AttFlat(args, args.lang_seq_len, merge=False)
            self.att_audio = AttFlat(args, args.audio_seq_len, merge=False)
            self.att_vid = AttFlat(args, args.video_seq_len, merge=False)
            self.norm_l = LayerNorm(args.hidden_size)
            self.norm_a = LayerNorm(args.hidden_size)
            self.norm_v = LayerNorm(args.hidden_size)
            self.dropout = nn.Dropout(args.dropout_r)

    def forward(self, x, x_mask, y, y_mask, z, z_mask):

        ax = self.sa1(x, x_mask)
        ay = self.sa2(y, x, y_mask, x_mask)
        az = self.sa3(z, x, z_mask, x_mask)

        x = ax + x
        y = ay + y
        z = az + z

        if self.last:
            return x, y, z

        ax = self.att_lang(x, x_mask)
        ay = self.att_audio(y, y_mask)
        az = self.att_vid(z, y_mask)

        return (
            self.norm_l(x + self.dropout(ax)),
            self.norm_a(y + self.dropout(ay)),
            self.norm_v(z + self.dropout(az)),
        )


class Model_LAV(nn.Module):
    def __init__(self, args, vocab_size, pretrained_emb):
        super(Model_LAV, self).__init__()

        self.args = args

        # LSTM
        self.embedding = nn.Embedding(
            num_embeddings=vocab_size, embedding_dim=args.word_embed_size
        )

        # Loading the GloVe embedding weights
        self.embedding.weight.data.copy_(torch.from_numpy(pretrained_emb))

        self.lstm_x = nn.LSTM(
            input_size=args.word_embed_size,
            hidden_size=args.hidden_size,
            num_layers=1,
            batch_first=True,
        )

        # self.lstm_y = nn.LSTM(
        #     input_size=args.audio_feat_size,
        #     hidden_size=args.hidden_size,
        #     num_layers=1,
        #     batch_first=True
        # )

        # Feature size to hid size
        self.adapter_y = nn.Linear(args.audio_feat_size, args.hidden_size)
        self.adapter_z = nn.Linear(args.video_feat_size, args.hidden_size)

        # Encoder blocks
        self.enc_list = nn.ModuleList([Block(args, i) for i in range(args.layer)])

        # Flattenting features before proj
        self.attflat_ac = AttFlat(args, 1, merge=True)
        self.attflat_vid = AttFlat(args, 1, merge=True)
        self.attflat_lang = AttFlat(args, 1, merge=True)

        # Classification layers
        self.proj_norm = LayerNorm(2 * args.hidden_size)
        if self.args.task == "sentiment":
            if self.args.task_binary:
                self.proj = nn.Linear(2 * args.hidden_size, 2)
            else:
                self.proj = nn.Linear(2 * args.hidden_size, 7)
        if self.args.task == "emotion":
            self.proj = self.proj = nn.Linear(2 * args.hidden_size, 6)

    def forward(self, x, y, z):
        x_mask = make_mask(x.unsqueeze(2))
        y_mask = make_mask(y)
        z_mask = make_mask(z)

        embedding = self.embedding(x)

        x, _ = self.lstm_x(embedding)
        # y, _ = self.lstm_y(y)

        y, z = self.adapter_y(y), self.adapter_z(z)

        for i, dec in enumerate(self.enc_list):
            x_m, y_m, z_m = None, None, None
            if i == 0:
                x_m, y_m, z_m = x_mask, y_mask, z_mask
            x, y, z = dec(x, x_m, y, y_m, z, z_m)

        x = self.attflat_lang(x, None)

        y = self.attflat_ac(y, None)

        z = self.attflat_vid(z, None)

        # Classification layers
        proj_feat = x + y + z
        proj_feat = self.proj_norm(proj_feat)
        ans = self.proj(proj_feat)

        return ans