forked from dreamgonfly/transformer-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembeddings.py
46 lines (38 loc) · 1.49 KB
/
embeddings.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
import torch
from torch import nn
import math
class PositionalEncoding(nn.Module):
"""
Implements the sinusoidal positional encoding for
non-recurrent neural networks.
Implementation based on "Attention Is All You Need"
:cite:`DBLP:journals/corr/VaswaniSPUJGKP17`
Args:
dropout_prob (float): dropout parameter
dim (int): embedding size
"""
def __init__(self, num_embeddings, embedding_dim, dim, dropout_prob=0., padding_idx=0, max_len=5000):
super(PositionalEncoding, self).__init__()
pe = torch.zeros(max_len, dim)
position = torch.arange(0, max_len).unsqueeze(1)
div_term = torch.exp((torch.arange(0, dim, 2) *
-(math.log(10000.0) / dim)).float())
pe[:, 0::2] = torch.sin(position.float() * div_term)
pe[:, 1::2] = torch.cos(position.float() * div_term)
pe = pe.unsqueeze(0)
self.num_embeddings = num_embeddings
self.embedding_dim = embedding_dim
self.embbedding = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
self.weight = self.embbedding.weight
self.register_buffer('pe', pe)
self.dropout = nn.Dropout(p=dropout_prob)
self.dim = dim
def forward(self, x, step=None):
x = self.embbedding(x)
x = x * math.sqrt(self.dim)
if step is None:
x = x + self.pe[:, :x.size(1)]
else:
x = x + self.pe[:, step]
x = self.dropout(x)
return x