#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#define MAX_STRING 100
#define EXP_TABLE_SIZE 1000
#define MAX_EXP 6
#define MAX_SENTENCE_LENGTH 1000
#define MAX_CODE_LENGTH 40
const int vocab_hash_size = 30000000; // Maximum 30 * 0.7 = 21M words in the vocabulary
typedef float real;
struct vocab_word {
long long cn; // 詞頻,來自於vocab file或者從訓練模型中來計算
int *point; // 霍夫曼樹中從根節點到該詞的路徑,存放路徑上每個非葉結點的索引
char *word, *code, codelen; //word:string 字面值 code:Huffman編碼 codelen:huffman編碼的長度
};
char train_file[MAX_STRING], output_file[MAX_STRING];
char save_vocab_file[MAX_STRING], read_vocab_file[MAX_STRING];
//輸入文件中每個基本詞的結構體數組
struct vocab_word *vocab;
int binary = 0, cbow = 1, debug_mode = 2;
int window = 5, min_count = 5;//在由語料庫構建詞典(vocab數組)時,剔除詞頻小於min_count的詞。
//構建詞典後仍需要判斷是否需要對低頻次進行清理,
//如果詞典的大小N>0.7*vocab_hash_size,則從詞典中刪除所有詞頻小於min_reduce的詞。
int num_threads = 12, min_reduce = 1;
//該數組存文件中基本詞的字面的hash碼,和基本詞在vocab_word數組中的位置
//其中基本詞的字面的hash碼作爲該數組的下標
int *vocab_hash;
//vocab_size 不同單詞的個數,也就是詞典的大小
//layer1_size 詞向量的長度
//file_size訓練文件的大小
long long vocab_max_size = 1000, vocab_size = 0, layer1_size = 100;
long long train_words = 0, word_count_actual = 0, iter = 5, file_size = 0, classes = 0;
real alpha = 0.025, starting_alpha, sample = 1e-3;
//syn0 - 存儲詞典中每個詞的詞向量
//syn1 - 存儲Huffman樹各個內節點對應的向量
//syn1neg -負採樣時,存儲每個詞對應的輔助向量
//expTable: sigmoid函數表,提前計算好,提高效率
real *syn0, *syn1, *syn1neg, *expTable;
clock_t start;
int hs = 0, negative = 5;
const int table_size = 1e8;
int *table;
//每個單詞的能量分佈表,table在負採樣中用到
void InitUnigramTable() {
int a, i;
double train_words_pow = 0;
double d1, power = 0.75;
table = (int *)malloc(table_size * sizeof(int));//分配空間
//遍歷詞彙表,統計詞的能量總值train_words_pow
for (a = 0; a < vocab_size; a++)
train_words_pow += pow(vocab[a].cn, power);
i = 0;
//表示已遍歷詞的能量值佔總能量的比
d1 = pow(vocab[i].cn, power) / train_words_pow;
//a - table表的索引
//i - 詞彙表的索引
for (a = 0; a < table_size; a++) {
table[a] = i;//單詞i佔用table的a位置
//table反映的是一個單詞能量的分佈,一個單詞能量越大,所佔用的table的位置越多
if (a / (double)table_size > d1) {
i++; //移到下個詞
d1 += pow(vocab[i].cn, power) / train_words_pow;
}
// put everthing else in the end of the unigram table???
if (i >= vocab_size) i = vocab_size - 1;
}
}
// Reads a single word from a file, assuming space(' ') + tab(\t) + EOL(\n) to be word boundaries
//從fin中讀一個詞到字符串word
void ReadWord(char *word, FILE *fin) {
int a = 0, ch;//a - 用於向word中插入字符的索引;ch - 從fin中讀取的每個字符
while (!feof(fin)) {
ch = fgetc(fin);
if (ch == 13) continue;//回車,開始新的一行,重新開始while循環讀取下一個字符
//當遇到space(' ') + tab(\t) + EOL(\n)時,認爲word結束
if ((ch == ' ') || (ch == '\t') || (ch == '\n')) {
if (a > 0) {//跳出while循環,這裏的特例是‘\n’,我們需要將回退給fin,詞彙表中'\n'用</s>來表示。
if (ch == '\n') ungetc(ch, fin);
break;
}
if (ch == '\n') {//此時word還爲空(a=0),直接將</s>賦給word
strcpy(word, (char *)"</s>");
return;
}
else continue;//此時a=0,且遇到的爲\t or ' ',直接跳過取得下一個字符
}
word[a] = ch;
a++;
if (a >= MAX_STRING - 1) a--; // Truncate too long words
}
word[a] = 0;//字符串末尾以/0作爲結束符
}
//Returns hash value of a word
//返回一個詞的hash值,一個詞跟hash值一一對應(可能衝突)
//hash規則
//hash = hash * 257 + word[a];
//@param word 基本詞字面
//@return 每個基本詞的hash編碼
int GetWordHash(char *word) {
unsigned long long a, hash = 0;
for (a = 0; a < strlen(word); a++) hash = hash * 257 + word[a];//int和char相加,相當於加了char的ansii值
hash = hash % vocab_hash_size;
return hash;
}
// 返回一個詞在詞彙表中的位置,如果不存在則返回-1
// @brief 根據word字面的hash查找其在vocab中的位置,即vocab_hash[hash]的值
// @param word 字面
// @return vocab_hash[hash]的值
int SearchVocab(char *word) {
unsigned int hash = GetWordHash(word);
while (1) {
if (vocab_hash[hash] == -1) return -1;
if (!strcmp(word, vocab[vocab_hash[hash]].word)) return vocab_hash[hash];
hash = (hash + 1) % vocab_hash_size;
}
return -1;
}
// Reads a word and returns its index in the vocabulary
// 從文件流中讀取一個詞,並返回這個詞在詞彙表中的位置
int ReadWordIndex(FILE *fin) {
char word[MAX_STRING];
ReadWord(word, fin);
if (feof(fin)) return -1;//當文件只有一個EOF字符時,當將EOF讀入word後,_IOEOF被設置,達到文件尾。
return SearchVocab(word);
}
// Adds a word to the vocabulary 將一個詞添加到一個詞彙中,返回該詞在詞彙表中的位置
int AddWordToVocab(char *word) {
unsigned int hash, length = strlen(word) + 1;
if (length > MAX_STRING) length = MAX_STRING;
vocab[vocab_size].word = (char *)calloc(length, sizeof(char));//在當前詞彙表末尾添加word
strcpy(vocab[vocab_size].word, word);
vocab[vocab_size].cn = 0;//詞頻記爲0
vocab_size++;
// Reallocate memory if needed
if (vocab_size + 2 >= vocab_max_size) {
vocab_max_size += 1000;
vocab = (struct vocab_word *)realloc(vocab, vocab_max_size * sizeof(struct vocab_word));
}
hash = GetWordHash(word);
//由於hash表初始化爲-1
while (vocab_hash[hash] != -1)
hash = (hash + 1) % vocab_hash_size;//使用開放地址法解決衝突
vocab_hash[hash] = vocab_size - 1;//由詞的hash值找到她所在詞彙表的排序位置
return vocab_size - 1;
}
// Used later for sorting by word counts
//根據詞頻從大到小排序
int VocabCompare(const void *a, const void *b) {
return ((struct vocab_word *)b)->cn - ((struct vocab_word *)a)->cn;
}
// Sorts the vocabulary by frequency using word counts
// 根據詞頻排序
// 通過排序把出現數量少的word排在vocab數組的後面
void SortVocab() {
int a, size;
unsigned int hash;
// Sort the vocabulary and keep </s> at the first position??? 這是因爲:
// 在LearnVocabFromTrainFile將</s>放在vocab的第一個位置
qsort(&vocab[1], vocab_size - 1, sizeof(struct vocab_word), VocabCompare);
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
size = vocab_size;
train_words = 0;
for (a = 0; a < size; a++) {
// Words occuring less than min_count times will be discarded from the vocab
if ((vocab[a].cn < min_count) && (a != 0)) {//不考慮</s>小於min_count的情況
vocab_size--;
free(vocab[a].word);
} else {
// Hash will be re-computed, as after the sorting it is not actual
hash=GetWordHash(vocab[a].word);
while (vocab_hash[hash] != -1) hash = (hash + 1) % vocab_hash_size;//處理衝突
vocab_hash[hash] = a;
train_words += vocab[a].cn;
}
}
vocab = (struct vocab_word *)realloc(vocab, (vocab_size + 1) * sizeof(struct vocab_word));
// Allocate memory for the binary tree construction
for (a = 0; a < vocab_size; a++) {
vocab[a].code = (char *)calloc(MAX_CODE_LENGTH, sizeof(char));
vocab[a].point = (int *)calloc(MAX_CODE_LENGTH, sizeof(int));
}
}
// Reduces the vocabulary by removing infrequent tokens
void ReduceVocab() {
int a, b = 0;
unsigned int hash;
//如果詞典的大小N>0.7*vocab_hash_size,則從詞典中刪除所有詞頻小於min_reduce的詞。
for (a = 0; a < vocab_size; a++)
if (vocab[a].cn > min_reduce) {
vocab[b].cn = vocab[a].cn;
vocab[b].word = vocab[a].word;
b++;
} else free(vocab[a].word);
vocab_size = b;
// reset the hash table
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
for (a = 0; a < vocab_size; a++) {
// Hash will be re-computed, as it is not actual
hash = GetWordHash(vocab[a].word);
while (vocab_hash[hash] != -1) hash = (hash + 1) % vocab_hash_size;
vocab_hash[hash] = a;
}
fflush(stdout);
min_reduce++;
}
// Create binary Huffman tree using the word counts
// Frequent words will have short uniqe binary codes
void CreateBinaryTree() {
long long a, b, i;
long long min1i, min2i;// two smallest nodes
long long pos1, pos2; // current pivots
long long point[MAX_CODE_LENGTH]; //記錄從root到word的路徑
char code[MAX_CODE_LENGTH];
// calloc initializes the memory to zeros
// SHOULD IT BE vocab_size * 2 - 1 - this is because
// it seems that </s> is in part of construction, but vocab_size is
// actually len(vocab)-1, excluding </s>
long long *count = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));
long long *binary = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));
long long *parent_node = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));
// count - word counts of all words
for (a = 0; a < vocab_size; a++) count[a] = vocab[a].cn;
// extend count as twice large
// SO ONLY vocab_size * 2 - 1 elements will BE NEEDED, EVEN FOR A
// COMPLETE TREE
for (a = vocab_size; a < vocab_size * 2; a++) count[a] = 1e15;
// initialize the node positions
pos1 = vocab_size - 1;
pos2 = vocab_size;
// Following algorithm constructs the Huffman tree by adding one node at a time
// the vocab should have been sorted IN DECREASING order
// pos1 and pos2 will be the two current smallest node
// they could be the original elements, they could be composed parent nodes
// the parents node will be constructed and placed along the array
// from [vocab_size to vocab_size * 2 - 1].
// THE GOOD THING IS: the elements on the left of pos1 are all SORTED,
// and the elements on the right of pos2 will also be SORTED.
// THE LAST WORD </s> WILL ALSO BE INCLUDED IN THE TREE
// ONLY NEED TO CONSTRUCT vocab_size - 1 times, that is max number
// of parent nodes for a complete binary tree
for (a = 0; a < vocab_size - 1; a++) {
// First, find two smallest nodes 'min1i, min2i'
// MIN1 goes first
if (pos1 >= 0) {
if (count[pos1] < count[pos2]) {
min1i = pos1;
pos1--;
} else {// move right via pos2
min1i = pos2;
pos2++;
}
} else {// no choice, can move right ONLy now
min1i = pos2;
pos2++;
}
// MIN2 goes next
if (pos1 >= 0) {
if (count[pos1] < count[pos2]) {// move left via pos1
min2i = pos1;
pos1--;
} else {// move right via pos2
min2i = pos2;
pos2++;
}
} else {
min2i = pos2;
pos2++;
}
// parent's count is the sum of children's counts
count[vocab_size + a] = count[min1i] + count[min2i];
// commmon parents
// level 2 parents will be from vocab_size to vocab_size * 2
parent_node[min1i] = vocab_size + a;
parent_node[min2i] = vocab_size + a;
// binary code: min1i 0 min2i 1, for each leaf and internal node
binary[min2i] = 1;
}
// Now assign binary code to each vocabulary word
// update each vocab word and its parent
for (a = 0; a < vocab_size; a++) {
b = a;
i = 0;
while (1) {
code[i] = binary[b];
point[i] = b;
i++;// the depth of traverse from leaf to root
b = parent_node[b];
if (b == vocab_size * 2 - 2) break;
}
vocab[a].codelen = i;// depth
// point - relative index of parent from vocab_size+1
// in reverse order - path from root to the current word (leaf node)
vocab[a].point[0] = vocab_size - 2;
//下面存放每個基本詞的路徑,注意i - b - 1是距離葉子節點最近的父節點的編碼
for (b = 0; b < i; b++) {
vocab[a].code[i - b - 1] = code[b];
vocab[a].point[i - b] = point[b] - vocab_size;// parent node index
}
}
free(count);
free(binary);
free(parent_node);
}
void LearnVocabFromTrainFile() {
char word[MAX_STRING];
FILE *fin;
long long a, i;
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
fin = fopen(train_file, "rb");
if (fin == NULL) {
printf("ERROR: training data file not found!\n");
exit(1);
}
vocab_size = 0;
// THis is consistent with ReadVocab()???
AddWordToVocab((char *)"</s>");//最初將</s>添加到vocab的第一個位置
while (1) {
ReadWord(word, fin);
if (feof(fin)) break;
train_words++;
if ((debug_mode > 1) && (train_words % 100000 == 0)) {
printf("%lldK%c", train_words / 1000, 13);
fflush(stdout);
}
i = SearchVocab(word);//SearchVocab返回word在vocab中的位置,如果不存在返回-1.
// no found in vocab - add to vocab and vocab_hash, set word.cn = 1
// found in vocab - update word.cn += 1
if (i == -1) {
a = AddWordToVocab(word);
vocab[a].cn = 1;
} else vocab[i].cn++;
// vocab is too LARGE for the current vocab_hash_table
if (vocab_size > vocab_hash_size * 0.7) ReduceVocab();
}//end of while:vocab 和 vocab_hash 建立完成
SortVocab();//在該方法中將詞頻<min_count的詞都捨去
if (debug_mode > 0) {
printf("Vocab size: %lld\n", vocab_size);
printf("Words in train file: %lld\n", train_words);
}
file_size = ftell(fin);
fclose(fin);
}
// 輸出單詞和詞頻到文件
void SaveVocab() {
long long i;
FILE *fo = fopen(save_vocab_file, "wb");
// "</s>" will be the first line of the written voc file
for (i = 0; i < vocab_size; i++)
fprintf(fo, "%s %lld\n", vocab[i].word, vocab[i].cn);
fclose(fo);
}
void ReadVocab() {
long long a, i = 0;// i -number of words in vocab
char c;
char word[MAX_STRING];
FILE *fin = fopen(read_vocab_file, "rb");
if (fin == NULL) {
printf("Vocabulary file not found\n");
exit(1);
}
//給Hash表初始化
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
vocab_size = 0;
// read all the words, and their counts from the file
// add them in the vocab, add their index to vocab_hash
while (1) {
ReadWord(word, fin);
if (feof(fin)) break;
a = AddWordToVocab(word);//AddWordToVocab返回word在vocab的位置
fscanf(fin, "%lld%c", &vocab[a].cn, &c);//這裏的c是什麼???
i++;
}
SortVocab();//排序中刪除詞頻小於min_count的詞
if (debug_mode > 0) {
printf("Vocab size: %lld\n", vocab_size);
printf("Words in train file: %lld\n", train_words);
}
fin = fopen(train_file, "rb");//讀train_file主要是爲了確定訓練文件的大小,
//在LearnVocabFromTrainFile中最後也進行了此步驟。
if (fin == NULL) {
printf("ERROR: training data file not found!\n");
exit(1);
}
fseek(fin, 0, SEEK_END);
file_size = ftell(fin);
fclose(fin);
}
//初始化神經網絡結構
void InitNet() {
long long a, b;
// layer1_size will the the dimension of feature space
// syn0 and syn1/syn1neg are of size vocab_size * layer1_size
// syn0-詞向量
unsigned long long next_random = 1;
a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real));
if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);}
// Hierarchical Softmax
if (hs) {
a = posix_memalign((void **)&syn1, 128, (long long)vocab_size * layer1_size * sizeof(real));
if (syn1 == NULL) {printf("Memory allocation failed\n"); exit(1);}
for (a = 0; a < vocab_size; a++)//將Huffman樹的內節點初始化爲0
for (b = 0; b < layer1_size; b++)
syn1[a * layer1_size + b] = 0;
}
// Negative Sampling
if (negative>0) {
a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real));
if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);}
for (a = 0; a < vocab_size; a++)//將每個詞的輔助向量初始化爲0
for (b = 0; b < layer1_size; b++)
syn1neg[a * layer1_size + b] = 0;
}
for (a = 0; a < vocab_size; a++) //初始化詞向量,每一維都是[-0.5,0.5]/layer1_size的隨機數
for (b = 0; b < layer1_size; b++) {
next_random = next_random * (unsigned long long)25214903917 + 11;
syn0[a * layer1_size + b] = (((next_random & 0xFFFF) / (real)65536) - 0.5) / layer1_size;
}
CreateBinaryTree();
}
//這個線程函數執行之前,已經做好了一些工作:根據詞頻排序的詞彙表,每個單詞的huffman編碼
void *TrainModelThread(void *id) {
long long a, b, d, cw, word;//cw-|context(w)|
// word 向sen中添加單詞用,句子完成後表示句子中的當前單詞
// last_word 上一個單詞,輔助掃描窗口
// sentence_length 當前句子的長度(單詞數)
// sentence_position 當前單詞在當前句子中的index
long long last_word, sentence_length = 0, sentence_position = 0;
// word_count 已訓練語料總長度
// last_word_count 保存值,以便在新訓練語料長度超過某個值時輸出信息
long long word_count = 0, last_word_count = 0;
//此處注意sen數組存放的是當前訓練樣本,即基本詞的上下文在vocab中的位置
long long sen[MAX_SENTENCE_LENGTH + 1];
// l1 ns中表示word在concatenated word vectors中的起始位置,之後layer1_size是對應的word vector,因爲把矩陣拉成長向量了
// l2 cbow或ns中權重向量的起始位置,之後layer1_size是對應的syn1或syn1neg,因爲把矩陣拉成長向量了
// c 循環中的計數作用
// target ns中當前的sample
// label ns中當前sample的label
long long l1, l2, c, target, label, local_iter = iter;
// id 線程創建的時候傳入,輔助隨機數生成
unsigned long long next_random = (long long)id;
// f e^x / (1/e^x),fs中指當前編碼爲是0(父親的左子節點爲0,右爲1)的概率,ns中指label是1的概率
// g 誤差(f與真實值的偏離)與學習速率的乘積
real f, g;
// 當前時間,和start比較計算算法效率
clock_t now;
// neu1對應於Xw,即如果是CBOW,neu1是context(w)各vector的累加和;如果是skip-gram的話,neu1是w對應的vector
real *neu1 = (real *)calloc(layer1_size, sizeof(real));
// 誤差累計項
real *neu1e = (real *)calloc(layer1_size, sizeof(real));
FILE *fi = fopen(train_file, "rb");
//每個線程對應一段文本。根據線程id找到自己負責的文本的初始位置
fseek(fi, file_size / (long long)num_threads * (long long)id, SEEK_SET);
while (1) {
if (word_count - last_word_count > 10000) {
word_count_actual += word_count - last_word_count;
last_word_count = word_count;
if ((debug_mode > 1)) {
now=clock();
//打印訓練進度情況 回車的ascii碼是13 alpha是訓練速率
printf("%cAlpha: %f Progress: %.2f%% Words/thread/sec: %.2fk ", 13, alpha,
word_count_actual / (real)(iter * train_words + 1) * 100,
word_count_actual / ((real)(now - start + 1) / (real)CLOCKS_PER_SEC * 1000));
fflush(stdout);
}
//自適應調整學習率
alpha = starting_alpha * (1 - word_count_actual / (real)(iter * train_words + 1));
if (alpha < starting_alpha * 0.0001) alpha = starting_alpha * 0.0001;
}
if (sentence_length == 0) {
while (1) {
//從文件流中讀取一個詞,並返回這個詞在詞彙表中的位置
word = ReadWordIndex(fi);
if (feof(fi)) break;
if (word == -1) continue;
word_count++;
if (word == 0) break;//是個'\n',句子結束
//對高頻詞進行亞採樣,亞採樣可以提高2~10倍的訓練速度,並且可以使低頻詞向量更精確,具體來說:
//以1-ran的概率捨棄高頻詞
if (sample > 0) {
real ran = (sqrt(vocab[word].cn / (sample * train_words)) + 1) * (sample * train_words) / vocab[word].cn;
next_random = next_random * (unsigned long long)25214903917 + 11;
if (ran < (next_random & 0xFFFF) / (real)65536) continue;
}
//sen存放的爲該詞在詞典中的索引,並且sen[]中詞的順序與文本中詞的順序一致。
sen[sentence_length] = word;
sentence_length++;
//1000個單詞視作一個句子
if (sentence_length >= MAX_SENTENCE_LENGTH) break;
}
//充當最外面while循環的指針,依次掃描句中每個word
sentence_position = 0;
}
//如果當前線程已處理的單詞超過了閾值,要麼進行新一輪的迭代,要麼在迭代iter次後退出
if (feof(fi) || (word_count > train_words / num_threads)) {
word_count_actual += word_count - last_word_count;
local_iter--;
if (local_iter == 0) break;
word_count = 0;
last_word_count = 0;
sentence_length = 0;
fseek(fi, file_size / (long long)num_threads * (long long)id, SEEK_SET);
continue;
}
//取出當前單詞
word = sen[sentence_position];
if (word == -1) continue;
for (c = 0; c < layer1_size; c++) neu1[c] = 0;
for (c = 0; c < layer1_size; c++) neu1e[c] = 0;
next_random = next_random * (unsigned long long)25214903917 + 11;
//[0,window-1]的隨機數,用於確定|context(w)|
b = next_random % window;
//~~~~~~~~~~~~~~~~~~~~~train the cbow architecture (context(w),w)~~~~~~~~~~~~~~~~~~~~
if (cbow) {
cw = 0;//|context(w)|,初始化爲0
for (a = b; a < window * 2 + 1 - b; a++) //求向量和Xw
if (a != window) {//當a=window時,c=sentence_position,當前詞上下文中不應該包括
c = sentence_position - window + a;
if (c < 0) continue;
if (c >= sentence_length) continue;
last_word = sen[c];//c處存放的單詞在詞典中的索引
if (last_word == -1) continue;
for (c = 0; c < layer1_size; c++) //step1:傳說中的向量和,layer1_size是詞向量的維度,默認是100
neu1[c] += syn0[c + last_word * layer1_size];
cw++;
}
if (cw) {
for (c = 0; c < layer1_size; c++) //求平均向量和
neu1[c] /= cw;
//層次softmax優化
if (hs)
for (d = 0; d < vocab[word].codelen; d++) {//遍歷Huffman樹上的內節點
f = 0;
//l2爲內節點在syn1的起始位置
l2 = vocab[word].point[d] * layer1_size;
for (c = 0; c < layer1_size; c++)//step2:f爲Xw和Theta的內積
f += neu1[c] * syn1[c + l2];
if (f <= -MAX_EXP) continue;//sigmoid(f)=0
else if (f >= MAX_EXP) continue;//sigmoid(f)=1
//step3:對f進行sigmoid變換
else f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
g = (1 - vocab[word].code[d] - f) * alpha;// step4:alpha*[1-dj-sigmoid(f)]
for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1[c + l2];//step5:計算累計誤差,用於更新詞向量
for (c = 0; c < layer1_size; c++) syn1[c + l2] += g * neu1[c];//update1:更新內節點Theta
}
// 負採樣優化
if (negative > 0)
for (d = 0; d < negative + 1; d++) {
if (d == 0) {
target = word;//目標單詞
label = 1;//正樣本
} else {
next_random = next_random * (unsigned long long)25214903917 + 11;
target = table[(next_random >> 16) % table_size];
if (target == 0) target = next_random % (vocab_size - 1) + 1;//負採樣抽到回車,再重新採樣
if (target == word) continue;
label = 0;//負樣本
}
l2 = target * layer1_size;//l2-target對應輔助變量Theta(syn1neg)的起始位置
f = 0;
for (c = 0; c < layer1_size; c++) f += neu1[c] * syn1neg[c + l2];//在負採樣優化中,每個word都對應一個輔助向量Theta(syn1neg)
if (f > MAX_EXP) g = (label - 1) * alpha;//sigmoid(f)=1
else if (f < -MAX_EXP) g = (label - 0) * alpha;//sigmoid(f)=0
//g=alpha*[label-sigmoid(Xw*Theta)]
else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;
//neu1e-累積誤差,直到一輪抽樣完了後才能更新輸入層的詞向量。
for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1neg[c + l2];//隱藏層的誤差
for (c = 0; c < layer1_size; c++) syn1neg[c + l2] += g * neu1[c];//update1:更新輔助變量Theta(syn1neg)
}
//對context(w)中的每個詞向量進行更新
for (a = b; a < window * 2 + 1 - b; a++)
if (a != window) {//cbow模型 更新的不是中間詞語的向量,而是周圍幾個詞語的向量。
c = sentence_position - window + a;
if (c < 0) continue;
if (c >= sentence_length) continue;
last_word = sen[c];
if (last_word == -1) continue;
for (c = 0; c < layer1_size; c++)
syn0[c + last_word * layer1_size] += neu1e[c];//update2:更新context(w)中的每一個詞向量
}
}
}//end of cbow
// ~~~~~~~~~~~~~~~~~~~~train the skip-gram architecture (w,context(w))~~~~~~~~~~~~~~~~~~~
else {
for (a = b; a < window * 2 + 1 - b; a++)
if (a != window) {
c = sentence_position - window + a;
if (c < 0) continue;
if (c >= sentence_length) continue;
last_word = sen[c];//窗口中待處理的詞(公式推導中的u)
if (last_word == -1) continue;
l1 = last_word * layer1_size;//l1-u在syn0層的起始位置
for (c = 0; c < layer1_size; c++) neu1e[c] = 0;//累計誤差初始化
// 層次Softmax,這裏用到了模型對稱:p(u|w)=p(w|u),其中u是context(w)中每個詞
if (hs)
for (d = 0; d < vocab[word].codelen; d++) {//遍歷word對應的內節點
f = 0;
l2 = vocab[word].point[d] * layer1_size;//l2-內節點在syn1層的位置
for (c = 0; c < layer1_size; c++) f += syn0[c + l1] * syn1[c + l2];//V_u和Theta_w的內積
if (f <= -MAX_EXP) continue;
else if (f >= MAX_EXP) continue;
else f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];//sigmoid(f)
//g=alpha*[1-d_j-sigmoid(V_u和Theta_w的內積)]
g = (1 - vocab[word].code[d] - f) * alpha;
for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1[c + l2];//計算累計誤差,用於更新V_u
// Learn weights hidden -> output
for (c = 0; c < layer1_size; c++) syn1[c + l2] += g * syn0[c + l1];//update1:更新內節點
}
// 負採樣 (w,context(w))
if (negative > 0)
for (d = 0; d < negative + 1; d++) {
if (d == 0) {
target = word;
label = 1;
} else {
next_random = next_random * (unsigned long long)25214903917 + 11;
target = table[(next_random >> 16) % table_size];
if (target == 0) target = next_random % (vocab_size - 1) + 1;
if (target == word) continue;
label = 0;
}
l2 = target * layer1_size;//l2-負採樣的詞在syn1neg的初始位置 l1-context(w)中的每個詞在syn0層的初始位置
f = 0;
for (c = 0; c < layer1_size; c++) f += syn0[c + l1] * syn1neg[c + l2];//context(w)中的詞與負採樣的詞做內積
if (f > MAX_EXP) g = (label - 1) * alpha;
else if (f < -MAX_EXP) g = (label - 0) * alpha;
//g=alpga*[label-sigmoid(context(w)中的詞與負採樣的詞做內積)]
else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;
for (c = 0; c < layer1_size; c++) neu1e[c] += g * syn1neg[c + l2];//計算累計誤差
for (c = 0; c < layer1_size; c++) syn1neg[c + l2] += g * syn0[c + l1];//update1:更新負採樣的詞對應的輔助向量
}
for (c = 0; c < layer1_size; c++) syn0[c + l1] += neu1e[c];//update2:更新context(w)中的每個詞向量
}
}//end of skip gram
sentence_position++;
if (sentence_position >= sentence_length) {
sentence_length = 0;
continue;
}
}//end of while(1)
fclose(fi);
free(neu1);
free(neu1e);
pthread_exit(NULL);
}
void TrainModel() {
long a, b, c, d;
FILE *fo;
// 創建多線程
pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
printf("Starting training using file %s\n", train_file);
starting_alpha = alpha;
//先從詞彙表中加載,否則從訓練文件中加載
if (read_vocab_file[0] != 0) ReadVocab(); else LearnVocabFromTrainFile();
//輸出詞彙表文件,詞+詞頻
if (save_vocab_file[0] != 0) SaveVocab(); //根據需要來判斷是否進行vocab(排序好的)的保存
if (output_file[0] == 0) return;
InitNet();
if (negative > 0) InitUnigramTable();
start = clock();
for (a = 0; a < num_threads; a++)
pthread_create(&pt[a], NULL, TrainModelThread, (void *)a);
for (a = 0; a < num_threads; a++)
pthread_join(pt[a], NULL);
fo = fopen(output_file, "wb");
if (classes == 0) {
// Save the word vectors
fprintf(fo, "%lld %lld\n", vocab_size, layer1_size);
for (a = 0; a < vocab_size; a++) {
fprintf(fo, "%s ", vocab[a].word);
if (binary)
for (b = 0; b < layer1_size; b++)
fwrite(&syn0[a * layer1_size + b], sizeof(real), 1, fo);
else
for (b = 0; b < layer1_size; b++)
fprintf(fo, "%lf ", syn0[a * layer1_size + b]);
fprintf(fo, "\n");
}
} else {
// Run K-means on the word vectors
int clcn = classes;//cluser count
int iter = 10;
int closeid;
//屬於每個中心的單詞個數
int *centcn = (int *)malloc(classes * sizeof(int));
// 存放每個單詞指派的中心id
int *cl = (int *)calloc(vocab_size, sizeof(int));
real closev, x;
//存放每個中心的向量表示
real *cent = (real *)calloc(classes * layer1_size, sizeof(real));
//隨機指派每個單詞的中心
for (a = 0; a < vocab_size; a++)
cl[a] = a % clcn;
for (a = 0; a < iter; a++) { //一共迭代iter輪
//中心向量座標初始化爲0
for (b = 0; b < clcn * layer1_size; b++)
cent[b] = 0;
//屬於每個中心的單詞數爲1
for (b = 0; b < clcn; b++)
centcn[b] = 1;
//將屬於每個中心的點的每個座標相加
for (c = 0; c < vocab_size; c++) {
for (d = 0; d < layer1_size; d++)
cent[layer1_size * cl[c] + d] += syn0[c * layer1_size + d];
centcn[cl[c]]++;// 分別計算屬於每個中心的點個數
}
for (b = 0; b < clcn; b++) {//更新每個中心的向量表示??
closev = 0;
for (c = 0; c < layer1_size; c++) {
cent[layer1_size * b + c] /= centcn[b];//每個中心的平均座標
closev += cent[layer1_size * b + c] * cent[layer1_size * b + c];//每個中心的每維的平方和
}
closev = sqrt(closev);//每個中心的向量模
for (c = 0; c < layer1_size; c++) //將中心向量歸一化
cent[layer1_size * b + c] /= closev;
}
for (c = 0; c < vocab_size; c++) {//更新每個樣本的中心,循環迭代每個詞
closev = -10;
closeid = 0;
for (d = 0; d < clcn; d++) {//判斷該詞該歸爲哪一類
x = 0;
for (b = 0; b < layer1_size; b++) //歸一化聚類中心和詞向量做內積
x += cent[layer1_size * d + b] * syn0[c * layer1_size + b];
if (x > closev) {// 詞向量也歸一化的,那麼兩個點的距離最小等價於內積最大,可是
// 詞向量並沒有歸一化啊???
closev = x;
closeid = d;
}
}
cl[c] = closeid;
}
}
// Save the K-means classes
for (a = 0; a < vocab_size; a++) fprintf(fo, "%s %d\n", vocab[a].word, cl[a]);
free(centcn);
free(cent);
free(cl);
}
fclose(fo);
}
int ArgPos(char *str, int argc, char **argv) {
int a;
for (a = 1; a < argc; a++)
if (!strcmp(str, argv[a])) {
if (a == argc - 1) {
printf("Argument missing for %s\n", str);
exit(1);
}
return a;
}
return -1;
}
int main(int argc, char **argv) {
int i;
if (argc == 1) {
printf("WORD VECTOR estimation toolkit v 0.1c\n\n");
printf("Options:\n");
printf("Parameters for training:\n");
printf("\t-train <file>\n");
printf("\t\tUse text data from <file> to train the model\n");
printf("\t-output <file>\n");
printf("\t\tUse <file> to save the resulting word vectors / word clusters\n");
printf("\t-size <int>\n");
printf("\t\tSet size of word vectors; default is 100\n");
printf("\t-window <int>\n");
printf("\t\tSet max skip length between words; default is 5\n");
printf("\t-sample <float>\n");
printf("\t\tSet threshold for occurrence of words. Those that appear with higher frequency in the training data\n");
printf("\t\twill be randomly down-sampled; default is 1e-3, useful range is (0, 1e-5)\n");
printf("\t-hs <int>\n");
printf("\t\tUse Hierarchical Softmax; default is 0 (not used)\n");
printf("\t-negative <int>\n");
printf("\t\tNumber of negative examples; default is 5, common values are 3 - 10 (0 = not used)\n");
printf("\t-threads <int>\n");
printf("\t\tUse <int> threads (default 12)\n");
printf("\t-iter <int>\n");
printf("\t\tRun more training iterations (default 5)\n");
printf("\t-min-count <int>\n");
printf("\t\tThis will discard words that appear less than <int> times; default is 5\n");
printf("\t-alpha <float>\n");
printf("\t\tSet the starting learning rate; default is 0.025 for skip-gram and 0.05 for CBOW\n");
printf("\t-classes <int>\n");
printf("\t\tOutput word classes rather than word vectors; default number of classes is 0 (vectors are written)\n");
printf("\t-debug <int>\n");
printf("\t\tSet the debug mode (default = 2 = more info during training)\n");
printf("\t-binary <int>\n");
printf("\t\tSave the resulting vectors in binary moded; default is 0 (off)\n");
printf("\t-save-vocab <file>\n");
printf("\t\tThe vocabulary will be saved to <file>\n");
printf("\t-read-vocab <file>\n");
printf("\t\tThe vocabulary will be read from <file>, not constructed from the training data\n");
printf("\t-cbow <int>\n");
printf("\t\tUse the continuous bag of words model; default is 1 (use 0 for skip-gram model)\n");
printf("\nExamples:\n");
printf("./word2vec -train data.txt -output vec.txt -size 200 -window 5 -sample 1e-4 -negative 5 -hs 0 -binary 0 -cbow 1 -iter 3\n\n");
return 0;
}
output_file[0] = 0;
save_vocab_file[0] = 0;
read_vocab_file[0] = 0;
if ((i = ArgPos((char *)"-size", argc, argv)) > 0) layer1_size = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-train", argc, argv)) > 0) strcpy(train_file, argv[i + 1]);
if ((i = ArgPos((char *)"-save-vocab", argc, argv)) > 0) strcpy(save_vocab_file, argv[i + 1]);
if ((i = ArgPos((char *)"-read-vocab", argc, argv)) > 0) strcpy(read_vocab_file, argv[i + 1]);
if ((i = ArgPos((char *)"-debug", argc, argv)) > 0) debug_mode = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-binary", argc, argv)) > 0) binary = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-cbow", argc, argv)) > 0) cbow = atoi(argv[i + 1]);
if (cbow) alpha = 0.05;
if ((i = ArgPos((char *)"-alpha", argc, argv)) > 0) alpha = atof(argv[i + 1]);
if ((i = ArgPos((char *)"-output", argc, argv)) > 0) strcpy(output_file, argv[i + 1]);
if ((i = ArgPos((char *)"-window", argc, argv)) > 0) window = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-sample", argc, argv)) > 0) sample = atof(argv[i + 1]);
if ((i = ArgPos((char *)"-hs", argc, argv)) > 0) hs = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-negative", argc, argv)) > 0) negative = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-threads", argc, argv)) > 0) num_threads = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-iter", argc, argv)) > 0) iter = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-min-count", argc, argv)) > 0) min_count = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-classes", argc, argv)) > 0) classes = atoi(argv[i + 1]);
vocab = (struct vocab_word *)calloc(vocab_max_size, sizeof(struct vocab_word));
vocab_hash = (int *)calloc(vocab_hash_size, sizeof(int));
expTable = (real *)malloc((EXP_TABLE_SIZE + 1) * sizeof(real));
for (i = 0; i < EXP_TABLE_SIZE; i++) {
expTable[i] = exp((i / (real)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // e()
expTable[i] = expTable[i] / (expTable[i] + 1); // sigmoid()
}
TrainModel();
return 0;
}
word2vec.c源碼分析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.