word2vec词向量原理

word2vec词向量原理

    • 一、词的表示
    • 1.1离散表示(one-hot representation)
    • 1.2分布式表示(distribution representation)

一、词的表示

word2vec也叫word embeddings,中文名“词向量”,作用就是将自然语言中的字词转为计算机可以理解的稠密向量(Dense
Vector)。在word2vec出现之前,自然语言处理经常把字词转为离散的单独的符号,也就是One-Hot Encoder。

在自然语言处理任务中,首先需要考虑词如何在计算机中表示。通常,有两种表示方式:one-hot representation和distribution representation。

1.1离散表示(one-hot representation)

传统的基于规则或基于统计的自然语义处理方法将单词看作一个原子符号 被称作one-hot representation。
one-hot representation把每个词表示为一个长向量。这个向量的维度是词表大小,向量中只有一个维度的值为1,其余维度为0,这个维度就代表了当前的词。

例如: 苹果 [0,0,0,1,0,0,0,0,0,……]

one-hot representation相当于给每个词分配一个id,这就导致这种表示方式不能展示词与词之间的关系。另外,one-hot representation将会导致特征空间非常大,但也带来一个好处,就是在高维空间中,很多应用任务线性可分。

1.2分布式表示(distribution representation)

word embedding指的是将词转化成一种分布式表示,又称词向量。分布
式表示将词表示成一个定长的连续的稠密向量。

分布式表示优点:
(1)词之间存在相似关系:
是词之间存在“距离”概念,这对很多自然语言处理的任务非常有帮助。
(2)包含更多信息:
词向量能够包含更多信息,并且每一维都有特定的含义。在采用one-hot特征时,可以对特征向量进行删减,词向量则不能。

模型拆解
word2vec模型其实就是简单化的神经网络。

word2vec词向量原理_第1张图片
输入是One-Hot Vector,Hidden Layer没有激活函数,也就是线性的单元。Output Layer维度跟Input Layer的维度一样,用的是Softmax回归。我们要获取的dense vector其实就是Hidden Layer的输出单元。有的地方定为Input Layer和Hidden Layer之间的权重,其实说的是一回事。
word2vec词向量原理_第2张图片

CBOW与Skip-Gram模式
word2vec主要分为CBOW(Continuous Bag of Words)和Skip-Gram两种模式。CBOW是从原始语句推测目标字词;而Skip-Gram正好相反,是从目标字词推测出原始语句。CBOW对小型数据库比较合适,而Skip-Gram在大型语料中表现更好。
对同样一个句子:Hangzhou is a nice city。我们要构造一个语境与目标词汇的映射关系,其实就是input与label的关系。
这里假设滑窗尺寸为1(滑窗尺寸……这个……不懂自己google吧-_-|||)
CBOW可以制造的映射关系为:[Hangzhou,a]—>is,[is,nice]—>a,[a,city]—>nice
Skip-Gram可以制造的映射关系为(is,Hangzhou),(is,a),(a,is), (a,nice),(nice,a),(nice,city)

训练优化
额,到这里,你可能会注意到,这个训练过程的参数规模非常巨大。假设语料库中有30000个不同的单词,hidden
layer取128,word2vec两个权值矩阵维度都是[30000,128],在使用SGD对庞大的神经网络进行学习时,将是十分缓慢的。而且,你需要大量的训练数据来调整许多权重,避免过度拟合。数以百万计的重量数十亿倍的训练样本意味着训练这个模型将是一个野兽。
一般来说,有Hierarchical Softmax、Negative Sampling等方式来解决。

三、实战

"""
Created on 2019/4/10
@author: Michael
"""

"""
1 简介  :根据官方给的数据集中'的word_seg'内容,训练词向量,生成word_idx_dict和vectors_arr两个结果,并保存
2 注意  :1)需要16g内存的电脑,否则由于数据量大,会导致内存溢出。(解决方案:可通过迭代器的格式读入数据。
            见https://rare-technologies.com/word2vec-tutorial/#online_training__resuming)
"""

import pandas as pd
import gensim
import time
import pickle
import numpy as np
import csv, sys

vector_size = 100

maxInt = sys.maxsize
decrement = True
while decrement:
    # decrease the maxInt value by factor 10
    # as long as the OverflowError occurs.
    decrement = False
    try:
        csv.field_size_limit(maxInt)
    except OverflowError:
        maxInt = int(maxInt / 10)
        decrement = True


# =======================================================================================================================
# 0 辅助函数
# =======================================================================================================================

def sentence2list(sentence):
    return sentence.strip().split()


start_time = time.time()

data_path = 'D:/ML/competition/daguan/new_data/'
feature_path = 'D:/ML/competition/daguan/new_data/'
# =======================================================================================================================
# 1 准备训练数据
# =======================================================================================================================

print("准备数据................ ")
df_train = pd.read_csv(data_path + 'train_set.csv', engine='python')
df_test = pd.read_csv(data_path + 'test_set.csv', engine='python')
sentences_train = list(df_train.loc[:, 'word_seg'].apply(sentence2list))
sentences_test = list(df_test.loc[:, 'word_seg'].apply(sentence2list))
sentences = sentences_train + sentences_test
print("准备数据完成! ")

# =======================================================================================================================
# 2 训练
# =======================================================================================================================
print("开始训练................ ")
model = gensim.models.Word2Vec(sentences=sentences, size=vector_size, window=5, min_count=5, workers=8, sg=0, iter=5)
print("训练完成! ")

# =======================================================================================================================
# 3 提取词汇表及vectors,并保存
# =======================================================================================================================
print(" 保存训练结果........... ")
wv = model.wv
vocab_list = wv.index2word
word_idx_dict = {}
for idx, word in enumerate(vocab_list):
    word_idx_dict[word] = idx

vectors_arr = wv.vectors
vectors_arr = np.concatenate((np.zeros(vector_size)[np.newaxis, :], vectors_arr), axis=0)  # 第0位置的vector为'unk'的vector

f_wordidx = open(feature_path + 'word_seg_word_idx_dict.pkl', 'wb')
f_vectors = open(feature_path + 'word_seg_vectors_arr.pkl', 'wb')
pickle.dump(word_idx_dict, f_wordidx)
pickle.dump(vectors_arr, f_vectors)
f_wordidx.close()
f_vectors.close()
print("训练结果已保存到该目录下! ")

end_time = time.time()
print("耗时:{}s ".format(end_time - start_time))

https://blog.csdn.net/class_brick/article/details/78908984
https://blog.csdn.net/mawenqi0729/article/details/80698350

你可能感兴趣的:(NLP,Competition)