当前位置:   article > 正文

基于pytorch实现Word2Vec(skip-gram+Negative Sampling)_skip-gram with negative sampling)模型

skip-gram with negative sampling)模型

目录

word2vec简介

语料处理

数据预处理

训练模型

近似训练法

参数设定

预测及可视化


word2vec简介

2013 年,Google 团队发表了 word2vec 工具。word2vec 工具主要包含两个模型:跳字模型(skip-gram)和连续词模型(continuous bag of words,简称 CBOW),以及两种高效训练的方法:负采样(negative sampling)和层序 softmax(hierarchical softmax)。
类似于f(x)->y,Word2vec 的最终目的,不是要把 f 训练得多么完美,而是只关心模型训练完后的副产物——模型参数(这里特指神经网络的权重),并将这些参数,作为输入 x 的某种向量化的表示,这个向量便叫做——词向量。
word2vec 词向量可以较好地表达不同词之间的相似度和类比关系。


语料处理

步骤:

  1. 使用 re 的 findall 方法以及正则表达式去除标点符号;
  2. 使用 jieba 进行分词;
  3. 使用停用词典剔除无意义的词。

处理前:

处理后:

代码如下:

  1. import re
  2. import jieba
  3. stopwords = {}
  4. fstop = open('stop_words.txt', 'r', encoding='utf-8', errors='ingnore')
  5. for eachWord in fstop:
  6. stopwords[eachWord.strip()] = eachWord.strip() # 创建停用词典
  7. fstop.close()
  8. f1 = open('红楼梦.txt', 'r', encoding='utf-8', errors='ignore')
  9. f2 = open('红楼梦_p.txt', 'w', encoding='utf-8')
  10. line = f1.readline()
  11. while line:
  12. line = line.strip() # 去前后的空格
  13. if line.isspace(): # 跳过空行
  14. line = f1.readline()
  15. line = re.findall('[\u4e00-\u9fa5]+', line) # 去除标点符号
  16. line = "".join(line)
  17. seg_list = jieba.cut(line, cut_all=False) # 结巴分词
  18. outStr = ""
  19. for word in seg_list:
  20. if word not in stopwords: # 去除停用词
  21. outStr += word
  22. outStr += " "
  23. if outStr: # 不为空添加换行符
  24. outStr = outStr.strip() + '\n'
  25. f2.writelines(outStr)
  26. line = f1.readline()
  27. f1.close()
  28. f2.close()

数据预处理

步骤:

  1. 剔除低频词;
  2. 生成 id 到 word、word 到 id 的映射;
  3. 使用 subsampling 处理语料;
  4. 定义获取正、负样本方法;
  5. 估计数据中正采样对数。

测试结果:

 这里 min_count=1 也就是不剔除低频词,窗口大小设定为2,负样本数量 k 设定为3。

代码如下:

  1. import math
  2. import numpy
  3. from collections import deque
  4. from numpy import random
  5. numpy.random.seed(6)
  6. class InputData:
  7. def __init__(self, file_name, min_count):
  8. self.input_file_name = file_name
  9. self.get_words(min_count)
  10. self.word_pair_catch = deque() # deque为队列,用来读取数据
  11. self.init_sample_table() # 采样表
  12. print('Word Count: %d' % len(self.word2id))
  13. print("Sentence_Count:", self.sentence_count)
  14. print("Sentence_Length:", self.sentence_length)
  15. def get_words(self, min_count): # 剔除低频词,生成id到word、word到id的映射
  16. self.input_file = open(self.input_file_name, encoding="utf-8")
  17. self.sentence_length = 0
  18. self.sentence_count = 0
  19. word_frequency = dict()
  20. for line in self.input_file:
  21. self.sentence_count += 1
  22. line = line.strip().split(' ') # strip()去除首尾空格,split(' ')按空格划分词
  23. self.sentence_length += len(line)
  24. for w in line:
  25. try:
  26. word_frequency[w] += 1
  27. except:
  28. word_frequency[w] = 1
  29. self.word2id = dict()
  30. self.id2word = dict()
  31. wid = 0
  32. self.word_frequency = dict()
  33. for w, c in word_fre
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号