-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgloss_encoder.py
216 lines (175 loc) · 6.87 KB
/
gloss_encoder.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
# coding: utf-8
import torch.nn as nn
import torch
from torch import Tensor
import math
class MultiHeadedAttention(nn.Module):
def __init__(self, num_heads: int, size: int, dropout: float = 0.1):
"""
Create a multi-headed attention layer.
:param num_heads: the number of heads
:param size: model size (must be divisible by num_heads)
:param dropout: probability of dropping a unit
"""
super(MultiHeadedAttention, self).__init__()
assert size % num_heads == 0
self.head_size = head_size = size // num_heads
self.model_size = size
self.num_heads = num_heads
self.k_layer = nn.Linear(size, num_heads * head_size)
self.v_layer = nn.Linear(size, num_heads * head_size)
self.q_layer = nn.Linear(size, num_heads * head_size)
self.output_layer = nn.Linear(size, size)
self.softmax = nn.Softmax(dim=-1)
self.dropout = nn.Dropout(dropout)
def forward(self, k: Tensor, v: Tensor, q: Tensor, mask: Tensor = None):
"""
Computes multi-headed attention.
:param k: keys [B, M, D] with M being the sentence length.
:param v: values [B, M, D]
:param q: query [B, M, D]
:param mask: optional mask [B, 1, M]
:return:
"""
batch_size = k.size(0)
num_heads = self.num_heads
# project the queries (q), keys (k), and values (v)
k = self.k_layer(k)
v = self.v_layer(v)
q = self.q_layer(q)
# reshape q, k, v for our computation to [batch_size, num_heads, ..]
k = k.view(batch_size, -1, num_heads, self.head_size).transpose(1, 2)
v = v.view(batch_size, -1, num_heads, self.head_size).transpose(1, 2)
q = q.view(batch_size, -1, num_heads, self.head_size).transpose(1, 2)
# compute scores
q = q / math.sqrt(self.head_size)
# batch x num_heads x query_len x key_len
scores = torch.matmul(q, k.transpose(2, 3))
# apply the mask (if we have one)
# we add a dimension for the heads to it below: [B, 1, 1, M]
if mask is not None:
scores = scores.masked_fill(~mask.unsqueeze(1), float("-inf"))
# apply attention dropout and compute context vectors.
attention = self.softmax(scores)
attention = self.dropout(attention)
# get context vector (select values with attention) and reshape
# back to [B, M, D]
context = torch.matmul(attention, v)
context = (
context.transpose(1, 2)
.contiguous()
.view(batch_size, -1, num_heads * self.head_size)
)
output = self.output_layer(context)
return output
class PositionalEmbedding(nn.Module):
def __init__(self, d_model, max_len=5000):
super().__init__()
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model).float()
pe.require_grad = False
position = torch.arange(0, max_len).float().unsqueeze(1)
div_term = (torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model)).exp()
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
return self.pe[:, :x.size(1)]
class PositionwiseFeedForward(nn.Module):
def __init__(self, input_size, ff_size, dropout=0.1):
"""
Initializes position-wise feed-forward layer.
:param input_size: dimensionality of the input.
:param ff_size: dimensionality of intermediate representation
:param dropout:
"""
super(PositionwiseFeedForward, self).__init__()
self.layer_norm = nn.LayerNorm(input_size, eps=1e-6)
self.pwff_layer = nn.Sequential(
nn.Linear(input_size, ff_size),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(ff_size, input_size),
nn.Dropout(dropout),
)
def forward(self, x):
x_norm = self.layer_norm(x)
return self.pwff_layer(x_norm) + x
class TransformerEncoderLayer(nn.Module):
def __init__(self, size: int = 0, ff_size: int = 0, num_heads: int = 0, dropout: float = 0.1):
super(TransformerEncoderLayer, self).__init__()
self.layer_norm = nn.LayerNorm(size, eps=1e-6)
self.src_src_att = MultiHeadedAttention(num_heads, size, dropout=dropout)
self.feed_forward = PositionwiseFeedForward(
input_size=size, ff_size=ff_size, dropout=dropout
)
self.dropout = nn.Dropout(dropout)
self.size = size
def forward(self, x: Tensor, mask: Tensor) -> Tensor:
x_norm = self.layer_norm(x)
h = self.src_src_att(x_norm, x_norm, x_norm, mask)
h = self.dropout(h) + x
o = self.feed_forward(h)
return o
class Encoder(nn.Module):
"""
Base encoder class
"""
@property
def output_size(self):
"""
Return the output size
:return:
"""
return self._output_size
class SelfAttentionAdapter(Encoder):
def __init__(
self,
hidden_size: int = 768,
ff_size: int = 2048,
num_layers: int = 1,
num_heads: int = 4,
dropout: float = 0.1,
emb_dropout: float = 0.1
):
"""
Initializes the Transformer.
:param hidden_size: hidden size and size of embeddings
:param ff_size: position-wise feed-forward layer size.
(Typically this is 2*hidden_size.)
:param num_layers: number of layers
:param num_heads: number of heads for multi-headed attention
:param dropout: dropout probability for Transformer layers
:param emb_dropout: Is applied to the input (word embeddings).
"""
super(SelfAttentionAdapter, self).__init__()
# build all (num_layers) layers
self.layers = nn.ModuleList(
[
TransformerEncoderLayer(
size=hidden_size,
ff_size=ff_size,
num_heads=num_heads,
dropout=dropout,
)
for _ in range(num_layers)
]
)
self.layer_norm = nn.LayerNorm(hidden_size, eps=1e-6)
# for position emb
self.dropout_func = nn.Dropout(p=dropout)
self.pe = PositionalEmbedding(d_model=hidden_size)
# pylint: disable=arguments-differ
def forward(self, SeqEmb: Tensor, mask: Tensor):
SeqEmb = self.dropout_func(SeqEmb + self.pe(SeqEmb))
for layer in self.layers:
SeqEmb = layer(SeqEmb, mask)
SeqEmb = self.layer_norm(SeqEmb)
return SeqEmb
def __repr__(self):
return "%s(num_layers=%r, num_heads=%r)" % (
self.__class__.__name__,
len(self.layers),
self.layers[0].src_src_att.num_heads,
)