【Pytorch】Pytorch实例:基于词向量的语言模型实现

在Pytorch中,词嵌入使用函数nn.embedding:

class torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2, scale_grad_by_freq=False, sparse=False)

embedding使用的参数如下:

  • num_embeddings:词嵌入的字典大小
  • embedding_dim:词嵌入的维度
  • padding_idx:可选项,如果选择,对该index上的结果填充0
  • max_norm:可选项,如果选择,对词嵌入归一化时,设置归一化的最大值
  • norm_type:可选项,如果选择,对词嵌入归一化时,设置p-norm的p值
  • scale_grad_by_freq:可选项,如果选择,在mini-batch时,根据词频对梯度进行规整
  • sparse:可选项,如果选择,梯度w.r.t权值矩阵将是一个稀疏张量

词嵌入的简单实用例子如下:

import torch
import torch.nn as nn
import torch.autograd as autograd

word_to_ix = {"hello":0, "Pytorch":1}
embeds = nn.Embedding(2,5)
lookup_tensor = torch.LongTensor([word_to_ix["Pytorch"]])
Pytorch_embed = embeds(autograd.Variable(lookup_tensor))

下面使用Pytorch实现一个基于词向量的语言模型,具体代码如下:

1. 加载库和设置参数
import torch
import torch.nn as nn
import torch.autograd as autograd
import torch.nn.functional as F
import torch.optim as potim

torch.manual_seed(1)
CPNTEXT_SIZE=2  #表示想由前面的几个单词来预测这个单词
EMBEDDING_DIM=10
N_EPOCHES=10

2.数据准备
test_sentence = """ Word embedding ... """.split()
# 三元模型语料准备
trigrams = [([test_sentense[i], test_sentence[i+1]], test_sentence[i+2]) for i in range(len(test_sentence)-2)]
vocab = set(text_sentence) #去重
word_to_ix = {word:i for i, word in enumerate(vocab)}
# 这一部分是将单词三个分组,每个组前两个作为传入的数据,而最后一个作为预测的结果。给每个单词编码是为了用数字来表示每个单词,这样才能够传入word embedding得到词向量

3. 语言模型
class NGramLanguageModeler(nn.Module):
	def __init__(self, vocab_size, embedding_dim, context_size):
		super(NGramLanguageModeler, self).__init__()
		self.embedding = nn.Embedding(vocab_size, embedding_dim)
		self.linear1 = nn.Linear(context_size*embedding_dim, 128)
		self.linear2 = nn.Linear(128, vocab_size)

	def forward(self, inputs):
		embeds = self.embedding(inputs).view((1,-1))
		out = F.relu(self.linear1(embeds))
		out = self.linear2(out)
		log_probs = F.log_softmax(out)
		return log_probs

4. loss函数和优化器
losses = []
loss_function = nn.NLLLoss()
model = NGramLanguageModeler(len(vocab), EMBEDDING_DIM, CONTEXT_SIZE)
optimizer = optim.SGD(model.parameters(), lr=0.001)

5. 训练语言模型
for epoch in range(N_EPOCHES):
	total_loss = torch.Tensor([0])
	for context, target in trigrams:
		context_idxs = [word_to_ix[w] for w in context]
		context_var = autograd.Variable(torch.LongTensor(context_idxs))
		
		model.zero_grad()
		log_probs = model(context_var)
		loss = loss_function(log_probs, autograd.Variable(torch.LongTensor(word_to_ix[target])))
		loss.backward()
		optimizer.step()
		total_loss += loss.data 

你可能感兴趣的:(深度学习,python,pytorch)