技术交流QQ群:1027579432,欢迎你的加入!
1.词向量介绍
- 在NLP任务中,第一步首先将自然语言转化成数学符号表示。一般常用的词汇表示方法:one-hot表示,这种方法是将每个单词表示为一个很长的向量,这个向量的长度是词汇表的大小,其中绝大数元素是0,只有一个元素是1,如“男人”表示为:[0 0 0 1 0 0 0 0 0 0...],“男孩”表示为:[0 1 0 0 0 0 0 0 0 0...]。one-hot方法采用稀疏的方式进行单词的表示,非常的简洁。即为每个单词分配一个数字ID号,数字ID号对应于每个单词在词汇表中的索引。比如“国王”这个词语在词汇表中的索引是3,“男孩”这个词语在词汇表中的索引是1。
- one-hot编码单词存在的问题:每个one-hot向量之间是相互正交的,任意两个单词之间是相互独立的,仅从one-hot表示出的词向量中无法看出两个单词之间是否有关系,即使是同义词也是相互独立。
- 大多数NLP任务中,一般用到的词向量并不是one-hot表示出来的维数很长的词向量,而是采用一种"Distributed Representation"的表示方法来表示一种低维实数向量。这个词向量的表示一般是这样的:[0.792,-0.177,-0.107,0.109,-0.542,...],维度以5o和100维比较常见,这种向量的表示也不唯一。
- Distributed Representation最大的贡献是让相关或相似的词,在距离上更接近。向量的距离可以是传统的欧式距离衡量,也可以使用余弦相似度cosine来衡量。
2.词向量的来历
- Distributed representation 最早是 Hinton 在 1986 年的论文《Learning distributed representations of concepts》中提出的。Distributed representation 用来表示词,通常被称为“Word Representation”或“Word Embedding”,中文俗称“词向量”或“词嵌入”。
- 如果用传统的稀疏表示法表示单词,在解决某些任务的时候(比如构建语言模型)会造成维数灾难[Bengio 2003]。使用低维的词向量就没这样的问题。同时从实践上看,高维的特征如果要套用 Deep Learning,其复杂度几乎是难以接受的,因此低维的词向量在这里也备受追捧。
3.词向量的训练
- 在介绍如何训练词向量之前,必须先介绍语言模型。因为对于大多数的训练方法,都是在训练语言模型的同时,顺便得到词向量的,词向量只是训练语言模型过程中的“意外之物”。从大量未标注的普通文本数据中无监督地学习出词向量,建立一个语言模型。
- 语言模型的作用就是检测一句话是不是正常人说出来的,语言模型意义重大。比如机器翻译、语音识别得到若干候选选项后,可以利用语言模型挑选一个尽量准确的结果。在其他NLP任务中都能用到。
- 语言模型形像化的解释是:给定一句话,看它是自然语言的概率P(w1,w2,w3,..,wt)是多少。w1到wt依次表示这句话中的每个单词,简单的推论是:P(w1,w2,w3...,wt)=P(w1)P(w2|w1)P(w3|w1,w2)*...P(wt|w1,w2,...,wt-1),常用的语言模型都是在求P(wt|w1,w2,...,wt-1)的值,比如n-gram模型就是用P(wt|w1,w2,...,wt-1)近似代替P(w1,w2,w3...,wt)
4.A Neural Probabilistic Language Model 原理解释
-
训练语言模型的最经典之作,要数 Bengio 等人在 2001 年发表在 NIPS 上的文章《A Neural Probabilistic Language Model》,Bengio 用了一个三层的神经网络来构建语言模型,同样也是 n-gram 模型,如下图所示。
Neural Probabilistic Language Model原理图.png - 目标:上图中最下方的wt-n+1,...,wt-2,wt-1就是前n-1个单词,现在根据这已知的n-1个单词预测下一个单词wt。
- 数学符号说明:
- C(w):表示单词w对应的词向量,整个模型中使用一套唯一的词向量。
- C:词向量C(w)存在于矩阵C(|V|*m)中,矩阵C的行数表示词汇表的大小;列数表示词向量C(w)的维度。矩阵C的某一行对应一个单词的词向量表示。
- |V|:表示词汇表的大小,即语料库中的单词总数
- m:表示词向量C(w)的维度,一般是50到100
- w到C(w)的转化:从矩阵C中取出一行
- d:隐藏层偏置bias(h)
- H: 隐藏层的权重(h*(n-1)m)
- U:隐藏层到输出层的权重(|V|*h)
- b:输出层的偏置bias(|V|)
- W:输入层到输出层权重(|V|*(n-1)m)
- h:隐藏层神经元的数量
- 网络的第一层(输入层)是将C(wt-n+1),...,C(wt-2),C(wt-1)这已知的n-1和单词的词向量首尾相连拼接起来,形成(n-1)w的向量,下面用x表示。
- 网络的第二层(隐藏层)直接用d+Hx计算得到,d是一个偏置项。之后,用tanh作为激活函数。
- 网络的第三层(输出层)一共有|V|个节点,每个节点yi表示下一个单词i的未归一化log概率。最后使用softmax函数将输出值y归一化成概率,最终y的计算公式如下:
y = b + Wx + Utanh(d+Hx) - 最后,用随机梯度下降法把这个模型优化出来就可以了。
5.代码实现---PyTorch版本
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-02-26 14:15:49 # @Author : cdl (1217096231@qq.com) # @Link : https://github.com/cdlwhm1217096231/python3_spider # @Version : $Id$ import torch import numpy as np import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.autograd import Variable """ 1.Basic Embedding Model 1-1. NNLM(Neural Network Language Model) """ dtype = torch.FloatTensor sentences = ["i like dog", "i love coffee", "i hate milk"] word_list = " ".join(sentences).split() # 制作词汇表 print(word_list) word_list = list(set(word_list)) # 去除词汇表中的重复元素 print("去重后的word_list:", word_list) word_dict = {w: i for i, w in enumerate(word_list)} # 将每个单词对应于相应的索引 number_dict = {i: w for i, w in enumerate(word_list)} # 将每个索引对应于相应的单词 n_class = len(word_dict) # 单词的总数 # NNLM parameters n_step = 2 # 根据前两个单词预测第3个单词 n_hidden = 2 # 隐藏层神经元的个数 m = 2 # 词向量的维度 # 由于pytorch中输入的数据是以batch小批量进行输入的,下面的函数就是将原始数据以一个batch为基本单位喂给模型 def make_batch(sentences): input_batch = [] target_batch = [] for sentence in sentences: word = sentence.split() input = [word_dict[w] for w in word[:-1]] target = word_dict[word[-1]] input_batch.append(input) target_batch.append(target) return input_batch, target_batch # Model class NNLM(nn.Module): def __init__(self): super(NNLM, self).__init__() self.C = nn.Embedding(n_class, embedding_dim=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) # x: [batch_size, n_step*n_class] tanh = torch.tanh(self.d + torch.mm(x, self.H)) # tanh: [batch_size, n_hidden] output = self.b + torch.mm(x, self.W) + torch.mm(tanh, self.U) # output: [batch_size, n_class] return output model = NNLM() criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) # 制作输入 input_batch, target_batch = make_batch(sentences) input_batch = Variable(torch.LongTensor(input_batch)) target_batch = Variable(torch.LongTensor(target_batch)) # 开始训练 for epoch in range(5000): optimizer.zero_grad() output = model(input_batch) # output : [batch_size, n_class], target_batch : [batch_size] (LongTensor, not one-hot) loss = criterion(output, target_batch) if (epoch + 1) % 1000 == 0: print("Epoch:{}".format(epoch + 1), "Loss:{:.3f}".format(loss)) loss.backward() optimizer.step() # 预测 predict = model(input_batch).data.max( 1, keepdim=True)[1] # [batch_size, n_class] print("predict: \n", predict) # 测试 print([sentence.split()[:2] for sentence in sentences], "---->", [number_dict[n.item()] for n in predict.squeeze()])
6.结果
['i', 'like', 'dog', 'i', 'love', 'coffee', 'i', 'hate', 'milk'] 去重后的word_list: ['coffee', 'i', 'hate', 'dog', 'love', 'milk', 'like'] Epoch:1000 Loss:0.114 Epoch:2000 Loss:0.021 Epoch:3000 Loss:0.007 Epoch:4000 Loss:0.003 Epoch:5000 Loss:0.002 predict: tensor([[3], [0], [5]]) [['i', 'like'], ['i', 'love'], ['i', 'hate']] ----> ['dog', 'coffee', 'milk'] [Finished in 4.5s]
7.代码实现----TensorFlow版本
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-02-26 21:25:01 # @Author : cdl (1217096231@qq.com) # @Link : https://github.com/cdlwhm1217096231/python3_spider # @Version : $Id$ import numpy as np import tensorflow as tf tf.reset_default_graph() sentences = ["i like coffee", "i love curry", "i hate apple"] word_list = " ".join(sentences).split() word_list = list(set(word_list)) print(word_list) word_dict = {w: i for i, w in enumerate(word_list)} number_dict = {i: w for i, w in enumerate(word_list)} n_class = len(word_dict) # Model parameters n_step = 2 n_hidden = 5 def make_batch(sentences): input_batch = [] target_batch = [] for sentence in sentences: words = sentence.split() input = [word_dict[word] for word in words[:-1]] target = word_dict[words[-1]] input_batch.append(np.eye(n_class)[input]) # np.eye()是单位对角阵 target_batch.append(np.eye(n_class)[target]) return input_batch, target_batch # Model # [batch_size, number of steps, number of Vocabulary] X = tf.placeholder(tf.float32, [None, n_step, n_class]) Y = tf.placeholder(tf.float32, [None, n_class]) # [batch_size, n_step * n_class] input = tf.reshape(X, shape=[-1, n_step * n_class]) H = tf.Variable(tf.random_normal([n_step * n_class, n_hidden])) d = tf.Variable(tf.random_normal([n_hidden])) U = tf.Variable(tf.random_normal([n_hidden, n_class])) b = tf.Variable(tf.random_normal([n_class])) tanh = tf.nn.tanh(d + tf.matmul(input, H)) # [batch_size, n_hidden] output = tf.matmul(tanh, U) + b # [batch_size, n_class] cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=output, labels=Y)) optimizer = tf.train.AdamOptimizer(0.001).minimize(cost) prediction = tf.argmax(output, 1) # Training init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) input_batch, target_batch = make_batch(sentences) for epoch in range(5000): _, loss = sess.run([optimizer, cost], feed_dict={ X: input_batch, Y: target_batch}) if (epoch + 1) % 1000 == 0: print("Epoch:{}".format(epoch + 1), "Cost:{:.4f}".format(loss)) # Predict predict = sess.run([prediction], feed_dict={X: input_batch}) # Test input = [sentence.split()[:2] for sentence in sentences] print([sentence.split()[:2] for sentence in sentences], '---->', [number_dict[n] for n in predict[0]])
8.结果
['like', 'love', 'apple', 'coffee', 'hate', 'curry', 'i'] Epoch:1000 Cost:0.1147 Epoch:2000 Cost:0.0324 Epoch:3000 Cost:0.0127 Epoch:4000 Cost:0.0057 Epoch:5000 Cost:0.0029 [['i', 'like'], ['i', 'love'], ['i', 'hate']] ----> ['coffee', 'curry', 'apple'] [Finished in 7.0s]
9.参考文献
1.Yoshua Bengio, Rejean Ducharme, Pascal Vincent, and Christian Jauvin. A neural probabilistic language model. Journal of Machine Learning Research (JMLR), 3:1137–1155, 2003.
2.LICSTAR的博客
3.A Neural Probabilistic Language Model CSDN博客