代码拉取完成,页面将自动刷新
同步操作将从 Hauk Zero/Transformer Demo 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import utils
from torch import nn
from pe import PositionEncoding
from mha import MultiHeadAttention
from ffn import FFN
class EncoderLayer(nn.Module):
def __init__(self, d_model, d_k, n_head, d_ff, dropout=0.5, device='cpu'):
super().__init__()
self.mha = MultiHeadAttention(d_model, d_k, d_k, n_head, device)
self.ffn = FFN(d_model, d_ff, dropout, device)
def forward(self, x, mask):
# x: (batch_size, n_seq, d_model)
x = self.mha(x, x, x, mask)
return self.ffn(x)
class Encoder(nn.Module):
def __init__(self, n, n_vocab, d_model,
d_k, n_head, d_ff, pad_token=0,
max_len=5000, dropout=0.5, device='cpu'):
super().__init__()
self.pad_token = pad_token
self.device = device
self.embd = nn.Embedding(n_vocab, d_model).to(device)
self.pe = PositionEncoding(d_model, max_len, dropout).to(device)
self.layers = nn.ModuleList([
EncoderLayer(d_model, d_k, n_head, d_ff, dropout, device)
for _ in range(n)
])
def forward(self, x):
# x: (batch_size, n_seq)
pad_mask = utils.get_pad_mask(x, x, self.pad_token).to(self.device)
mask = utils.bool_mask(pad_mask)
# x: (batch_size, n_seq, d_model)
x = self.embd(x)
x = self.pe(x)
for layer in self.layers:
x = layer(x, mask)
return x
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。