当前位置:   article > 正文

Word2vec 源码详解_word2vec源码

word2vec源码

已经看了很久的word2vec,但是发现了很多不同版本的解释,再加上原始论文没有提到太多的细节,所以打算直接看一遍源码,一方面可以加深自己理解;另一方面,以后也可以做适当的改进!

先给出源码中执行的流程图,按照流程图对代码进行详细的解读,流程图如下:

图片

训练部分的流程图如下:

图片

讲解将会按照这个训练过程来!

一、训练参数

注意,这些参数都是「全局参数」,包括以下参数:

  • size: 对应代码中layer1_size, 表示词向量的维度,默认值是100。

  • train: 对应代码中train_file, 表示语料库文件路径。

  • save-vocab: 对应代码中save_vocab_file, 词汇表保存路径。

  • read-vocab: 对应代码中read_vocab_file, 表示已有的词汇表文件路径,直接读取,不用从语料库学习得来。

  • debug: 对应代码中debug_mode, 表示是否选择debug模型,值大于1表示开启,默认是2。开启debug会打印一些信息。

  • binary: 对应代码中全局变量binary,表示文件保存方式,1表示按二进制保存,0表示按文本保存,默认是0.

  • cbow: 对应代码中cbow, 1表示按cbow模型训练, 0表示按skip模式训练,默认是1。

  • alpha: 对应代码中alpha,表示学习率。skip模式下默认为0.025, cbow模式下默认是0.05。

  • output: 对应代码中output_file, 表示词向量保存路径。

  • window: 对应代码中window,表示训练窗口大小。默认是5

  • sample: 对应代码中sample,表示下采样阀值。

  • hs: 对应代码中hs, 表示按huffman softmax模式训练。默认是0, 表示不使用hs。

  • negative: 对应代码中negative, 表示按负采样模式训练, 默认是5。值为0表示不采用负采样训练;如果使用,值一般为3到10。

  • threads: 对应代码中num_threads,训练线程数,一般为12。

  • iter: 对应代码中iter,训练迭代次数,默认是5.

  • min-count: 对应代码中min_count,表示最小出现频率,低于这个频率的词会被移除词汇表。默认值是5

  • classes: 对应代码中classes,表示聚类中心数, 默认是0, 表示不启用聚类。

以上参数都对应了代码中一些全局变量,全局变量具体含义,参考上述参数说明!

二、预生成expTable

word2vec计算过程中用上下文预测中心词或者用中心词预测上下文,都需要进行预测;而word2vec中采用的预测方式是逻辑回归分类,需要用到sigmoid函数,具体函数形式为:

在训练过程中需要用到大量的sigmoid值计算,如果每次都临时去算 exex的值,将会影响性能;当对精度的要求不是很严格的时候,我们可以采用近似的运算。在word2vec中,将区间 「[-MAX_EXP, MAX_EXP]」(代码中MAX_EXP默认值为6)等距划分为 「EXP_TABLE_SIZE」等份,并将每个区间的sigmoid值计算好存入到expTable中。在需要使用时,只需要确定所属的区间,属于哪一份,然后直接去数组中查找。「expTable」初始化代码如下:

  1. expTable = (real *)malloc((EXP_TABLE_SIZE + 1* sizeof(real));       //初始化expTable,近似逼近sigmoid(x)值,x区间为[-MAX_EXP, MAX_EXP],分成EXP_TABLE_SIZE
  2. //将[-MAX_EXP, MAX_EXP]分成EXP_TABLE_SIZE
  3. for (i = 0; i < EXP_TABLE_SIZE; i++) {
  4.     expTable[i] = exp((i / (real)EXP_TABLE_SIZE * 2 - 1* MAX_EXP);   // Precompute the exp() table
  5.     expTable[i] = expTable[i] / (expTable[i] + 1);                     // Precompute f(x) = x / (x + 1)
  6. }

三、构建词汇库

构建词汇库过程中,先判断是否已经有处理好现成的词汇库,有的话直接读取,没有的话再进行训练。

「词汇表训练过程」分为以下几个步骤:「1.读取一个单词」「2.计算单词对应hash值」「3.通过hash值得到单词在词汇表中索引」「4.将单词加入到词汇表」, 「5.对词汇表根据词频进行降序排序」「6.保存训练好的词汇表」。依次介绍以上几个步骤。首先给出词汇表中每个词对应的「结构体」

  1. //词汇中每个word对应的结构体
  2. struct vocab_word {
  3.     long long cn;                     //词频
  4.     int *point;                       //记录huffman树中父节点索引, 自顶向下
  5.     char *word, *code, codelen;       //word表示该单词;code表示Huffman编码表,记录父节点是左节点还是右节点;codelen表示码值表长度
  6. };

「1.读取一个单词对应代码」

  1. // Reads a single word from a file, assuming space + tab + EOL to be word boundaries
  2. //从文件中读取单个单词,假设单词之间通过空格或者tab键或者EOL键进行分割的
  3. void ReadWord(char *word, FILE *fin) {
  4.   int a = 0ch;
  5.   while (!feof(fin)) {
  6.     ch = fgetc(fin);                                             //读一个词
  7.     if (ch == 13continue;                                      //如果是换行符                                  
  8.     if ((ch == ' ') || (ch == '\t') || (ch == '\n')) {           //代表一个单词结束的边界
  9.       if (a > 0) {                                               //如果读到了单词但是遇到了换行符,
  10.         if (ch == '\n') ungetc(ch, fin);                         //退回到流中
  11.         break;
  12.       }
  13.       if (ch == '\n') {                                          //仅仅读到了换行符
  14.         strcpy(word, (char *)"</s>");                            //</s>赋予给word
  15.         return;
  16.       } else continue;
  17.     }
  18.     word[a] = ch;
  19.     a++;
  20.     if (a >= MAX_STRING - 1) a--;   // Truncate too long words   //截断
  21.   }
  22.   word[a] = 0;                                                   //最后一个字符是'\0'
  23. }

「2.计算单词对应的hash值」

  1. // Returns hash value of a word
  2. //返回一个词对应的hash值
  3. int GetWordHash(char *word) {
  4.   unsigned long long a, hash = 0;
  5.   for (a = 0; a < strlen(word); a++) hash = hash * 257 + word[a];
  6.   hash = hash % vocab_hash_size;
  7.   return hash;
  8. }

「3.通过hash值得到word在词汇表中索引」使用到了开放定址法,关于开放地址法,参考这里。

  1. //开放地址发得到词的位置
  2. int SearchVocab(char *word) {
  3.   unsigned int hash = GetWordHash(word);                                     //获得索引
  4.   while (1) {
  5.     if (vocab_hash[hash] == -1return -1;
  6.     if (!strcmp(word, vocab[vocab_hash[hash]].word)) return vocab_hash[hash];
  7.     hash = (hash + 1) % vocab_hash_size;                                     //开放定址法
  8.   }
  9.   return -1;
  10. }

wrod2vec中使用「ReadWordIndex()函数」直接整合了步骤1、步骤2和步骤3,代码如下:

  1. // Reads a word and returns its index in the vocabulary
  2. int ReadWordIndex(FILE *fin) {
  3.   char word[MAX_STRING];                     
  4.   ReadWord(word, fin);                                   //从文件流中读取一个单词
  5.   if (feof(fin)) return -1;
  6.   return SearchVocab(word);                              //返回对应的词汇表中索引
  7. }

「4.将word加入到词汇表」

  1. // Adds a word to the vocabulary
  2. //将word加入到词汇表
  3. int AddWordToVocab(char *word) {
  4.   unsigned int hash, length = strlen(word) + 1;
  5.   if (length > MAX_STRINGlength = MAX_STRING;               //规定每个word不超过MAX_STRING个字符
  6.   vocab[vocab_size].word = (char *)calloc(length, sizeof(char));
  7.   strcpy(vocab[vocab_size].word, word);        //结构体的word词
  8.   vocab[vocab_size].cn = 0;
  9.   vocab_size++;
  10.   // Reallocate memory if needed       //动态扩展内存
  11.   if (vocab_size + 2 >= vocab_max_size) {
  12.     vocab_max_size += 1000;              //词汇量加上1000
  13.     vocab = (struct vocab_word *)realloc(vocab, vocab_max_size * sizeof(struct vocab_word));
  14.   }
  15.   hash = GetWordHash(word);
  16.   while (vocab_hash[hash] != -1) hash = (hash + 1) % vocab_hash_size;   //得到word实际对应的hash值
  17.   vocab_hash[hash] = vocab_size - 1;     //通过hash值获得word在vocab中索引
  18.   return vocab_size - 1;       //返回单词对应索引
  19. }

「5.对词汇表进行排序」  排序需要先尽力一个比较器,这里构造了一个降序排列的比较器,代码如下:

  1. // Used later for sorting by word counts
  2. //构造一个比较器,用来排序,降序
  3. int VocabCompare(const void *a, const void *b) {
  4.     return ((struct vocab_word *)b)->cn - ((struct vocab_word *)a)->cn;
  5. }
  6. // Sorts the vocabulary by frequency using word counts
  7. void SortVocab() {
  8.   int a, size;
  9.   unsigned int hash;
  10.   // Sort the vocabulary and keep </s> at the first position
  11.   qsort(&vocab[1], vocab_size - 1, sizeof(struct vocab_word), VocabCompare);
  12.   for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
  13.   size = vocab_size;
  14.   train_words = 0;
  15.   for (a = 0; a < size; a++) {
  16.     // Words occuring less than min_count times will be discarded from the vocab
  17.     //频率低于一定程度的词会被抛弃掉
  18.     if ((vocab[a].cn < min_count&& (a != 0)) {
  19.       vocab_size--;
  20.       free(vocab[a].word);
  21.     } else {
  22.       // Hash will be re-computed, as after the sorting it is not actual
  23.       //因为排序之后顺序打乱,会重新计算一次hash值
  24.       hash=GetWordHash(vocab[a].word);
  25.       while (vocab_hash[hash] != -1) hash = (hash + 1) % vocab_hash_size;
  26.       vocab_hash[hash] = a;
  27.       train_words += vocab[a].cn;
  28.     }
  29.   }
  30.   //重新规划内存大小
  31.   vocab = (struct vocab_word *)realloc(vocab, (vocab_size + 1* sizeof(struct vocab_word));
  32.   // Allocate memory for the binary tree construction
  33.   for (a = 0; a < vocab_size; a++) {
  34.     vocab[a].code = (char *)calloc(MAX_CODE_LENGTH, sizeof(char));
  35.     vocab[a].point = (int *)calloc(MAX_CODE_LENGTH, sizeof(int));
  36.   }
  37. }

「6.保存训练好的词汇表」

  1. //保存学习到的词汇文件表
  2. void SaveVocab() {
  3.   long long i;
  4.   FILE *fo = fopen(save_vocab_file"wb");
  5.   for (i = 0; i < vocab_size; i++
  6.     fprintf(fo, "%s %lld\n", vocab[i].word, vocab[i].cn);  //保存单词和词频
  7.   fclose(fo);
  8. }

代码中还有一个词汇表裁剪函数, 当词汇表中词汇量大于一定值时,会进行裁剪,先裁掉频率低的词,然后再裁剪掉频率高的词,直到词汇量满足要求,代码如下:

  1. // Reduces the vocabulary by removing infrequent tokens
  2. //对于频率小于min_reduce的词将会被裁剪掉
  3. void ReduceVocab() {
  4.   int a, b = 0;
  5.   unsigned int hash;
  6.   //仅仅一个数组就实现了裁剪过程
  7.   for (a = 0; a < vocab_size; a++if (vocab[a].cn > min_reduce) {
  8.     vocab[b].cn = vocab[a].cn;
  9.     vocab[b].word = vocab[a].word;
  10.     b++;
  11.   } else free(vocab[a].word);
  12.   vocab_size = b;
  13.   //重新设置hash值
  14.   for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
  15.   for (a = 0; a < vocab_size; a++) {
  16.     // Hash will be re-computed, as it is not actual
  17.     hash = GetWordHash(vocab[a].word);
  18.     while (vocab_hash[hash] != -1) hash = (hash + 1) % vocab_hash_size;
  19.     vocab_hash[hash] = a;
  20.   }
  21.   fflush(stdout);
  22.   min_reduce++;     //每次裁剪之后都会提高最低频率数
  23. }

如果已经有训练好的词汇表,可以直接读取,不需要通过语料库进行训练,代码如下:

  1. //从已有的词汇文件中直接读取,不用临时去学习
  2. void ReadVocab() {
  3.   long long a, i = 0;
  4.   char c;
  5.   char word[MAX_STRING];
  6.   FILE *fin = fopen(read_vocab_file"rb");
  7.   if (fin == NULL) {               //判断文件是否存在
  8.     printf("Vocabulary file not found\n");
  9.     exit(1);
  10.   }
  11.   for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;  //vocab_hash值默认为-1
  12.   vocab_size = 0;
  13.   while (1) {                        //不停读取,直到文件末尾
  14.     ReadWord(word, fin);          //从文件流中读取一个单词到word中
  15.     if (feof(fin)) break;
  16.     a = AddWordToVocab(word);            //将单词加入到词汇表            
  17.     fscanf(fin, "%lld%c"&vocab[a].cn, &c);     //读取词频到vocav.cn中,换行符                    
  18.     i++;
  19.   }
  20.   SortVocab();
  21.   if (debug_mode > 0) {
  22.     printf("Vocab size: %lld\n", vocab_size);
  23.     printf("Words in train file: %lld\n", train_words);
  24.   }
  25.   fin = fopen(train_file"rb");
  26.   if (fin == NULL) {
  27.     printf("ERROR: training data file not found!\n");
  28.     exit(1);
  29.   }
  30.   fseek(fin, 0, SEEK_END);     //将读取指针定位到文件尾部
  31.   file_size = ftell(fin);  //得到离头部偏离值,获取文件大小
  32.   fclose(fin);
  33. }

词汇库生成过程由「LearnVocabFromTrainFile()函数」组合以上步骤来完成,代码如下:

  1. //整合上面的文件操作
  2. void LearnVocabFromTrainFile() {
  3.   char word[MAX_STRING];
  4.   FILE *fin;
  5.   long long a, i;
  6.   for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;    //hash值初始为-1
  7.   fin = fopen(train_file"rb");
  8.   if (fin == NULL) {
  9.     printf("ERROR: training data file not found!\n");
  10.     exit(1);
  11.   }
  12.   vocab_size = 0;
  13.   AddWordToVocab((char *)"</s>");                              //'</s>'添加到词汇表,换行符就是用这个表示
  14.   while (1) {
  15.     ReadWord(word, fin);
  16.     if (feof(fin)) break;
  17.     train_words++;
  18.     if ((debug_mode > 1&& (train_words % 100000 == 0)) {
  19.       printf("%lldK%c", train_words / 100013);
  20.       fflush(stdout);
  21.     }
  22.     i = SearchVocab(word);                                     //查找该词的位置
  23.     if (i == -1) {                                             //还未加入到词汇表                   
  24.       a = AddWordToVocab(word);
  25.       vocab[a].cn = 1;
  26.     } else vocab[i].cn++;                                      //已经加入到词汇表
  27.     if (vocab_size > vocab_hash_size * 0.7) ReduceVocab();     //裁剪词操作
  28.   }
  29.   SortVocab();                                                 //排序
  30.   if (debug_mode > 0) {
  31.     printf("Vocab size: %lld\n", vocab_size);
  32.     printf("Words in train file: %lld\n", train_words);
  33.   }
  34.   file_size = ftell(fin);
  35.   fclose(fin);
  36. }

四、初始化网络

初始化网络包括以下几个过程:「1.初始化网络参数」, 「2.构建哈夫曼树」, 「3,初始化负采样概率表」

1.初始化网络参数

网络中的参数主要包括「syn0,syn1和syn1neg」

  1. syn0: 我们需要得到的词向量,源码中使用一个real(float)类型的一维数组表示,注意是一个一维数组!
  2.       容量大小为vocab_size * layer1_size,即 词汇量 * 词向量维度。
  3. syn1: huffman树中,包括叶子节点和非叶子节点。叶子节点是对应的是词汇表中的单词,而非叶子节点是在构造huffman树过程中
  4.       生成的路径节点。syn1表示的就是huffman树中的非叶子节点向量,其维度和词向量维度是一样的,共有(n-1)个非叶子节点,
  5.       n表示词汇表中单词量。注意,syn1也是一个一维real(float)数组,容量为 vocab_size * layer1_size
  6.     
  7. syn1neg: 这是单词的另一个向量表示,之前看斯坦福自然语言处理视频中有提到过每个单词会训练出两个向量,现在看来的确是这    
  8.          样,不过是通过negative方式训练才有。这个向量是用于负采样模式优化时需要的变量。也是一个一维的float数组,
  9.          大小是 vocab_size * layer1_size123456789

初始化代码如下:

  1. //初始化网络
  2. void InitNet() {
  3.   long long a, b;
  4.   unsigned long long next_random = 1;
  5.   //为syn0分配内存,对齐的内存,大小为vocab_size * layer1_size * sizeof(real),也就是每个词汇对应一个layer1_size的向量
  6.   a = posix_memalign((void **)&syn0128, (long long)vocab_size * layer1_size * sizeof(real));
  7.   if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);}
  8.   //如果采用huffman softmax构造,那么需要初始化syn1,大小为vocab_size * layer1_size * sizeof(real),每个词对应一个
  9.   if (hs) {
  10.     a = posix_memalign((void **)&syn1128, (long long)vocab_size * layer1_size * sizeof(real));
  11.     if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);}
  12.     for (a = 0; a < vocab_size; a++for (b = 0; b < layer1_size; b++)
  13.      syn1[a * layer1_size + b] = 0;
  14.   }
  15.   //如果采用负采样进行训练,那么久初始化syn1neg,大小为vocab_size * layer1_size * sizeof(real),每个词对应一个
  16.   if (negative>0) {
  17.     a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real));
  18.     if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);}
  19.     for (a = 0; a < vocab_size; a++for (b = 0; b < layer1_size; b++)
  20.      syn1neg[a * layer1_size + b] = 0;
  21.   }
  22.   //对syn0中每个词对应的词向量进行初始化
  23.   for (a = 0; a < vocab_size; a++for (b = 0; b < layer1_size; b++) {
  24.     next_random = next_random * (unsigned long long)25214903917 + 11;            //生成一个很大的数
  25.     syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5/ layer1_size;//& 0xFFFF表示截断为[065536]
  26.   }
  27.   //构建huffman softmax需要的哈夫曼树
  28.   CreateBinaryTree();
  29. }

syn0的每个值的范围为:[−0.5m,0.5m][−0.5m,0.5m],m表示向量维度;syn1初始化为0;syn1neg也初始化为0.

2.构建哈夫曼树

  1. // Create binary Huffman tree using the word counts
  2. // Frequent words will have short uniqe binary codes
  3. void CreateBinaryTree() {
  4.   long long a, b, i, min1i, min2i, pos1, pos2, point[MAX_CODE_LENGTH];
  5.   char code[MAX_CODE_LENGTH];
  6.   //分配的空间大小为,(vocab_size * 2 + 1* sizeof(long long),因为hufuman树的特性,所以总结点数是2 * n + 1, 其中n是节点数, 此处应该有错误,是2 * n - 1才对
  7.   long long *count = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));       //节点对应频率
  8.   long long *binary = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));      //记录每个节点是左节点还是右节点
  9.   long long *parent_node = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long)); //父节点位置
  10.   for (a = 0; a < vocab_size; a++count[a] = vocab[a].cn;
  11.   //后面的设为无穷
  12.   for (a = vocab_size; a < vocab_size * 2; a++count[a] = 1e15;
  13.   pos1 = vocab_size - 1;
  14.   pos2 = vocab_size;
  15.   // Following algorithm constructs the Huffman tree by adding one node at a time
  16.   //如同天才般的代码,一次遍历就构造好了huffuman树, ##注意,这个a还代表了一种顺序,所有count值由小到大的顺序##
  17.   for (a = 0; a < vocab_size - 1; a++) {
  18.     // First, find two smallest nodes 'min1, min2',注意vocab中的词是已经按照cn排好序的了,是按照降序排列的
  19.     //pos1表示取最原始的词对应的词频,而pos2表示取合并最小值形成的词频
  20.     //连续两次取,两次取的时候代码操作时一模一样的
  21.     if (pos1 >= 0) {
  22.       if (count[pos1< count[pos2]) {
  23.         min1= pos1;
  24.         pos1--;
  25.       } else {
  26.         min1= pos2;
  27.         pos2++;
  28.       }
  29.     } else {
  30.       min1= pos2;
  31.       pos2++;
  32.     }
  33.     if (pos1 >= 0) {
  34.       if (count[pos1< count[pos2]) {
  35.         min2= pos1;
  36.         pos1--;
  37.       } else {
  38.         min2= pos2;
  39.         pos2++;
  40.       }
  41.     } else {
  42.       min2= pos2;
  43.       pos2++;
  44.     }
  45.     count[vocab_size + a] = count[min1i] + count[min2i];
  46.     parent_node[min1i] = vocab_size + a;                   //记录好合并形成的父节点的位置
  47.     parent_node[min2i] = vocab_size + a;
  48.     binary[min2i] = 1;                                     //左为0,右为1
  49.   }
  50.   // Now assign binary code to each vocabulary word
  51.   // 建好了hufuman树之后,就需要分配code了,注意这个hufuman树是用一个数组来存储的,并不是我们常用的指针式链表
  52.   for (a = 0; a < vocab_size; a++) {
  53.     b = a;
  54.     i = 0;
  55.     while (1) {
  56.       code[i] = binary[b];                                 //对于每个节点,自底向上得到code值,通过每个节点的binary来实现
  57.       point[i] = b;                                        //point记录节点到根节点经过的节点的路径
  58.       i++;
  59.       b = parent_node[b];
  60.       if (b == vocab_size * 2 - 2) break;
  61.     }
  62.     vocab[a].codelen = i;                                  //记录词对应的码值的长度
  63.     vocab[a].point[0= vocab_size - 2;                    //最大值作为根节点
  64.     for (b = 0; b < i; b++) {
  65.       vocab[a].code[i - b - 1= code[b];                  //倒序过来,自顶向下
  66.       vocab[a].point[i - b] = point[b] - vocab_size;       //注意这个索引对应的是huffman树中的非叶子节点,对应syn1中的索引, 因为非叶子节点都是在vocab_size * 2 + 1 的后(vocab_size + 1)个
  67.     }
  68.   }
  69.   free(count);
  70.   free(binary);
  71.   free(parent_node);
  72. }

多么简洁而亮眼的代码。「它主要利用了词汇表的有序性,是降序排列。所以刚开始 pos1 = vocab_size - 1 是原始词汇表中词频最小的那个单词。每次合并两个最小值,我们将新生成的节点放到后vocab-size + 1个位置,并且也是有序的往后填充,所以最终代表huffman数的count数组有一个特性,都是中心往两头在递增值。所以,我们每次取最小值,只需要比较两头中哪一头的当前值最小,就能取到两个最小值。」

3.初始化负采样概率表

如果是采用负采样的方法,此时还需要初始化每个词被选中的概率。在所有的词构成的词典中,每一个词出现的频率有高有低,我们希望,「对于那些高频的词,被选中成为负样本的概率要大点,同时,对于那些出现频率比较低的词,我们希望其被选中成为负样本的频率低点」

  1. //生成负采样的概率表
  2. void InitUnigramTable() {
  3.   int a, i;
  4.   double train_words_pow = 0;
  5.   double d1, power = 0.75;
  6.   table = (int *)malloc(table_size * sizeof(int));
  7.   //pow(x, y)计算x的y次方;train_words_pow表示总的词的概率,不是直接用每个词的频率,而是频率的0.75次方幂
  8.   for (a = 0; a < vocab_size; a++) train_words_pow += pow(vocab[a].cn, power);  
  9.   i = 0;
  10.   d1 = pow(vocab[i].cn, power) / train_words_pow;
  11.   //每个词在table中占的小格子数是不一样的,频率高的词,占的格子数显然多
  12.   for (a = 0; a < table_size; a++) {
  13.     table[a] = i;
  14.     if (a / (double)table_size > d1) {
  15.       i++;
  16.       d1 += pow(vocab[i].cn, power) / train_words_pow;
  17.     }
  18.     if (i >= vocab_size) i = vocab_size - 1;
  19.   }
  20. }

五、模型训练

关于word2vec的CBOW和SKIP模型原理,强力推荐大神的博客讲解,虽然有错误细节,但是大体思想都是正确的。首先定义了几个重要的变量,变量解释如下:

  1. last_word: 当前窗口正在训练的词的索引。
  2. sentence_length: 当前训练的句子的长度
  3. sentence_position: 当前中心词在句子中的位置
  4. sen: 数组,存的是句子中每个词在词汇表中的索引
  5. neu1: 是cbow模式下映射层对应的上下文向量表示,为上下文中所有词向量的平均值
  6. neu1e: 因为skip模式下,映射层向量就是输入层向量的复制,所以neu1e仅仅用来记录上下文词对输入层的梯度。123456

每次读取一条句子,记录好句子中每个词在词汇表中对应的索引。如果启用了下采样,则会随机的跳过一些词,会随机的丢弃频繁的单词,同时保持顺序不变。代码如下:

  1. if (sentence_length == 0) {
  2.     while (1) {
  3.       word = ReadWordIndex(fi);                                                   //得到词在词汇表中对应的索引
  4.       if (feof(fi)) break;                                                        //
  5.       if (word == -1continue;
  6.       word_count++;                                                               //句子总的次数
  7.       if (word == 0) break;                                                       //遇到换行符,则直接跳出来,第一个词'</s>'代表换行符
  8.       // The subsampling randomly discards frequent words while keeping the ranking same
  9.       //下采样随机丢弃频繁的单词,同时保持排名相同,随机跳过一些词的训练
  10.       if (sample > 0) {
  11.         real ran = (sqrt(vocab[word].cn / (sample * train_words)) + 1* (sample * train_words) / vocab[word].cn;
  12.         next_random = next_random * (unsigned long long)25214903917 + 11;
  13.         //频率越大的词,对应的ran就越小,越容易被抛弃,被跳过
  14.         if (ran < (next_random & 0xFFFF) / (real)65536continue;
  15.       }
  16.       sen[sentence_length= word;                                                //当前句子包含的词,存的是索引
  17.       sentence_length++;                                                          //句子实际长度,减去跳过的词
  18.       if (sentence_length >= MAX_SENTENCE_LENGTH) break;
  19.     }
  20.     sentence_position = 0;
  21. }123456789101112131415161718192021

然后就开始训练了,先初始化了neu1neu1e的值。并且确定了窗口的起始位置,通过b = next_random % window来确定,「理论上,我们在中心词左右都是取大小为window个上下文词,但是在代码中,并不是保证左右都是window个,而是左边为(window - b)个, 右边为(window + b)个,总数仍然是2 * window个。「训练的时候,有两种训练模型,分别是」CBOW模型和SKIP模型;对于每种模型,又有两种训练模式,分别为huffman softmax模式(hs)和negative模式(负采样)」,下面分别讲解。

1.CBOW模型

在CBOW模型中,总共有三层,分别是「输入层,映射层和输出层」。如下图所示:

图片

hs模式和negative模式中,输入层到映射层的处理是一样的,仅仅是映射层到输出层的处理不一致。输入层到映射层的具体操作是:**将上下文窗口中的每个词向量求和,然后再平均,得到一个和词向量一样维度的向量,假设叫上下文向量,这个向量就是映射层的向量。**代码如下:

  1. if (cbow) {  //train the cbow architecture
  2.   // in -> hidden
  3.   cw = 0;
  4.   //随机取一个词word,然后计算该词上下文词对应的向量的各维度之和
  5.   for (a = b; a < window * 2 + 1 - b; a++if (a != window) {
  6.     c = sentence_position - window + a;
  7.     if (c < 0continue;
  8.     if (c >= sentence_lengthcontinue;
  9.     last_word = sen[c];                                                         //获得senten中第c个词的索引
  10.     if (last_word == -1continue;
  11.     //注意syn0是一维数组,不是二维的,所以通过last_word * layer1_size来定位某个词对应的向量位置, last_word表示上下文中上一个词
  12.     for (c = 0; c < layer1_size; c++) neu1[c] += syn0[c + last_word * layer1_size];  //neu1表示映射层向量,上下文累加平均 
  13.     cw++;
  14.   }
  15.   if (cw) {
  16.   //上下文表示是所有词对应词向量的平均值
  17.     for (c = 0; c < layer1_size; c++) neu1[c] /= cw;
  18.     ......
  19.   }
  20.   ......
  21. }

1.1 hs模式

huffman softmax中,计算上下文向量到中心词的概率,是一连串的二分类问题,因为从根节点到中心词对应的叶子节点,需要多次决定沿左节点还是右节点到叶子节点。详细介绍请参考word2vec数学原理详解。对于中心词w,从根节点到中心词节点的总概率为:

即:

其对数似然函数为:

中 j 表示的是从根节点到中心词w所经过的非叶子节点的索引值(huffman树是用一维数组存的,非叶子节点在数组中对应的索引), 表示的是该非叶子节点对应的 huffman 编码,作为左节点是 0 右节点是 1。 表示映射层的上下文向量,θ 表示非叶子节点向量。在这里 ,θ 都是变量,此时,对二者求偏导数:

则:

再对应代码实现:

  1. if (hs) for (d = 0; d < vocab[word].codelen; d++) {
  2.   f = 0;
  3.   l2 = vocab[word].point[d] * layer1_size;                                     //索引到该词在数组偏移量
  4.   // Propagate hidden -> output, 传播过程
  5.   for (c = 0; c < layer1_size; c++) f += neu1[c] * syn1[c + l2];               //注意syn1也是一维数组,不同词对应的位置需要偏移量l2确定
  6.     if (f <= -MAX_EXP) continue;                                               //当f值不属于[-MAX_EXP, MAX_EXP]
  7.     else if (f >= MAX_EXP) continue;
  8.     else f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];  //查看f属于第几份,((f + MAX_EXP) / (2 * MAX_EXP)) * EXP_TABLE_SIZE
  9.     // 'g' is the gradient multiplied by the learning rate
  10.     g = (1 - vocab[word].code[d] - f) * alpha;                                 //需要推导,得到这个梯度比例
  11.     // Propagate errors output -> hidden
  12.     for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1[c + l2];            //这个部分才是最终梯度值
  13.     // Learn weights hidden -> output
  14.     for (c = 0; c < layer1_size; c++) syn1[c + l2+= g * neu1[c];             //加上梯度值,更新syn1
  15.  }

更新词向量代码如下:

  1. // hidden -> in
  2. //更新输入层的词向量
  3. for (a = b; a < window * 2 + 1 - b; a++if (a != window) {
  4.    c = sentence_position - window + a;
  5.    if (c < 0continue;
  6.    if (c >= sentence_lengthcontinue;
  7.    last_word = sen[c];
  8.    if (last_word == -1continue;
  9.       for (c = 0; c < layer1_size; c++
  10.      syn0[c + last_word * layer1_size+= neu1e[c];
  11. }

1.2 negative模式

负采样过程中,只有一个正样本也就是中心词,其他词都是负样本,将所有概率乘起来,使其最大。对于单个样本 u 有:

则所有样本的概率之和为:

其对数似然函数为:

即为:

其中 u 表示随机选取的词样本, θ 是该词样本对应的向量, 表示映射层的上下文向量, 表示判断词 u 是不是当前窗口中心词 w,1 表示是,0 表示不是。 表示相对于中心词 w 进行的负采样集合。其中 θ 和  是变量,对二者求导:

导数就能够进行梯度上升求最大值。实现代码如下:

  1. // NEGATIVE SAMPLING
  2. if (negative > 0for (d = 0; d < negative + 1; d++) {
  3.   if (d == 0) {                                                               //一个正样本
  4.      target = word;
  5.      label = 1;
  6.    } else {
  7.       next_random = next_random * (unsigned long long)25214903917 + 11;        //随机挑选一个负样本,负样本就是除中心词以外的所有词
  8.       target = table[(next_random >> 16) % table_size];
  9.       if (target == 0) target = next_random % (vocab_size - 1+ 1;            //如果target为0,这个等式保证不为0
  10.       if (target == word) continue;                                            //正样本则跳过
  11.         label = 0;
  12.       }
  13.       l2 = target * layer1_size;                                               //syn1neg是一维数组,某个词需要先计算偏移量
  14.       f = 0;
  15.       for (c = 0; c < layer1_size; c++) f += neu1[c] * syn1neg[c + l2];        //负采样实际会为每个词生成两个向量
  16.         if (f > MAX_EXP) g = (label - 1* alpha;
  17.         else if (f < -MAX_EXP) g = (label - 0* alpha;
  18.         else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;
  19.         for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1neg[c + l2];
  20.         for (c = 0; c < layer1_size; c++) syn1neg[c + l2+= g * neu1[c];
  21.  }

「更新词向量代码如下:」

  1. // hidden -> in
  2. //更新输入层的词向量
  3. for (a = b; a < window * 2 + 1 - b; a++if (a != window) {
  4.    c = sentence_position - window + a;
  5.    if (c < 0continue;
  6.    if (c >= sentence_lengthcontinue;
  7.    last_word = sen[c];
  8.    if (last_word == -1continue;
  9.       for (c = 0; c < layer1_size; c++) syn0[c + last_word * layer1_size+= neu1e[c];
  10. }

2.SKIP模型

skip 模型中,也是三层,输入层、映射层和输出层。结构如下:

图片

skip模型和cbow模型优化类似,主要是输入层到映射层之间不同,「cbow中是上下文词向量平均求和,而skip模型中是直接复制中心词向量。skip模型中,优化过程是逐个计算中心词和上下文词之间的概率,使其最大化,所以和cbow中的优化计算基本类似」,代码如下:

  1. else {  //train skip-gram
  2.   //还是保证一个2 * window大小上下文,但是中心词左右并不一定刚好都是window个,根据b确定
  3.   for (a = b; a < window * 2 + 1 - b; a++if (a != window) {
  4.     c = sentence_position - window + a;                          //c表示上下文的当前遍历位置
  5.     if (c < 0continue;
  6.     if (c >= sentence_lengthcontinue;
  7.     last_word = sen[c];                                          //用来记录上一个词
  8.     if (last_word == -1continue;                               //如果词不在词汇表中,则直接跳过
  9.     l1 = last_word * layer1_size;                                //偏移量,因为syn0是一维数组,每个词对应的向量需要先偏移前面的词对应向量
  10.     for (c = 0; c < layer1_size; c++) neu1e[c] = 0;
  11.     // HIERARCHICAL SOFTMAX 
  12.     //不需要像cbow一样求平均
  13.     if (hs) for (d = 0; d < vocab[word].codelen; d++) {
  14.       f = 0;
  15.       l2 = vocab[word].point[d] * layer1_size;                   //
  16.       // Propagate hidden -> output
  17.       for (c = 0; c < layer1_size; c++) f += syn0[c + l1* syn1[c + l2];
  18.       if (f <= -MAX_EXP) continue;
  19.       else if (f >= MAX_EXP) continue;
  20.       else f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
  21.       // 'g' is the gradient multiplied by the learning rate
  22.       g = (1 - vocab[word].code[d] - f) * alpha;
  23.       // Propagate errors output -> hidden
  24.       for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1[c + l2];
  25.       // Learn weights hidden -> output
  26.       for (c = 0; c < layer1_size; c++) syn1[c + l2+= g * syn0[c + l1];
  27.     }
  28.     // NEGATIVE SAMPLING
  29.     if (negative > 0for (d = 0; d < negative + 1; d++) {
  30.       if (d == 0) {                                                         //正样本
  31.         target = word;
  32.         label = 1;
  33.       } else {                                                              //负样本
  34.         next_random = next_random * (unsigned long long)25214903917 + 11;
  35.         target = table[(next_random >> 16) % table_size];
  36.         if (target == 0) target = next_random % (vocab_size - 1+ 1;
  37.         if (target == word) continue;
  38.         label = 0;
  39.       }
  40.       l2 = target * layer1_size;                                            //偏移量
  41.       f = 0;
  42.       for (c = 0; c < layer1_size; c++) f += syn0[c + l1* syn1neg[c + l2];//
  43.       if (f > MAX_EXP) g = (label - 1* alpha;                             //计算梯度
  44.       else if (f < -MAX_EXP) g = (label - 0* alpha;
  45.       else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;
  46.       for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1neg[c + l2];    //完整梯度
  47.       for (c = 0; c < layer1_size; c++) syn1neg[c + l2+= g * syn0[c + l1];//更新
  48.     }
  49.     // Learn weights input -> hidden
  50.     //更新输入层权重
  51.     for (c = 0; c < layer1_size; c++) syn0[c + l1+= neu1e[c];
  52.   }
  53. }

六、结果处理

可以直接保存结果或者用k-means聚类算法分析结果,代码如下:

  1. //训练模型
  2. void TrainModel() {
  3.   long a, b, c, d;
  4.   FILE *fo;
  5.   pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
  6.   printf("Starting training using file %s\n", train_file);
  7.   starting_alpha = alpha;                                                                         //设置学习率
  8.   if (read_vocab_file[0] != 0) ReadVocab(); else LearnVocabFromTrainFile();                       //获得词汇表,如果已经有直接读,否则学
  9.   if (save_vocab_file[0] != 0) SaveVocab();
  10.   if (output_file[0== 0return;                                                                //必须有输出文件参数
  11.   InitNet();                                                                                      //初始化网络参数
  12.   if (negative > 0) InitUnigramTable();                                                           //如果是使用负采样,那么需要负采样概率表
  13.   start = clock();                                                                                //计时器
  14.   for (a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, TrainModelThread, (void *)a);
  15.   for (a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);
  16.   fo = fopen(output_file"wb");
  17.   if (classes == 0) {                                                                             //classes判断是否使用kmean聚类,为0表示否
  18.     // Save the word vectors
  19.     fprintf(fo, "%lld %lld\n", vocab_size, layer1_size);
  20.     for (a = 0; a < vocab_size; a++) {
  21.       fprintf(fo, "%s ", vocab[a].word);
  22.       if (binaryfor (b = 0; b < layer1_size; b++) fwrite(&syn0[a * layer1_size + b], sizeof(real), 1, fo);
  23.       else for (b = 0; b < layer1_size; b++) fprintf(fo, "%lf ", syn0[a * layer1_size + b]);
  24.       fprintf(fo, "\n");
  25.     }
  26.   } else {
  27.     // Run K-means on the word vectors
  28.     //类别中心数,迭代次数,
  29.     int clcn = classes, iter = 10, closeid;
  30.     int *centcn = (int *)malloc(classes * sizeof(int));                                          //每个中心点拥有的词数量
  31.     int *cl = (int *)calloc(vocab_size, sizeof(int));                                            //每个词所属类别标签
  32.     real closev, x;
  33.     real *cent = (real *)calloc(classes * layer1_size, sizeof(real));                            //聚类中心,注意是用一维数组表示,每个中心需要通过偏移量来定位
  34.     for (a = 0; a < vocab_size; a++) cl[a] = a % clcn;                                           //初始化每个词所属类别
  35.     for (a = 0; a < iter; a++) {                                                                 //开始训练
  36.       for (b = 0; b < clcn * layer1_size; b++) cent[b] = 0;                                      //初始化中心点位置
  37.       for (b = 0; b < clcn; b++) centcn[b] = 1;                                                  //初始化每个中心点拥有的词的数量
  38.       //求每个中心点每个维度值的总和,等于所有属于这个类别的词向量的相应维度相加
  39.       for (c = 0; c < vocab_size; c++) {
  40.         for (d = 0; d < layer1_size; d++) cent[layer1_size * cl[c] + d] += syn0[c * layer1_size + d];
  41.         centcn[cl[c]]++;                                                                         //所包含词的数量+1
  42.       }
  43.       //对于每一个类别,需要更新中心点各维度值,就是总和平均
  44.       for (b = 0; b < clcn; b++) {                                                               
  45.         closev = 0;
  46.         for (c = 0; c < layer1_size; c++) {                                                       //遍历每个维度
  47.           cent[layer1_size * b + c] /= centcn[b];                                                 //每个词每个维度平均
  48.           closev += cent[layer1_size * b + c] * cent[layer1_size * b + c];                        //求每个中心点的模的平方
  49.         }
  50.         closev = sqrt(closev);                                                                    //每个中心点模
  51.         for (c = 0; c < layer1_size; c++) cent[layer1_size * b + c] /= closev;                    //归一化处理
  52.       }
  53.       //更新每个词所属的类别,看离哪个中心点最近就归为相应的类别
  54.       for (c = 0; c < vocab_size; c++) {
  55.         closev = -10;                                                                             //记录离最近中心点距离
  56.         closeid = 0;                                                                              //记录最近的类别id
  57.         for (d = 0; d < clcn; d++) {
  58.           x = 0;
  59.           for (b = 0; b < layer1_size; b++) x += cent[layer1_size * d + b] * syn0[c * layer1_size + b];
  60.           if (x > closev) {
  61.             closev = x;
  62.             closeid = d;
  63.           }
  64.         }
  65.         cl[c] = closeid;
  66.       }
  67.     }
  68.     // Save the K-means classes
  69.     for (a = 0; a < vocab_size; a++) fprintf(fo, "%s %d\n", vocab[a].word, cl[a]);
  70.     free(centcn);
  71.     free(cent);
  72.     free(cl);
  73.   }
  74.   fclose(fo);
  75. }
  76. int ArgPos(char *str, int argc, char **argv) {
  77.   int a;
  78.   for (a = 1; a < argc; a++if (!strcmp(str, argv[a])) {
  79.     if (a == argc - 1) {
  80.       printf("Argument missing for %s\n", str);
  81.       exit(1);
  82.     }
  83.     return a;
  84.   }
  85.   return -1;
  86. }

完整的注释代码:https://github.com/liuwei1206/word2vec/blob/master/word2vec%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90/word2vec.c

参考博客: 

https://blog.csdn.net/itplus/article/details/37969979 

https://blog.csdn.net/google19890102/article/details/51887344

作者:玩人

地址:https://blog.csdn.net/jeryjeryjery/article/details/80245924

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/881847
推荐阅读
相关标签
  

闽ICP备14008679号