homemade_lo_vi / modules /positional_encoding.py
moiduy04's picture
Upload 18 files
bc1ada8
raw
history blame
1.35 kB
import math
import torch
import torch.nn as nn
from torch import Tensor
class PositionalEncoding(nn.Module):
r"""
Positional Encoding in "Attention Is All You Need" (section 3.5).
"Attention Is All You Need" uses sine and cosine functions of different frequencies:
PE_(pos, 2i) = sin(pos / power(10000, 2i / d_model))
PE_(pos, 2i+1) = cos(pos / power(10000, 2i / d_model))
only change is that calculations are done with -log(power(10000, 2i / d_model))
Uses OpenSpeech's PositionalEncoding, as I don't see the point in coding this from scratch.
"""
def __init__(self, d_model: int, dropout_p: float, max_length: int = 5000) -> None:
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout_p)
pe = torch.zeros(max_length, d_model, requires_grad=False)
position = torch.arange(0, max_length, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model))
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: Tensor) -> Tensor:
x = x + (self.pe[:, :x.shape[1], :]).requires_grad_(False)
return self.dropout(x)