-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathword2vec.c
1602 lines (1383 loc) · 60.7 KB
/
word2vec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#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
/*
* The size of the hash table for the vocabulary.
* The vocabulary won't be allowed to grow beyond 70% of this number.
* For instance, if the hash table has 30M entries, then the maximum
* vocab size is 21M. This is to minimize the occurrence (and performance
* impact) of hash collisions.
*/
const int vocab_hash_size = 30000000; // Maximum 30 * 0.7 = 21M words in the vocabulary
typedef float real; // Precision of float numbers
/**
* ======== vocab_word ========
* Properties:
* cn - The word frequency (number of times it appears).
* word - The actual string word.
*/
struct vocab_word {
long long cn;
int *point;
char *word, *code, codelen;
};
/*
* ======== Global Variables ========
*
*/
char train_file[MAX_STRING], output_file[MAX_STRING];
char save_vocab_file[MAX_STRING], read_vocab_file[MAX_STRING];
/*
* ======== vocab ========
* This array will hold all of the words in the vocabulary.
* This is internal state.
*/
struct vocab_word *vocab;
int binary = 0, cbow = 1, debug_mode = 2, window = 5, min_count = 5, num_threads = 12, min_reduce = 1;
/*
* ======== vocab_hash ========
* This array is the hash table for the vocabulary. Word strings are hashed
* to a hash code (an integer), then the hash code is used as the index into
* 'vocab_hash', to retrieve the index of the word within the 'vocab' array.
*/
int *vocab_hash;
/*
* ======== vocab_max_size ========
* This is not a limit on the number of words in the vocabulary, but rather
* a chunk size for allocating the vocabulary table. The vocabulary table will
* be expanded as necessary, and is allocated, e.g., 1,000 words at a time.
*
* ======== vocab_size ========
* Stores the number of unique words in the vocabulary.
* This is not a parameter, but rather internal state.
*
* ======== layer1_size ========
* This is the number of features in the word vectors.
* It is the number of neurons in the hidden layer of the model.
*/
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;
/*
* ======== alpha ========
* TODO - This is a learning rate parameter.
*
* ======== starting_alpha ========
*
* ======== sample ========
* This parameter controls the subsampling of frequent words.
* Smaller values of 'sample' mean words are less likely to be kept.
* Set 'sample' to 0 to disable subsampling.
* See the comments in the subsampling section for more details.
*/
real alpha = 0.025, starting_alpha, sample = 1e-3;
/*
* IMPORTANT - Note that the weight matrices are stored as 1D arrays, not
* 2D matrices, so to access row 'i' of syn0, the index is (i * layer1_size).
*
* ======== syn0 ========
* This is the hidden layer weights (which is also the word vectors!)
*
* ======== syn1 ========
* This is the output layer weights *if using heirarchical softmax*
*
* ======== syn1neg ========
* This is the output layer weights *if using negative sampling*
*
* ======== expTable ========
* Stores precalcultaed activations for the output layer.
*/
real *syn0, *syn1, *syn1neg, *expTable;
clock_t start;
int hs = 0, negative = 5;
const int table_size = 1e8;
int *table;
/**
* ======== InitUnigramTable ========
* This table is used to implement negative sampling.
* Each word is given a weight equal to it's frequency (word count) raised to
* the 3/4 power. The probability for a selecting a word is just its weight
* divided by the sum of weights for all words.
*
* Note that the vocabulary has been sorted by word count, descending, such
* that we will go through the vocabulary from most frequent to least.
*/
void InitUnigramTable() {
int a, i;
double train_words_pow = 0;
double d1, power = 0.75;
// Allocate the table. It's bigger than the vocabulary, because words will
// appear in it multiple times based on their frequency.
// Every vocab word appears at least once in the table.
// The size of the table relative to the size of the vocab dictates the
// resolution of the sampling. A larger unigram table means the negative
// samples will be selected with a probability that more closely matches the
// probability calculated by the equation.
table = (int *)malloc(table_size * sizeof(int));
// Calculate the denominator, which is the sum of weights for all words.
for (a = 0; a < vocab_size; a++) train_words_pow += pow(vocab[a].cn, power);
// 'i' is the vocabulary index of the current word, whereas 'a' will be
// the index into the unigram table.
i = 0;
// Calculate the probability that we choose word 'i'. This is a fraction
// betwee 0 and 1.
d1 = pow(vocab[i].cn, power) / train_words_pow;
// Loop over all positions in the table.
for (a = 0; a < table_size; a++) {
// Store word 'i' in this position. Word 'i' will appear multiple times
// in the table, based on its frequency in the training data.
table[a] = i;
// If the fraction of the table we have filled is greater than the
// probability of choosing this word, then move to the next word.
if (a / (double)table_size > d1) {
// Move to the next word.
i++;
// Calculate the probability for the new word, and accumulate it with
// the probabilities of all previous words, so that we can compare d1 to
// the percentage of the table that we have filled.
d1 += pow(vocab[i].cn, power) / train_words_pow;
}
// Don't go past the end of the vocab.
// The total weights for all words should sum up to 1, so there shouldn't
// be any extra space at the end of the table. Maybe it's possible to be
// off by 1, though? Or maybe this is just precautionary.
if (i >= vocab_size) i = vocab_size - 1;
}
}
/**
* ======== ReadWord ========
* Reads a single word from a file, assuming space + tab + EOL to be word
* boundaries.
*
* Parameters:
* word - A char array allocated to hold the maximum length string.
* fin - The training file.
*/
void ReadWord(char *word, FILE *fin) {
// 'a' will be the index into 'word'.
int a = 0, ch;
// Read until the end of the word or the end of the file.
while (!feof(fin)) {
// Get the next character.
ch = fgetc(fin);
// ASCII Character 13 is a carriage return 'CR' whereas character 10 is
// newline or line feed 'LF'.
if (ch == 13) continue;
// Check for word boundaries...
if ((ch == ' ') || (ch == '\t') || (ch == '\n')) {
// If the word has at least one character, we're done.
if (a > 0) {
// Put the newline back before returning so that we find it next time.
if (ch == '\n') ungetc(ch, fin);
break;
}
// If the word is empty and the character is newline, treat this as the
// end of a "sentence" and mark it with the token </s>.
if (ch == '\n') {
strcpy(word, (char *)"</s>");
return;
// If the word is empty and the character is tab or space, just continue
// on to the next character.
} else continue;
}
// If the character wasn't space, tab, CR, or newline, add it to the word.
word[a] = ch;
a++;
// If the word's too long, truncate it, but keep going till we find the end
// of it.
if (a >= MAX_STRING - 1) a--;
}
// Terminate the string with null.
word[a] = 0;
}
/**
* ======== GetWordHash ========
* Returns hash value of a word. The hash is an integer between 0 and
* vocab_hash_size (default is 30E6).
*
* For example, the word 'hat':
* hash = ((((h * 257) + a) * 257) + t) % 30E6
*/
int GetWordHash(char *word) {
unsigned long long a, hash = 0;
for (a = 0; a < strlen(word); a++) hash = hash * 257 + word[a];
hash = hash % vocab_hash_size;
return hash;
}
/**
* ======== SearchVocab ========
* Lookup the index in the 'vocab' table of the given 'word'.
* Returns -1 if the word is not found.
* This function uses a hash table for fast lookup.
*/
int SearchVocab(char *word) {
// Compute the hash value for 'word'.
unsigned int hash = GetWordHash(word);
// Lookup the index in the hash table, handling collisions as needed.
// See 'AddWordToVocab' to see how collisions are handled.
while (1) {
// If the word isn't in the hash table, it's not in the vocab.
if (vocab_hash[hash] == -1) return -1;
// If the input word matches the word stored at the index, we're good,
// return the index.
if (!strcmp(word, vocab[vocab_hash[hash]].word)) return vocab_hash[hash];
// Otherwise, we need to scan through the hash table until we find it.
hash = (hash + 1) % vocab_hash_size;
}
// This will never be reached.
return -1;
}
/**
* ======== ReadWordIndex ========
* Reads the next word from the training file, and returns its index into the
* 'vocab' table.
*/
int ReadWordIndex(FILE *fin) {
char word[MAX_STRING];
ReadWord(word, fin);
if (feof(fin)) return -1;
return SearchVocab(word);
}
/**
* ======== AddWordToVocab ========
* Adds a new word to the vocabulary (one that hasn't been seen yet).
*/
int AddWordToVocab(char *word) {
// Measure word length.
unsigned int hash, length = strlen(word) + 1;
// Limit string length (default limit is 100 characters).
if (length > MAX_STRING) length = MAX_STRING;
// Allocate and store the word string.
vocab[vocab_size].word = (char *)calloc(length, sizeof(char));
strcpy(vocab[vocab_size].word, word);
// Initialize the word frequency to 0.
vocab[vocab_size].cn = 0;
// Increment the vocabulary size.
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));
}
// Add the word to the 'vocab_hash' table so that we can map quickly from the
// string to its vocab_word structure.
// Hash the word to an integer between 0 and 30E6.
hash = GetWordHash(word);
// If the spot is already taken in the hash table, find the next empty spot.
while (vocab_hash[hash] != -1)
hash = (hash + 1) % vocab_hash_size;
// Map the hash code to the index of the word in the 'vocab' array.
vocab_hash[hash] = vocab_size - 1;
// Return the index of the word in the 'vocab' array.
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;
}
/**
* ======== SortVocab ========
* Sorts the vocabulary by frequency using word counts, and removes words that
* occur fewer than 'min_count' times in the training text.
*
* Removing words from the vocabulary requires recomputing the hash table.
*/
void SortVocab() {
int a, size;
unsigned int hash;
/*
* Sort the vocabulary by number of occurrences, in descending order.
*
* Keep </s> at the first position by sorting starting from index 1.
*
* Sorting the vocabulary this way causes the words with the fewest
* occurrences to be at the end of the vocabulary table. This will allow us
* to free the memory associated with the words that get filtered out.
*/
qsort(&vocab[1], vocab_size - 1, sizeof(struct vocab_word), VocabCompare);
// Clear the vocabulary hash table.
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
// Store the initial vocab size to use in the for loop condition.
size = vocab_size;
// Recompute the number of training words.
train_words = 0;
// For every word currently in the vocab...
for (a = 0; a < size; a++) {
// If it occurs fewer than 'min_count' times, remove it from the vocabulary.
if ((vocab[a].cn < min_count) && (a != 0)) {
// Decrease the size of the new vocabulary.
vocab_size--;
// Free the memory associated with the word string.
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;
}
}
// Reallocate the vocab array, chopping off all of the low-frequency words at
// the end of the table.
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;
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;
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++;
}
/**
* ======== CreateBinaryTree ========
* Create binary Huffman tree using the word counts.
* Frequent words will have short unique binary codes.
* Huffman encoding is used for lossless compression.
* For each vocabulary word, the vocab_word structure includes a `point` array,
* which is the list of internal tree nodes which:
* 1. Define the path from the root to the leaf node for the word.
* 2. Each correspond to a row of the output matrix.
* The `code` array is a list of 0s and 1s which specifies whether each output
* in `point` should be trained to output 0 or 1.
*/
void CreateBinaryTree() {
long long a, b, i, min1i, min2i, pos1, pos2, point[MAX_CODE_LENGTH];
char code[MAX_CODE_LENGTH]; // Default is 40
// Note that calloc initializes these arrays to 0.
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));
// The count array is twice the size of the vocabulary, plus one.
// - The first half of `count` becomes a list of the word counts
// for each word in the vocabulary. We do not modify this part of the
// list.
// - The second half of `count` is set to a large positive integer (1
// quadrillion). When we combine two trees under a word (e.g., word_id
// 13), then we place the total weight of those subtrees into the word's
// position in the second half (e.g., count[vocab_size + 13]).
//
for (a = 0; a < vocab_size; a++) count[a] = vocab[a].cn;
for (a = vocab_size; a < vocab_size * 2; a++) count[a] = 1e15;
// `pos1` and `pos2` are indeces into the `count` array.
// - `pos1` starts at the middle of `count` (the end of the list of word
// counts) and moves left.
// - `pos2` starts at the beginning of the list of large integers and moves
// right.
pos1 = vocab_size - 1;
pos2 = vocab_size;
/* ===============================
* Step 1: Create Huffman Tree
* ===============================
* [Original Comment] Following algorithm constructs the Huffman tree by
* adding one node at a time
*
* The Huffman coding algorithm starts with every node as its own tree, and
* then combines the two smallest trees on each step. The weight of a tree is
* the sum of the word counts for the words it contains.
*
* Once the tree is constructed, you can use the `parent_node` array to
* navigate it. For the word at index 13, for example, you would look at
* parent_node[13], and then parent_node[parent_node[13]], and so on, till
* you reach the root.
*
* A Huffman tree stores all of the words in the vocabulary at the leaves.
* Frequent words have short paths, and infrequent words have long paths.
* Here, we are also associating each internal node of the tree with a
* row of the output matrix. Every time we combine two trees and create a
* new node, we give it a row in the output matrix.
*/
// The number of tree combinations needed is equal to the size of the vocab,
// minus 1.
for (a = 0; a < vocab_size - 1; a++) {
// First, find two smallest nodes 'min1, min2'
// Find min1 (at index `min1i`)
if (pos1 >= 0) {
if (count[pos1] < count[pos2]) {
min1i = pos1;
pos1--;
} else {
min1i = pos2;
pos2++;
}
} else {
min1i = pos2;
pos2++;
}
// Find min2 (at index `min2i`).
if (pos1 >= 0) {
if (count[pos1] < count[pos2]) {
min2i = pos1;
pos1--;
} else {
min2i = pos2;
pos2++;
}
} else {
min2i = pos2;
pos2++;
}
// Calculate the combined weight. We could be combining two words, a word
// and a tree, or two trees.
count[vocab_size + a] = count[min1i] + count[min2i];
// Store the path for working back up the tree.
parent_node[min1i] = vocab_size + a;
parent_node[min2i] = vocab_size + a;
// binary[min1i] = 0; // This is implied.
// min1 is the (left?) node and is labeled '0', min2 is the (right?) node
// and is labeled '1'.
binary[min2i] = 1;
}
/* ==========================================
* Step 2: Define Samples for Each Word
* ==========================================
* [Original Comment] Now assign binary code to each vocabulary word
*
* vocab[word]
* .code - A variable-length string of 0s and 1s.
* .point - A variable-length array of output row indeces.
* .codelen - The length of the `code` array.
* The point array has length `codelen + 1`.
*
*/
// For each word in the vocabulary...
for (a = 0; a < vocab_size; a++) {
b = a;
i = 0; // `i` stores the code length.
// Construct the binary code...
// `code` stores 1s and 0s.
// `point` stores indeces.
// This loop works backwards from the leaf, so the `code` and `point`
// lists end up in reverse order.
while (1) {
// Lookup whether this is on the left or right of its parent node.
code[i] = binary[b];
// Note: point[0] always holds the word iteself...
point[i] = b;
// Increment the code length.
i++;
// This will always return an index in the second half of the array.
b = parent_node[b];
// We've reached the root when...
if (b == vocab_size * 2 - 2) break;
}
// Record the code length (the length of the `point` list).
vocab[a].codelen = i;
// The root node is at row `vocab_size - 2` of the output matrix.
vocab[a].point[0] = vocab_size - 2;
// For each bit in this word's code...
for (b = 0; b < i; b++) {
// Reverse the code in `code` and store it in `vocab[a].code`
vocab[a].code[i - b - 1] = code[b];
// Store the row indeces of the internal nodes leading to this word.
// These are the set of outputs which will be trained every time
// this word is encountered in the training data as an output word.
vocab[a].point[i - b] = point[b] - vocab_size;
}
}
free(count);
free(binary);
free(parent_node);
}
/**
* ======== LearnVocabFromTrainFile ========
* Builds a vocabulary from the words found in the training file.
*
* This function will also build a hash table which allows for fast lookup
* from the word string to the corresponding vocab_word object.
*
* Words that occur fewer than 'min_count' times will be filtered out of
* vocabulary.
*/
void LearnVocabFromTrainFile() {
char word[MAX_STRING];
FILE *fin;
long long a, i;
// Populate the vocab table with -1s.
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
// Open the training file.
fin = fopen(train_file, "rb");
if (fin == NULL) {
printf("ERROR: training data file not found!\n");
exit(1);
}
vocab_size = 0;
// The special token </s> is used to mark the end of a sentence. In training,
// the context window does not go beyond the ends of a sentence.
//
// Add </s> explicitly here so that it occurs at position 0 in the vocab.
AddWordToVocab((char *)"</s>");
while (1) {
// Read the next word from the file into the string 'word'.
ReadWord(word, fin);
// Stop when we've reached the end of the file.
if (feof(fin)) break;
// Count the total number of tokens in the training text.
train_words++;
// Print progress at every 100,000 words.
if ((debug_mode > 1) && (train_words % 100000 == 0)) {
printf("%lldK%c", train_words / 1000, 13);
fflush(stdout);
}
// Look up this word in the vocab to see if we've already added it.
i = SearchVocab(word);
// If it's not in the vocab...
if (i == -1) {
// ...add it.
a = AddWordToVocab(word);
// Initialize the word frequency to 1.
vocab[a].cn = 1;
// If it's already in the vocab, just increment the word count.
} else vocab[i].cn++;
// If the vocabulary has grown too large, trim out the most infrequent
// words. The vocabulary is considered "too large" when it's filled more
// than 70% of the hash table (this is to try and keep hash collisions
// down).
if (vocab_size > vocab_hash_size * 0.7) ReduceVocab();
}
// Sort the vocabulary in descending order by number of word occurrences.
// Remove (and free the associated memory) for all the words that occur
// fewer than 'min_count' times.
SortVocab();
// Report the final vocabulary size, and the total number of words
// (excluding those filtered from the vocabulary) in the training set.
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");
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;
char c;
char word[MAX_STRING];
FILE *fin = fopen(read_vocab_file, "rb");
if (fin == NULL) {
printf("Vocabulary file not found\n");
exit(1);
}
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
vocab_size = 0;
while (1) {
ReadWord(word, fin);
if (feof(fin)) break;
a = AddWordToVocab(word);
fscanf(fin, "%lld%c", &vocab[a].cn, &c);
i++;
}
SortVocab();
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");
if (fin == NULL) {
printf("ERROR: training data file not found!\n");
exit(1);
}
fseek(fin, 0, SEEK_END);
file_size = ftell(fin);
fclose(fin);
}
/**
* ======== InitNet ========
*
*/
void InitNet() {
long long a, b;
unsigned long long next_random = 1;
// Allocate the hidden layer of the network, which is what becomes the word vectors.
// The variable for this layer is 'syn0'.
a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real));
if (syn0 == NULL) {printf("Memory allocation failed\n"); exit(1);}
// If we're using hierarchical softmax for training...
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++) for (b = 0; b < layer1_size; b++)
syn1[a * layer1_size + b] = 0;
}
// If we're using negative sampling for training...
if (negative>0) {
// Allocate the output layer of the network.
// The variable for this layer is 'syn1neg'.
// This layer has the same size as the hidden layer, but is the transpose.
a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real));
if (syn1neg == NULL) {printf("Memory allocation failed\n"); exit(1);}
// Set all of the weights in the output layer to 0.
for (a = 0; a < vocab_size; a++) for (b = 0; b < layer1_size; b++)
syn1neg[a * layer1_size + b] = 0;
}
// Randomly initialize the weights for the hidden layer (word vector layer).
// TODO - What's the equation here?
for (a = 0; a < vocab_size; a++) 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;
}
// Create a binary tree for Huffman coding.
// TODO - As best I can tell, this is only used for hierarchical softmax training...
CreateBinaryTree();
}
/**
* ======== TrainModelThread ========
* This function performs the training of the model.
*/
void *TrainModelThread(void *id) {
/*
* word - Stores the index of a word in the vocab table.
* word_count - Stores the total number of training words processed.
*/
long long a, b, d, cw, word, last_word, sentence_length = 0, sentence_position = 0;
long long word_count = 0, last_word_count = 0, sen[MAX_SENTENCE_LENGTH + 1];
long long l1, l2, c, target, label, local_iter = iter;
unsigned long long next_random = (long long)id;
real f, g;
clock_t now;
// neu1 is only used by the CBOW architecture.
real *neu1 = (real *)calloc(layer1_size, sizeof(real));
// neu1e is used by both architectures.
real *neu1e = (real *)calloc(layer1_size, sizeof(real));
// Open the training file and seek to the portion of the file that this
// thread is responsible for.
FILE *fi = fopen(train_file, "rb");
fseek(fi, file_size / (long long)num_threads * (long long)id, SEEK_SET);
// This loop covers the whole training operation...
while (1) {
/*
* ======== Variables ========
* iter - This is the number of training epochs to run; default is 5.
* word_count - The number of input words processed.
* train_words - The total number of words in the training text (not
* including words removed from the vocabuly by ReduceVocab).
*/
// This block prints a progress update, and also adjusts the training
// 'alpha' parameter.
if (word_count - last_word_count > 10000) {
word_count_actual += word_count - last_word_count;
last_word_count = word_count;
// The percentage complete is based on the total number of passes we are
// doing and not just the current pass.
if ((debug_mode > 1)) {
now=clock();
printf("%cAlpha: %f Progress: %.2f%% Words/thread/sec: %.2fk ", 13, alpha,
// Percent complete = [# of input words processed] /
// ([# of passes] * [# of words in a pass])
word_count_actual / (real)(iter * train_words + 1) * 100,
word_count_actual / ((real)(now - start + 1) / (real)CLOCKS_PER_SEC * 1000));
fflush(stdout);
}
// Update alpha to: [initial alpha] * [percent of training remaining]
// This means that alpha will gradually decrease as we progress through
// the training text.
alpha = starting_alpha * (1 - word_count_actual / (real)(iter * train_words + 1));
// Don't let alpha go below [initial alpha] * 0.0001.
if (alpha < starting_alpha * 0.0001) alpha = starting_alpha * 0.0001;
}
// This 'if' block retrieves the next sentence from the training text and
// stores it in 'sen'.
// TODO - Under what condition would sentence_length not be zero?
if (sentence_length == 0) {
while (1) {
// Read the next word from the training data and lookup its index in
// the vocab table. 'word' is the word's vocab index.
word = ReadWordIndex(fi);
if (feof(fi)) break;
// If the word doesn't exist in the vocabulary, skip it.
if (word == -1) continue;
// Track the total number of training words processed.
word_count++;
// 'vocab' word 0 is a special token "</s>" which indicates the end of
// a sentence.
if (word == 0) break;
/*
* =================================
* Subsampling of Frequent Words
* =================================
* This code randomly discards training words, but is designed to
* keep the relative frequencies the same. That is, less frequent
* words will be discarded less often.
*
* We first calculate the probability that we want to *keep* the word;
* this is the value 'ran'. Then, to decide whether to keep the word,
* we generate a random fraction (0.0 - 1.0), and if 'ran' is smaller
* than this number, we discard the word. This means that the smaller
* 'ran' is, the more likely it is that we'll discard this word.
*
* The quantity (vocab[word].cn / train_words) is the fraction of all
* the training words which are 'word'. Let's represent this fraction
* by x.
*
* Using the default 'sample' value of 0.001, the equation for ran is:
* ran = (sqrt(x / 0.001) + 1) * (0.001 / x)
*
* You can plot this function to see it's behavior; it has a curved
* L shape.
*
* Here are some interesting points in this function (again this is
* using the default sample value of 0.001).
* - ran = 1 (100% chance of being kept) when x <= 0.0026.
* - That is, any word which is 0.0026 of the words *or fewer*
* will be kept 100% of the time. Only words which represent
* more than 0.26% of the total words will be subsampled.
* - ran = 0.5 (50% chance of being kept) when x = 0.00746.
* - ran = 0.033 (3.3% chance of being kept) when x = 1.
* - That is, if a word represented 100% of the training set
* (which of course would never happen), it would only be
* kept 3.3% of the time.
*
* NOTE: Seems like it would be more efficient to pre-calculate this
* probability for each word and store it in the vocab table...
*
* Words that are discarded by subsampling aren't added to our training
* 'sentence'. This means the discarded word is neither used as an
* input word or a context word for other inputs.
*/
if (sample > 0) {
// Calculate the probability of keeping 'word'.
real ran = (sqrt(vocab[word].cn / (sample * train_words)) + 1) * (sample * train_words) / vocab[word].cn;
// Generate a random number.
// The multiplier is 25.xxx billion, so 'next_random' is a 64-bit integer.
next_random = next_random * (unsigned long long)25214903917 + 11;
// If the probability is less than a random fraction, discard the word.
//
// (next_random & 0xFFFF) extracts just the lower 16 bits of the
// random number. Dividing this by 65536 (2^16) gives us a fraction
// between 0 and 1. So the code is just generating a random fraction.
if (ran < (next_random & 0xFFFF) / (real)65536) continue;
}
// If we kept the word, add it to the sentence.
sen[sentence_length] = word;
sentence_length++;
// Verify the sentence isn't too long.
if (sentence_length >= MAX_SENTENCE_LENGTH) break;
}
sentence_position = 0;
}
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;
}
// Get the next word in the sentence. The word is represented by its index
// into the vocab table.
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;
// This is a standard random integer generator, as seen here:
// https://en.wikipedia.org/wiki/Linear_congruential_generator
next_random = next_random * (unsigned long long)25214903917 + 11;
// 'b' becomes a random integer between 0 and 'window' - 1.
// This is the amount we will shrink the window size by.
b = next_random % window;
/*
* ====================================
* CBOW Architecture
* ====================================
* sen - This is the array of words in the sentence. Subsampling has
* already been applied. Words are represented by their ids.
*
* sentence_position - This is the index of the current input word.
*
* a - Offset into the current window, relative to the window start.
* a will range from 0 to (window * 2)
*
* b - The amount to shrink the context window by.
*
* c - 'c' is a scratch variable used in two unrelated ways:
* 1. It's first used as the index of the current context word
* within the sentence (the `sen` array).
* 2. It's then used as the for-loop variable for calculating
* vector dot-products and other arithmetic.
*
* syn0 - The hidden layer weights. Note that the weights are stored as a
* 1D array, so word 'i' is found at (i * layer1_size).
*
* target - The output word we're working on. If it's the positive sample
* then `label` is 1. `label` is 0 for negative samples.
* Note: `target` and `label` are only used in negative sampling,
* and not HS.
*
* neu1 - This vector will hold the *average* of all of the context word
* vectors. This is the output of the hidden layer for CBOW.
*
* neu1e - Holds the gradient for updating the hidden layer weights.
* It's a vector of length 300, not a matrix.
* This same gradient update is applied to all context word
* vectors.