-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.py
executable file
·57 lines (51 loc) · 1.81 KB
/
routing.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
from typing import Final
import torch
class MMRouting(torch.nn.Module):
def __init__(
self,
*,
in_capsules: int = -1,
input_size: int = -1,
out_capsules: int = -1,
output_size: int = -1,
iterations: int = 10,
) -> None:
"""
Args:
in_capsuls:
Number of modality combinations (7 for three modalities).
input_size:
Input embedding dimension for each capsule.
out_capsules:
Number of concepts/classes.
output_size:
Output embedding dimension for each capsule.
"""
super().__init__()
self.in_capsules: Final = in_capsules
self.input_size: Final = input_size
self.out_capsules: Final = out_capsules
self.output_size: Final = output_size
self.iterations: Final = iterations
self.weights = torch.nn.Parameter(
torch.randn(in_capsules, input_size, out_capsules, output_size)
)
def forward(
self,
*,
f_bid: torch.Tensor,
p_bi: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
# b:batch, i: modalities, j: concepts, d: modality features, e: concept features
# initialize concepts
c_bj = torch.ones(1, device=f_bid.device, dtype=f_bid.dtype).expand(
f_bid.shape[0], self.out_capsules, self.output_size
)
r_bij = c_bj # for jit
f_W_bije = torch.einsum("bid, idje -> bije", f_bid, self.weights)
for _ in range(self.iterations):
r_bij = torch.nn.functional.softmax(
torch.einsum("bije, bje -> bij", f_W_bije, c_bj), dim=-1
)
c_bj = torch.einsum("bi, bij, bije -> bje", p_bi, r_bij, f_W_bije)
return c_bj, r_bij