-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathembeddings.py
More file actions
91 lines (74 loc) · 2.5 KB
/
Copy pathembeddings.py
File metadata and controls
91 lines (74 loc) · 2.5 KB
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
import torch.nn as nn
from dmultipit.base import BaseModel
class ModalityEmbedding(BaseModel):
"""
Unimodal embedding network
Parameters
---------
dim_input: int
Input dimension for the modality
h_sizes: list of int
Sizes of hidden layers. The network is composed of several hidden layers (of dimension specified by h_sizes) and
with ReLU activation. If empty the network consists of a single linear layer (dim_input -> dim_output).
p_dropout: float in [0, 1]
Probability dropout
dim_output: int
Output dimension
final_activation: string in ["sigmdoid", "tanh"]
"""
def __init__(
self,
dim_input,
h_sizes,
p_dropout=0.5,
dim_output=1,
final_activation="sigmoid",
):
super(ModalityEmbedding, self).__init__()
self.layers = nn.ModuleList()
input_size = dim_input
if len(h_sizes) > 0:
for h in h_sizes:
self.layers.append(nn.Linear(input_size, h))
self.layers.extend(nn.ModuleList([nn.ReLU(), nn.Dropout(p=p_dropout)]))
input_size = h
if final_activation == "sigmoid":
assert (dim_output == 1), "Sigmoid should only be used with a 1-dimensional output"
self.layers.extend(
nn.ModuleList(
[nn.Linear(input_size, dim_output), nn.Sigmoid()]
)
)
elif final_activation == "tanh":
assert (dim_output == 1), "Tanh should only be used with a 1-dimensional output"
self.layers.extend(
nn.ModuleList(
[nn.Linear(input_size, dim_output), nn.Tanh()]
)
)
else:
self.layers.extend(
nn.ModuleList(
[nn.Linear(input_size, dim_output), nn.ReLU(), nn.Dropout(p=p_dropout)]
)
)
self.reset_weights()
def reset_weights(self):
for i, l in enumerate(self.layers):
if isinstance(l, nn.Linear):
# nn.init.kaiming_normal_(l.weight)
nn.init.zeros_(l.bias)
return self
def forward(self, x):
"""
Forward function
Parameters
----------
x: tensor of size (batch_size, dim_input)
Returns
-------
Tensor of size (batch_size, dim_output)
"""
for i, l in enumerate(self.layers):
x = l(x)
return x