-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
233 lines (192 loc) · 8.95 KB
/
layers.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
import numpy as np
import torch
from torch import nn
from utils import freeze_net
def MLP_factory(layer_sizes, dropout=False, layer_norm=False, activation='gelu', coral=False):
activation_classes = {'gelu': nn.GELU, 'relu': nn.ReLU, 'tanh': nn.Tanh}
modules = nn.ModuleList()
unpacked_sizes = []
for block in layer_sizes:
unpacked_sizes.extend([block[0]] * block[1])
for k in range(len(unpacked_sizes) - 1):
if layer_norm:
modules.append(nn.LayerNorm(unpacked_sizes[k]))
else:
modules.append(nn.Linear(unpacked_sizes[k], unpacked_sizes[k + 1]))
if k < len(unpacked_sizes) - 2:
modules.append(activation_classes[activation.lower()]())
if dropout is not False:
modules.append(nn.Dropout(dropout))
mlp = nn.Sequential(*modules)
return mlp
class TypedLinear(nn.Linear):
def __init__(self, in_features, out_features, n_type):
super().__init__(in_features, n_type * out_features)
self.in_features = in_features
self.out_features = out_features
self.n_type = n_type
def forward(self, X, type_ids=None):
"""
X: tensor of shape (*, in_features)
type_ids: long tensor of shape (*)
"""
output = super().forward(X)
if type_ids is None:
return output
output_shape = output.size()[:-1] + (self.out_features,)
output = output.view(-1, self.n_type, self.out_features)
idx = torch.arange(output.size(0), dtype=torch.long, device=type_ids.device)
output = output[idx, type_ids.view(-1)].view(*output_shape)
return output
class MLP(nn.Module):
"""
Multi-layer perceptron
Parameters
----------
num_layers: number of hidden layers
"""
activation_classes = {'gelu': nn.GELU, 'relu': nn.ReLU, 'tanh': nn.Tanh}
def __init__(self, input_size, hidden_size, output_size, num_layers, dropout, batch_norm=False,
init_last_layer_bias_to_zero=False, layer_norm=False, activation='gelu'):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.num_layers = num_layers
self.dropout = dropout
self.batch_norm = batch_norm
self.layer_norm = layer_norm
assert not (self.batch_norm and self.layer_norm)
self.layers = nn.Sequential()
for i in range(self.num_layers + 1):
n_in = self.input_size if i == 0 else self.hidden_size
n_out = self.hidden_size if i < self.num_layers else self.output_size
self.layers.add_module(f'{i}-Linear', nn.Linear(n_in, n_out))
if i < self.num_layers:
self.layers.add_module(f'{i}-Dropout', nn.Dropout(self.dropout))
if self.batch_norm:
self.layers.add_module(f'{i}-BatchNorm1d', nn.BatchNorm1d(self.hidden_size))
if self.layer_norm:
self.layers.add_module(f'{i}-LayerNorm', nn.LayerNorm(self.hidden_size))
self.layers.add_module(f'{i}-{activation}', self.activation_classes[activation.lower()]())
if init_last_layer_bias_to_zero:
self.layers[-1].bias.data.fill_(0)
def forward(self, input):
return self.layers(input)
class MatrixVectorScaledDotProductAttention(nn.Module):
def __init__(self, temperature, attn_dropout=0.1):
super().__init__()
self.temperature = temperature
self.dropout = nn.Dropout(attn_dropout)
self.softmax = nn.Softmax(dim=1)
def forward(self, q, k, v, mask=None, return_raw_attn=False):
"""
q: tensor of shape (n*b, d_k)
k: tensor of shape (n*b, l, d_k)
v: tensor of shape (n*b, l, d_v)
returns: tensor of shape (n*b, d_v), tensor of shape(n*b, l)
"""
attn = (q.unsqueeze(1) * k).sum(2) # (n*b, l)
attn = attn / self.temperature
if mask is not None:
attn = attn.masked_fill(mask, -np.inf)
if return_raw_attn:
raw_attn = torch.clone(attn)
attn = self.softmax(attn)
attn = self.dropout(attn)
output = (attn.unsqueeze(2) * v).sum(1)
if return_raw_attn:
return output, (attn, raw_attn)
else:
return output, attn
class AttPoolLayer(nn.Module):
def __init__(self, d_q, d_k, dropout=0.1):
super().__init__()
self.w_qs = nn.Linear(d_q, d_k)
nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_q + d_k)))
self.attention = MatrixVectorScaledDotProductAttention(temperature=np.power(d_k, 0.5))
self.dropout = nn.Dropout(dropout)
def forward(self, q, k, mask=None, return_raw_attn=False):
"""
q: tensor of shape (b, d_q)
k: tensor of shape (b, l, d_k)
mask: tensor of shape (b, l) (optional, default None)
returns: tensor of shape (b, d_k)
"""
qs = self.w_qs(q) # (b, d_k)
output, attn = self.attention(qs, k, k, mask=mask, return_raw_attn=return_raw_attn)
output = self.dropout(output)
return output, attn
class MultiheadAttPoolLayer(nn.Module):
def __init__(self, n_head, d_q_original, d_k_original, dropout=0.1):
super().__init__()
assert d_k_original % n_head == 0 # make sure the outpute dimension equals to d_k_origin
self.n_head = n_head
self.d_k = d_k_original // n_head
self.d_v = d_k_original // n_head
self.w_qs = nn.Linear(d_q_original, n_head * self.d_k)
self.w_ks = nn.Linear(d_k_original, n_head * self.d_k)
self.w_vs = nn.Linear(d_k_original, n_head * self.d_v)
nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_q_original + self.d_k)))
nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 / (d_k_original + self.d_k)))
nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 / (d_k_original + self.d_v)))
self.attention = MatrixVectorScaledDotProductAttention(temperature=np.power(self.d_k, 0.5))
self.dropout = nn.Dropout(dropout)
def forward(self, q, k, mask=None, return_raw_attn=False):
"""
q: tensor of shape (b, d_q_original)
k: tensor of shape (b, l, d_k_original)
mask: tensor of shape (b, l) (optional, default None)
returns: tensor of shape (b, n*d_v)
"""
n_head, d_k, d_v = self.n_head, self.d_k, self.d_v
bs, _ = q.size()
bs, len_k, _ = k.size()
qs = self.w_qs(q).view(bs, n_head, d_k) # (b, n, dk)
ks = self.w_ks(k).view(bs, len_k, n_head, d_k) # (b, l, n, dk)
vs = self.w_vs(k).view(bs, len_k, n_head, d_v) # (b, l, n, dv)
qs = qs.permute(1, 0, 2).contiguous().view(n_head * bs, d_k)
ks = ks.permute(2, 0, 1, 3).contiguous().view(n_head * bs, len_k, d_k)
vs = vs.permute(2, 0, 1, 3).contiguous().view(n_head * bs, len_k, d_v)
if mask is not None:
mask = mask.repeat(n_head, 1)
output, attn = self.attention(qs, ks, vs, mask=mask, return_raw_attn=return_raw_attn)
output = output.view(n_head, bs, d_v)
output = output.permute(1, 0, 2).contiguous().view(bs, n_head * d_v) # (b, n*dv)
output = self.dropout(output)
return output, attn
class CustomizedEmbedding(nn.Module):
def __init__(self, concept_num, concept_in_dim, concept_out_dim, use_contextualized,
pretrained_concept_emb=None, freeze_ent_emb=True, scale=1.0, init_range=0.02):
super().__init__()
self.scale = scale
self.use_contextualized = use_contextualized
if not use_contextualized:
self.emb = nn.Embedding(concept_num, concept_in_dim)
if pretrained_concept_emb is not None:
self.emb.weight.data.copy_(pretrained_concept_emb)
else:
self.emb.weight.data.normal_(mean=0.0, std=init_range)
if freeze_ent_emb:
freeze_net(self.emb)
if concept_in_dim != concept_out_dim:
self.cpt_transform = nn.Linear(concept_in_dim, concept_out_dim)
self.activation = nn.GELU()
def forward(self, index, contextualized_emb=None):
"""
index: size (bz, a)
contextualized_emb: size (bz, b, emb_size) (optional)
"""
if contextualized_emb is not None:
assert index.size(0) == contextualized_emb.size(0)
if hasattr(self, 'cpt_transform'):
contextualized_emb = self.activation(self.cpt_transform(contextualized_emb * self.scale))
else:
contextualized_emb = contextualized_emb * self.scale
emb_dim = contextualized_emb.size(-1)
return contextualized_emb.gather(1, index.unsqueeze(-1).expand(-1, -1, emb_dim))
else:
if hasattr(self, 'cpt_transform'):
return self.activation(self.cpt_transform(self.emb(index) * self.scale))
else:
return self.emb(index) * self.scale