-
Notifications
You must be signed in to change notification settings - Fork 1
/
attentions.py
455 lines (375 loc) · 16.6 KB
/
attentions.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import torch
import torch.nn as nn
from dmultipit.base import BaseModel
from dmultipit.utils import masked_softmax
class MultiModalAttention(BaseModel):
"""
Attention mechanism for multimodal framework. Attention weights are computed with one specific network per modality
(with the possibility of sharing some layers) and normalized over all the modalities. Missing modalities are handled
by masking them.
Parameters
----------
dim_input: list of int
Input dimension for each modality
h1: int or list of int
Size of hidden layers.
* If shared_weights is True, h1 shoud be an integer refering to the same latent dimension for each modality
* If shared_weights is False, if h1 is an integer it refers to the same latent dimension for each modality,
otherwise it should be a list of integers of the same length as *dim_input* specifying the size of the latent
dimension for each input modality.
shared_weights: boolean
* If True, each modality is embedded in the same latent dimension and then passed through a single fully
connected layer, shared between all modalities.
* If False, each modality passes through its own fully connected layer after embedding.
The default is True.
p_dropout: float in [0, 1]
Dropout probability. The default is 0 (i.e., no dropout).
"""
def __init__(self, dim_input, h1, shared_weights=True, p_dropout=0):
super(MultiModalAttention, self).__init__()
self.dim_input = dim_input
self.h1 = h1
self.shared_weights = shared_weights
self.p_dropout = p_dropout
self.attention_norm = None
self._build_layers()
self.reset_weights()
def _build_layers(self):
assert type(self.h1) == int or float or list, "h1 should be a list or a number"
if self.shared_weights:
assert (type(self.h1) == int or float), "If shared_weights is true h1 should refer to a single latent size"
self.embeddings = nn.ModuleList(
[nn.Linear(d, self.h1, bias=False) for d in self.dim_input]
)
self.fc = nn.Sequential(
nn.Dropout(p=self.p_dropout), nn.Linear(self.h1, 1, bias=False)
)
else:
if type(self.h1) == list:
assert len(self.dim_input) == len(self.h1), "If h1 is a list it should be of the same size as dim_input"
self.embeddings = nn.ModuleList(
[nn.Linear(self.dim_input[i], self.h1[i], bias=False) for i in range(len(self.dim_input))]
)
self.fc = nn.ModuleList(
[nn.Sequential(nn.Dropout(p=self.p_dropout), nn.Linear(h, 1)) for h in self.h1]
)
else:
self.embeddings = nn.ModuleList(
[nn.Linear(d, self.h1, bias=False) for d in self.dim_input]
)
self.fc = nn.ModuleList(
[nn.Sequential(nn.Dropout(p=self.p_dropout), nn.Linear(self.h1, 1)) for _ in range(self.dim_input)]
)
@staticmethod
def _init_weights(m):
nn.init.xavier_normal_(m.weight)
return
def reset_weights(self):
if self.shared_weights:
for layer in self.embeddings:
self._init_weights(layer)
self._init_weights(self.fc[1])
else:
for i in range(len(self.embeddings)):
self._init_weights(self.embeddings[i])
self._init_weights(self.fc[i][1])
return self
def forward(self, x_list, mask):
"""
Forward function
Parameters
----------
x_list: list of tensors
Tensor for each modality of size (batch_size, modality_dimension)
mask: Boolean tensor of size (batch_size, n_modalities)
Indicate whether a modality is missing (i.e., 0) for each sample.
Returns
-------
attentions: tensor of size (batch_size, n_modalities)
Attention weights for each modality and for each sample of the batch
"""
if self.shared_weights:
emb = []
for i, t in enumerate(x_list):
emb.append(self.embeddings[i](t))
emb = torch.tanh(torch.stack(emb, dim=1))
att = self.fc(emb).squeeze(dim=-1) # torch.where(mask, self.fc(emb).squeeze(), torch.tensor(0.))
else:
att = []
for i, t in enumerate(x_list):
att.append(self.fc[i](torch.tanh(self.embeddings[i](t))))
att = torch.cat(att, dim=-1)
# att = torch.where(mask, torch.cat(att, dim=-1), torch.tensor(0.))
attentions, self.attention_norm = masked_softmax(att, mask, dim=-1)
return attentions # F.softmax(att, dim=-1)
class MultiModalGatedAttention(BaseModel):
"""
Gated attention mechanism for multimodal framework. Attention weights are computed with one specific network per
modality (with the possibility of sharing some layers) and normalized over all the modalities. Missing modalities
are handled by masking them.
Parameters
----------
dim_input: list of int
Input dimension for each modality
h1: int or list of int
Size of hidden layers.
* If shared_weights is True, h1 shoud be an integer refering to the same latent dimension for each modality
* If shared_weights is False, if h1 is an integer it refers to the same latent dimension for each modality,
otherwise it should be a list of integers of the same length as *dim_input* specifying the size of the latent
dimension for each input modality.
shared_weights: boolean
* If True, each modality is embedded in the same latent dimension and then passed through a single fully
connected layer, shared between all modalities.
* If False, each modality passes through its own fully connected layer after embedding.
The default is True.
p_dropout: float in [0, 1]
Dropout probability. The default is 0 (i.e., no dropout).
"""
def __init__(self, dim_input, h1, shared_weights=True, p_dropout=0):
super(MultiModalGatedAttention, self).__init__()
self.dim_input = dim_input
self.h1 = h1
self.shared_weights = shared_weights
self.p_dropout = p_dropout
self.attention_norm = None
self._build_layers()
self.reset_weights()
def _build_layers(self):
assert type(self.h1) == int or float or list, "h1 should be a list or a number"
if self.shared_weights:
assert (type(self.h1) == int or float), "If shared_weights is true h1 should refer to a single latent size"
self.embeddings_1 = nn.ModuleList(
[nn.Linear(d, self.h1, bias=False) for d in self.dim_input]
)
self.embeddings_2 = nn.ModuleList(
[nn.Linear(d, self.h1, bias=False) for d in self.dim_input]
)
self.fc = nn.Sequential(
nn.Dropout(p=self.p_dropout), nn.Linear(self.h1, 1, bias=False)
) # nn.Linear(self.h1, 1, bias=False)
else:
if type(self.h1) == list:
assert len(self.dim_input) == len(self.h1), "If h1 is a list it should be of the same size as dim_input"
self.embeddings_1 = nn.ModuleList(
[nn.Linear(self.dim_input[i], self.h1[i], bias=False) for i in range(len(self.dim_input))]
)
self.embeddings_2 = nn.ModuleList(
[nn.Linear(self.dim_input[i], self.h1[i], bias=False) for i in range(len(self.dim_input))]
)
self.fc = nn.ModuleList(
[nn.Sequential(nn.Dropout(p=self.p_dropout), nn.Linear(h, 1)) for h in self.h1]
)
else:
self.embeddings_1 = nn.ModuleList(
[nn.Linear(d, self.h1, bias=False) for d in self.dim_input]
)
self.embeddings_2 = nn.ModuleList(
[nn.Linear(d, self.h1, bias=False) for d in self.dim_input]
)
self.fc = nn.ModuleList(
[nn.Sequential(nn.Dropout(p=self.p_dropout), nn.Linear(self.h1, 1)) for _ in range(self.dim_input)]
)
return
@staticmethod
def _init_weights(m):
nn.init.xavier_normal_(m.weight)
return
def reset_weights(self):
for i in range(len(self.embeddings_1)):
self._init_weights(self.embeddings_1[i])
self._init_weights(self.embeddings_2[i])
if self.shared_weights:
self._init_weights(self.fc[1])
else:
for i in range(len(self.embeddings_1)):
self._init_weights(self.fc[i][1])
return self
def forward(self, x_list, mask):
"""
Forward function
Parameters
----------
x_list: list of tensors
Tensor for each modality of size (batch_size, modality_dimension)
mask: Boolean tensor of size (batch_size, n_modalities)
Indicate whether a modality is missing (i.e., 0) for each sample.
Returns
-------
attentions: tensor of size (batch_size, n_modalities)
Attention weights for each modality and for each sample of the batch
"""
if self.shared_weights:
emb = []
for i, t in enumerate(x_list):
emb.append(
torch.tanh(self.embeddings_1[i](t))
* torch.sigmoid(self.embeddings_2[i](t))
)
emb = torch.stack(emb, dim=1)
att = self.fc(emb).squeeze(dim=-1) # torch.where(mask, self.fc(emb).squeeze(), torch.tensor(0.))
else:
att = []
for i, t in enumerate(x_list):
att.append(
self.fc[i](
torch.tanh(self.embeddings_1[i](t))
* torch.sigmoid(self.embeddings_2[i](t))
)
)
att = torch.cat(att, dim=-1) # torch.where(mask, torch.cat(att, dim=-1), torch.tensor(0.))
attentions, self.attention_norm = masked_softmax(att, mask, dim=-1)
return attentions # F.softmax(att, dim=-1)
class Attention(BaseModel):
"""
Define a neural network to compute attention weights (see [1] for more details)
Parameters
----------
dim_input: int
Input size.
h1: int
Size of hidden layer.
dim_output: int
Output size. The default is 1.
References
----------
[1] Attention-based Deep Multiple Instance Learning - Ilse et al. 2018
Notes
-----
This attention mechanisms could be seen as a special case of the MultimodalAttention mechanism where each modality
would have the same input dimension and a single embedding layer would be shared across all modalities (i.e.,
self.fc1).
"""
def __init__(self, dim_input, h1, dim_output=1):
super(Attention, self).__init__()
self.fc1 = nn.Linear(dim_input, h1, bias=False)
nn.init.xavier_normal_(self.fc1.weight)
# nn.init.zeros_(self.fc1.bias)
self.fc2 = nn.Linear(h1, dim_output, bias=False)
nn.init.xavier_normal_(self.fc2.weight)
# nn.init.zeros_(self.fc2.bias)
self.attention_norm = None
def reset_weights(self):
nn.init.xavier_normal_(self.fc1.weight)
nn.init.xavier_normal_(self.fc2.weight)
return self
def forward(self, x, mask):
"""
Forward function
Parameters
----------
x: tensor of size (batch_size, n_modalities, dim_input)
mask: boolean tensor of size (batch_size, n_modalities)
Indicate whether a modality is missing (i.e., 0) for each sample.
Returns
-------
attentions: tensor of size (batch_size, n_modalities)
Attention weights for each modality and for each sample of the batch
"""
out = torch.tanh(self.fc1(x))
out = self.fc2(out).squeeze()
# out = torch.where(mask, torch.transpose(out, -1, -2), torch.tensor(0.)) # to deal with missing modalities
attentions, self.attention_norm = masked_softmax(out, mask, dim=-1)
return attentions # F.softmax(out, dim=-1) torch.transpose(out, -1, -2)
class GatedAttention(BaseModel):
"""
Define a neural network to compute attention weights with a gating mechanism
(see [1] for more details)
Parameters
----------
dim_input: int
Input size.
h1: int
Size of hidden layer.
dim_output: int
Output size.
References
----------
[1] Attention-based Deep Multiple Instance Learning - Ilse et al. 2018
Notes
-----
This attention mechanisms could be seen as a special case of the MultimodalGatedAttention mechanism where each
modality would have the same input dimension and a single embedding layer would be shared across all modalities
(i.e., self.fc1).
"""
def __init__(self, dim_input, h1, dim_output=1):
super(GatedAttention, self).__init__()
self.fc1 = nn.Linear(dim_input, h1, bias=False)
nn.init.xavier_normal_(self.fc1.weight)
# nn.init.zeros_(self.fc1.bias)
self.fc2 = nn.Linear(dim_input, h1, bias=False)
nn.init.xavier_normal_(self.fc2.weight)
# nn.init.zeros_(self.fc2.bias)
self.fc3 = nn.Linear(h1, dim_output, bias=False)
nn.init.xavier_normal_(self.fc3.weight)
# nn.init.zeros_(self.fc3.bias)
self.attention_norm = None
def reset_weights(self):
nn.init.xavier_normal_(self.fc1.weight)
nn.init.xavier_normal_(self.fc2.weight)
nn.init.xavier_normal_(self.fc3.weight)
return self
def forward(self, x, mask):
"""
Forward function
Parameters
----------
x: tensor of size (batch_size, n_modalities, dim_input)
mask: boolean tensor of size (batch_size, n_modalities)
Indicate whether a modality is missing (i.e., 0) for each sample.
Returns
-------
attentions: tensor of size (batch_size, n_modalities)
Attention weights for each modality and for each sample of the batch
"""
x_v = torch.tanh(self.fc1(x))
x_u = torch.sigmoid(self.fc2(x))
out = self.fc3(x_v * x_u).squeeze()
# out = torch.where(mask, torch.transpose(out, -1, -2), torch.tensor(0.))
attentions, self.attention_norm = masked_softmax(out, mask, dim=-1)
return attentions # F.softmax(out, dim=-1) torch.transpose(out, -1, -2)
class MSKCCAttention(BaseModel):
"""
Attention mechanism to reproduce experiments from Vanguri et al. (https://doi.org/10.1038/s43018-022-00416-8).
Parameters
----------
dim_input: list of int
Input dimension for each modality.
References
----------
1. Vanguri, R.S. et al. Multimodal integration of radiology, pathology and genomics for prediction of response to
PD-(L)1 blockade in patients with non-small cell lung cancer. Nat Cancer 3, 1151–1164 (2022).
(https://doi.org/10.1038/s43018-022-00416-8)
"""
def __init__(self, dim_input):
super(MSKCCAttention, self).__init__()
self.dim_input = dim_input
self.attention_layers = nn.ModuleList([nn.Linear(d, 1) for d in self.dim_input])
self.softplus = torch.nn.Softplus()
self.attention_norm = None
def reset_weights(self):
for layer in self.attention_layers:
# nn.init.kaiming_normal_(layer.weight)
nn.init.zeros_(layer.bias)
return self
def forward(self, x_list, mask):
"""
Forward function
Parameters
----------
x_list: list of tensors
Tensor for each modality of size (batch_size, modality_dimension)
mask: Boolean tensor of size (batch_size, n_modalities)
Indicate whether a modality is missing (i.e., 0) for each sample.
Returns
-------
attentions: tensor of size (batch_size, n_modalities)
Attention weights for each modality and for each sample of the batch
"""
attentions = []
for i, t in enumerate(x_list):
attentions.append(self.attention_layers[i](t) / self.dim_input[i])
attentions = torch.stack(attentions, dim=1).squeeze(dim=-1)
attentions_softplus = self.softplus(attentions)
attentions_scores = torch.where(mask, attentions_softplus, torch.tensor(0.0))
self.attention_norm = attentions_scores.norm(p=2, dim=-1).mean()
return torch.nn.functional.normalize(attentions_scores, p=1, dim=-1)