愈来愈以为基础过重要了,要成为一个合格的算法工程师而不是调包侠,必定要知道各个基础模型的HOW&WHY,毕竟那些模型都是当年的SOTA,他们的思想也对以后的NLP模型影响很大。最近找到了一个还不错的nlp-tutorial,准备抽时间过一遍基础模型,模型的大体思想以及数学公式可能就带过了,主要是实现上的细节。git
经过神经语言模型学习词向量,网络结构如图:github
解决了统计语言模型(n-gram model)的如下问题:算法
class NNLM(nn.Module):
def __init__(self):
super(NNLM, self).__init__()
self.C = nn.Embedding(n_class, m)
self.H = nn.Parameter(torch.randn(n_step * m, n_hidden).type(dtype))
self.W = nn.Parameter(torch.randn(n_step * m, n_class).type(dtype))
self.d = nn.Parameter(torch.randn(n_hidden).type(dtype))
self.U = nn.Parameter(torch.randn(n_hidden, n_class).type(dtype))
self.b = nn.Parameter(torch.randn(n_class).type(dtype))
def forward(self, X):
X = self.C(X)
X = X.view(-1, n_step * m) # [batch_size, n_step * emb_dim]
tanh = torch.tanh(self.d + torch.mm(X, self.H)) # [batch_size, n_hidden]
output = self.b + torch.mm(X, self.W) + torch.mm(tanh, self.U) # [batch_size, n_class]
return output复制代码
要注意的点:网络