|
import math |
|
import torch |
|
import torch.nn as nn |
|
|
|
class TransformerModel(nn.Module): |
|
def __init__(self, vocab_size, d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout=0.1): |
|
super(TransformerModel, self).__init__() |
|
self.model_type = 'Transformer' |
|
self.src_mask = None |
|
self.pos_encoder = PositionalEncoding(d_model, dropout) |
|
self.encoder = nn.Embedding(vocab_size, d_model) |
|
self.transformer = nn.Transformer(d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout) |
|
self.decoder = nn.Linear(d_model, vocab_size) |
|
|
|
def forward(self, src, tgt, src_mask=None, tgt_mask=None): |
|
src = self.encoder(src) * math.sqrt(self.d_model) |
|
src = self.pos_encoder(src) |
|
tgt = self.encoder(tgt) * math.sqrt(self.d_model) |
|
tgt = self.pos_encoder(tgt) |
|
output = self.transformer(src, tgt, src_mask, tgt_mask) |
|
output = self.decoder(output) |
|
return output |
|
|
|
class PositionalEncoding(nn.Module): |
|
def __init__(self, d_model, dropout=0.1, max_len=5000): |
|
super(PositionalEncoding, self).__init__() |
|
self.dropout = nn.Dropout(p=dropout) |
|
|
|
pe = torch.zeros(max_len, d_model) |
|
position = torch.arange(0, max_len, 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).transpose(0, 1) |
|
self.register_buffer('pe', pe) |
|
|
|
def forward(self, x): |
|
x = x + self.pe[:x.size(0), :] |
|
return self.dropout(x) |
|
|