blob: 2bed2a16b43f07b60bc7c1fb63e4e42cda6c696f [file] [log] [blame]
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001// Copyright 2013 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <math.h>
19#include <pthread.h>
20
21#define MAX_STRING 100
22#define EXP_TABLE_SIZE 1000
23#define MAX_EXP 6
24#define MAX_SENTENCE_LENGTH 1000
Marc Kupietz71996e72016-03-18 13:40:24 +010025#define MAX_CC 100
Marc Kupietzd6f9c712016-03-16 11:50:56 +010026#define MAX_CODE_LENGTH 40
27
28const int vocab_hash_size = 30000000; // Maximum 30 * 0.7 = 21M words in the vocabulary
29
30typedef float real; // Precision of float numbers
31
32struct vocab_word {
33 long long cn;
34 int *point;
35 char *word, *code, codelen;
36};
37
38char train_file[MAX_STRING], output_file[MAX_STRING];
39char save_vocab_file[MAX_STRING], read_vocab_file[MAX_STRING];
40char save_net_file[MAX_STRING], read_net_file[MAX_STRING];
41struct vocab_word *vocab;
42int binary = 0, type = 1, debug_mode = 2, window = 5, min_count = 5,
43 num_threads = 12, min_reduce = 1;
44int *vocab_hash;
45long long vocab_max_size = 1000, vocab_size = 0, layer1_size = 100;
46long long train_words = 0, word_count_actual = 0, iter = 5, file_size = 0,
47 classes = 0;
48real alpha = 0.025, starting_alpha, sample = 1e-3;
49real *syn0, *syn1, *syn1neg, *syn1nce, *expTable;
50clock_t start;
51
52real *syn1_window, *syn1neg_window, *syn1nce_window;
53int w_offset, window_layer_size;
54
55int window_hidden_size = 500;
56real *syn_window_hidden, *syn_hidden_word, *syn_hidden_word_neg,
57 *syn_hidden_word_nce;
58
59int hs = 0, negative = 5;
60const int table_size = 1e8;
61int *table;
62
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +010063long cc = 0;
64
Marc Kupietzd6f9c712016-03-16 11:50:56 +010065//constrastive negative sampling
66char negative_classes_file[MAX_STRING];
67int *word_to_group;
68int *group_to_table; //group_size*table_size
69int class_number;
70
71//nce
72real* noise_distribution;
73int nce = 0;
74
75//param caps
76real CAP_VALUE = 50;
77int cap = 0;
78
79void capParam(real* array, int index) {
80 if (array[index] > CAP_VALUE)
81 array[index] = CAP_VALUE;
82 else if (array[index] < -CAP_VALUE)
83 array[index] = -CAP_VALUE;
84}
85
86real hardTanh(real x) {
87 if (x >= 1) {
88 return 1;
89 } else if (x <= -1) {
90 return -1;
91 } else {
92 return x;
93 }
94}
95
96real dHardTanh(real x, real g) {
97 if (x > 1 && g > 0) {
98 return 0;
99 }
100 if (x < -1 && g < 0) {
101 return 0;
102 }
103 return 1;
104}
105
106void InitUnigramTable() {
107 int a, i;
108 long long train_words_pow = 0;
109 real d1, power = 0.75;
110 table = (int *) malloc(table_size * sizeof(int));
111 for (a = 0; a < vocab_size; a++)
112 train_words_pow += pow(vocab[a].cn, power);
113 i = 0;
114 d1 = pow(vocab[i].cn, power) / (real) train_words_pow;
115 for (a = 0; a < table_size; a++) {
116 table[a] = i;
117 if (a / (real) table_size > d1) {
118 i++;
119 d1 += pow(vocab[i].cn, power) / (real) train_words_pow;
120 }
121 if (i >= vocab_size)
122 i = vocab_size - 1;
123 }
124
125 noise_distribution = (real *) calloc(vocab_size, sizeof(real));
126 for (a = 0; a < vocab_size; a++)
127 noise_distribution[a] = pow(vocab[a].cn, power)
128 / (real) train_words_pow;
129}
130
131// Reads a single word from a file, assuming space + tab + EOL to be word boundaries
132void ReadWord(char *word, FILE *fin) {
133 int a = 0, ch;
134 while (!feof(fin)) {
135 ch = fgetc(fin);
136 if (ch == 13)
137 continue;
138 if ((ch == ' ') || (ch == '\t') || (ch == '\n')) {
139 if (a > 0) {
140 if (ch == '\n')
141 ungetc(ch, fin);
142 break;
143 }
144 if (ch == '\n') {
145 strcpy(word, (char *) "</s>");
146 return;
147 } else
148 continue;
149 }
150 word[a] = ch;
151 a++;
152 if (a >= MAX_STRING - 1)
153 a--; // Truncate too long words
154 }
155 word[a] = 0;
156}
157
158// Returns hash value of a word
159int GetWordHash(char *word) {
160 unsigned long long a, hash = 0;
161 for (a = 0; a < strlen(word); a++)
162 hash = hash * 257 + word[a];
163 hash = hash % vocab_hash_size;
164 return hash;
165}
166
167// Returns position of a word in the vocabulary; if the word is not found, returns -1
168int SearchVocab(char *word) {
169 unsigned int hash = GetWordHash(word);
170 while (1) {
171 if (vocab_hash[hash] == -1)
172 return -1;
173 if (!strcmp(word, vocab[vocab_hash[hash]].word))
174 return vocab_hash[hash];
175 hash = (hash + 1) % vocab_hash_size;
176 }
177 return -1;
178}
179
180// Reads a word and returns its index in the vocabulary
181int ReadWordIndex(FILE *fin) {
182 char word[MAX_STRING];
183 ReadWord(word, fin);
184 if (feof(fin))
185 return -1;
186 return SearchVocab(word);
187}
188
189// Adds a word to the vocabulary
190int AddWordToVocab(char *word) {
191 unsigned int hash, length = strlen(word) + 1;
192 if (length > MAX_STRING)
193 length = MAX_STRING;
194 vocab[vocab_size].word = (char *) calloc(length, sizeof(char));
195 strcpy(vocab[vocab_size].word, word);
196 vocab[vocab_size].cn = 0;
197 vocab_size++;
198 // Reallocate memory if needed
199 if (vocab_size + 2 >= vocab_max_size) {
200 vocab_max_size += 1000;
201 vocab = (struct vocab_word *) realloc(vocab,
202 vocab_max_size * sizeof(struct vocab_word));
203 }
204 hash = GetWordHash(word);
205 while (vocab_hash[hash] != -1)
206 hash = (hash + 1) % vocab_hash_size;
207 vocab_hash[hash] = vocab_size - 1;
208 return vocab_size - 1;
209}
210
211// Used later for sorting by word counts
212int VocabCompare(const void *a, const void *b) {
213 return ((struct vocab_word *) b)->cn - ((struct vocab_word *) a)->cn;
214}
215
216// Sorts the vocabulary by frequency using word counts
217void SortVocab() {
218 int a, size;
219 unsigned int hash;
220 // Sort the vocabulary and keep </s> at the first position
221 qsort(&vocab[1], vocab_size - 1, sizeof(struct vocab_word), VocabCompare);
222 for (a = 0; a < vocab_hash_size; a++)
223 vocab_hash[a] = -1;
224 size = vocab_size;
225 train_words = 0;
226 for (a = 0; a < size; a++) {
227 // Words occuring less than min_count times will be discarded from the vocab
228 if ((vocab[a].cn < min_count) && (a != 0)) {
229 vocab_size--;
230 free(vocab[a].word);
231 } else {
232 // Hash will be re-computed, as after the sorting it is not actual
233 hash = GetWordHash(vocab[a].word);
234 while (vocab_hash[hash] != -1)
235 hash = (hash + 1) % vocab_hash_size;
236 vocab_hash[hash] = a;
237 train_words += vocab[a].cn;
238 }
239 }
240 vocab = (struct vocab_word *) realloc(vocab,
241 (vocab_size + 1) * sizeof(struct vocab_word));
242 // Allocate memory for the binary tree construction
243 for (a = 0; a < vocab_size; a++) {
244 vocab[a].code = (char *) calloc(MAX_CODE_LENGTH, sizeof(char));
245 vocab[a].point = (int *) calloc(MAX_CODE_LENGTH, sizeof(int));
246 }
247}
248
249// Reduces the vocabulary by removing infrequent tokens
250void ReduceVocab() {
251 int a, b = 0;
252 unsigned int hash;
253 for (a = 0; a < vocab_size; a++)
254 if (vocab[a].cn > min_reduce) {
255 vocab[b].cn = vocab[a].cn;
256 vocab[b].word = vocab[a].word;
257 b++;
258 } else
259 free(vocab[a].word);
260 vocab_size = b;
261 for (a = 0; a < vocab_hash_size; a++)
262 vocab_hash[a] = -1;
263 for (a = 0; a < vocab_size; a++) {
264 // Hash will be re-computed, as it is not actual
265 hash = GetWordHash(vocab[a].word);
266 while (vocab_hash[hash] != -1)
267 hash = (hash + 1) % vocab_hash_size;
268 vocab_hash[hash] = a;
269 }
270 fflush(stdout);
271 min_reduce++;
272}
273
274// Create binary Huffman tree using the word counts
275// Frequent words will have short uniqe binary codes
276void CreateBinaryTree() {
277 long long a, b, i, min1i, min2i, pos1, pos2, point[MAX_CODE_LENGTH];
278 char code[MAX_CODE_LENGTH];
279 long long *count = (long long *) calloc(vocab_size * 2 + 1,
280 sizeof(long long));
281 long long *binary = (long long *) calloc(vocab_size * 2 + 1,
282 sizeof(long long));
283 long long *parent_node = (long long *) calloc(vocab_size * 2 + 1,
284 sizeof(long long));
285 for (a = 0; a < vocab_size; a++)
286 count[a] = vocab[a].cn;
287 for (a = vocab_size; a < vocab_size * 2; a++)
288 count[a] = 1e15;
289 pos1 = vocab_size - 1;
290 pos2 = vocab_size;
291 // Following algorithm constructs the Huffman tree by adding one node at a time
292 for (a = 0; a < vocab_size - 1; a++) {
293 // First, find two smallest nodes 'min1, min2'
294 if (pos1 >= 0) {
295 if (count[pos1] < count[pos2]) {
296 min1i = pos1;
297 pos1--;
298 } else {
299 min1i = pos2;
300 pos2++;
301 }
302 } else {
303 min1i = pos2;
304 pos2++;
305 }
306 if (pos1 >= 0) {
307 if (count[pos1] < count[pos2]) {
308 min2i = pos1;
309 pos1--;
310 } else {
311 min2i = pos2;
312 pos2++;
313 }
314 } else {
315 min2i = pos2;
316 pos2++;
317 }
318 count[vocab_size + a] = count[min1i] + count[min2i];
319 parent_node[min1i] = vocab_size + a;
320 parent_node[min2i] = vocab_size + a;
321 binary[min2i] = 1;
322 }
323 // Now assign binary code to each vocabulary word
324 for (a = 0; a < vocab_size; a++) {
325 b = a;
326 i = 0;
327 while (1) {
328 code[i] = binary[b];
329 point[i] = b;
330 i++;
331 b = parent_node[b];
332 if (b == vocab_size * 2 - 2)
333 break;
334 }
335 vocab[a].codelen = i;
336 vocab[a].point[0] = vocab_size - 2;
337 for (b = 0; b < i; b++) {
338 vocab[a].code[i - b - 1] = code[b];
339 vocab[a].point[i - b] = point[b] - vocab_size;
340 }
341 }
342 free(count);
343 free(binary);
344 free(parent_node);
345}
346
347void LearnVocabFromTrainFile() {
348 char word[MAX_STRING];
349 FILE *fin;
350 long long a, i;
351 for (a = 0; a < vocab_hash_size; a++)
352 vocab_hash[a] = -1;
353 fin = fopen(train_file, "rb");
354 if (fin == NULL) {
355 printf("ERROR: training data file not found!\n");
356 exit(1);
357 }
358 vocab_size = 0;
359 AddWordToVocab((char *) "</s>");
360 while (1) {
361 ReadWord(word, fin);
362 if (feof(fin))
363 break;
364 train_words++;
365 if ((debug_mode > 1) && (train_words % 100000 == 0)) {
366 printf("%lldK%c", train_words / 1000, 13);
367 fflush(stdout);
368 }
369 i = SearchVocab(word);
370 if (i == -1) {
371 a = AddWordToVocab(word);
372 vocab[a].cn = 1;
373 } else
374 vocab[i].cn++;
375 if (vocab_size > vocab_hash_size * 0.7)
376 ReduceVocab();
377 }
378 SortVocab();
379 if (debug_mode > 0) {
380 printf("Vocab size: %lld\n", vocab_size);
381 printf("Words in train file: %lld\n", train_words);
382 }
383 file_size = ftell(fin);
384 fclose(fin);
385}
386
387void SaveVocab() {
388 long long i;
389 FILE *fo = fopen(save_vocab_file, "wb");
390 for (i = 0; i < vocab_size; i++)
391 fprintf(fo, "%s %lld\n", vocab[i].word, vocab[i].cn);
392 fclose(fo);
393}
394
395void ReadVocab() {
396 long long a, i = 0;
397 char c;
398 char word[MAX_STRING];
399 FILE *fin = fopen(read_vocab_file, "rb");
400 if (fin == NULL) {
401 printf("Vocabulary file not found\n");
402 exit(1);
403 }
404 for (a = 0; a < vocab_hash_size; a++)
405 vocab_hash[a] = -1;
406 vocab_size = 0;
407 while (1) {
408 ReadWord(word, fin);
409 if (feof(fin))
410 break;
411 a = AddWordToVocab(word);
412 fscanf(fin, "%lld%c", &vocab[a].cn, &c);
413 i++;
414 }
415 SortVocab();
416 if (debug_mode > 0) {
417 printf("Vocab size: %lld\n", vocab_size);
418 printf("Words in train file: %lld\n", train_words);
419 }
420 fin = fopen(train_file, "rb");
421 if (fin == NULL) {
422 printf("ERROR: training data file not found!\n");
423 exit(1);
424 }
425 fseek(fin, 0, SEEK_END);
426 file_size = ftell(fin);
427 fclose(fin);
428}
429
430void InitClassUnigramTable() {
431 long long a, c;
432 printf("loading class unigrams \n");
433 FILE *fin = fopen(negative_classes_file, "rb");
434 if (fin == NULL) {
435 printf("ERROR: class file not found!\n");
436 exit(1);
437 }
438 word_to_group = (int *) malloc(vocab_size * sizeof(int));
439 for (a = 0; a < vocab_size; a++)
440 word_to_group[a] = -1;
441 char class[MAX_STRING];
442 char prev_class[MAX_STRING];
443 prev_class[0] = 0;
444 char word[MAX_STRING];
445 class_number = -1;
446 while (1) {
447 if (feof(fin))
448 break;
449 ReadWord(class, fin);
450 ReadWord(word, fin);
451 int word_index = SearchVocab(word);
452 if (word_index != -1) {
453 if (strcmp(class, prev_class) != 0) {
454 class_number++;
455 strcpy(prev_class, class);
456 }
457 word_to_group[word_index] = class_number;
458 }
459 ReadWord(word, fin);
460 }
461 class_number++;
462 fclose(fin);
463
464 group_to_table = (int *) malloc(table_size * class_number * sizeof(int));
465 long long train_words_pow = 0;
466 real d1, power = 0.75;
467
468 for (c = 0; c < class_number; c++) {
469 long long offset = c * table_size;
470 train_words_pow = 0;
471 for (a = 0; a < vocab_size; a++)
472 if (word_to_group[a] == c)
473 train_words_pow += pow(vocab[a].cn, power);
474 int i = 0;
475 while (word_to_group[i] != c && i < vocab_size)
476 i++;
477 d1 = pow(vocab[i].cn, power) / (real) train_words_pow;
478 for (a = 0; a < table_size; a++) {
479 //printf("index %lld , word %d\n", a, i);
480 group_to_table[offset + a] = i;
481 if (a / (real) table_size > d1) {
482 i++;
483 while (word_to_group[i] != c && i < vocab_size)
484 i++;
485 d1 += pow(vocab[i].cn, power) / (real) train_words_pow;
486 }
487 if (i >= vocab_size)
488 while (word_to_group[i] != c && i >= 0)
489 i--;
490 }
491 }
492}
493
494void SaveNet() {
Marc Kupietz313fcc52016-03-16 16:43:37 +0100495 if(type != 3 || negative <= 0) {
496 fprintf(stderr, "save-net only supported for type 3 with negative sampling\n");
497 return;
498 }
499
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100500 FILE *fnet = fopen(save_net_file, "wb");
501 if (fnet == NULL) {
502 printf("Net parameter file not found\n");
503 exit(1);
504 }
Marc Kupietzc6979332016-03-16 15:29:07 +0100505 fwrite(syn0, sizeof(real), vocab_size * layer1_size, fnet);
Marc Kupietz313fcc52016-03-16 16:43:37 +0100506 fwrite(syn1neg_window, sizeof(real), vocab_size * window_layer_size, fnet);
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100507 fclose(fnet);
508}
509
510void InitNet() {
511 long long a, b;
512 unsigned long long next_random = 1;
Marc Kupietz57c0df12016-03-18 12:48:00 +0100513 long long read;
514
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100515 window_layer_size = layer1_size * window * 2;
516 a = posix_memalign((void **) &syn0, 128,
517 (long long) vocab_size * layer1_size * sizeof(real));
518 if (syn0 == NULL) {
519 printf("Memory allocation failed\n");
520 exit(1);
521 }
522
523 if (hs) {
524 a = posix_memalign((void **) &syn1, 128,
525 (long long) vocab_size * layer1_size * sizeof(real));
526 if (syn1 == NULL) {
527 printf("Memory allocation failed\n");
528 exit(1);
529 }
530 a = posix_memalign((void **) &syn1_window, 128,
531 (long long) vocab_size * window_layer_size * sizeof(real));
532 if (syn1_window == NULL) {
533 printf("Memory allocation failed\n");
534 exit(1);
535 }
536 a = posix_memalign((void **) &syn_hidden_word, 128,
537 (long long) vocab_size * window_hidden_size * sizeof(real));
538 if (syn_hidden_word == NULL) {
539 printf("Memory allocation failed\n");
540 exit(1);
541 }
542
543 for (a = 0; a < vocab_size; a++)
544 for (b = 0; b < layer1_size; b++)
545 syn1[a * layer1_size + b] = 0;
546 for (a = 0; a < vocab_size; a++)
547 for (b = 0; b < window_layer_size; b++)
548 syn1_window[a * window_layer_size + b] = 0;
549 for (a = 0; a < vocab_size; a++)
550 for (b = 0; b < window_hidden_size; b++)
551 syn_hidden_word[a * window_hidden_size + b] = 0;
552 }
553 if (negative > 0) {
Marc Kupietz1006a272016-03-16 15:50:20 +0100554 if(type == 0) {
555 a = posix_memalign((void **) &syn1neg, 128,
556 (long long) vocab_size * layer1_size * sizeof(real));
557 if (syn1neg == NULL) {
558 printf("Memory allocation failed\n");
559 exit(1);
560 }
561 for (a = 0; a < vocab_size; a++)
562 for (b = 0; b < layer1_size; b++)
563 syn1neg[a * layer1_size + b] = 0;
564 } else if (type == 3) {
565 a = posix_memalign((void **) &syn1neg_window, 128,
566 (long long) vocab_size * window_layer_size * sizeof(real));
567 if (syn1neg_window == NULL) {
568 printf("Memory allocation failed\n");
569 exit(1);
570 }
571 for (a = 0; a < vocab_size; a++)
572 for (b = 0; b < window_layer_size; b++)
573 syn1neg_window[a * window_layer_size + b] = 0;
574 } else if (type == 4) {
575 a = posix_memalign((void **) &syn_hidden_word_neg, 128,
576 (long long) vocab_size * window_hidden_size * sizeof(real));
577 if (syn_hidden_word_neg == NULL) {
578 printf("Memory allocation failed\n");
579 exit(1);
580 }
581 for (a = 0; a < vocab_size; a++)
582 for (b = 0; b < window_hidden_size; b++)
583 syn_hidden_word_neg[a * window_hidden_size + b] = 0;
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100584 }
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100585 }
586 if (nce > 0) {
587 a = posix_memalign((void **) &syn1nce, 128,
588 (long long) vocab_size * layer1_size * sizeof(real));
589 if (syn1nce == NULL) {
590 printf("Memory allocation failed\n");
591 exit(1);
592 }
593 a = posix_memalign((void **) &syn1nce_window, 128,
594 (long long) vocab_size * window_layer_size * sizeof(real));
595 if (syn1nce_window == NULL) {
596 printf("Memory allocation failed\n");
597 exit(1);
598 }
599 a = posix_memalign((void **) &syn_hidden_word_nce, 128,
600 (long long) vocab_size * window_hidden_size * sizeof(real));
601 if (syn_hidden_word_nce == NULL) {
602 printf("Memory allocation failed\n");
603 exit(1);
604 }
605
606 for (a = 0; a < vocab_size; a++)
607 for (b = 0; b < layer1_size; b++)
608 syn1nce[a * layer1_size + b] = 0;
609 for (a = 0; a < vocab_size; a++)
610 for (b = 0; b < window_layer_size; b++)
611 syn1nce_window[a * window_layer_size + b] = 0;
612 for (a = 0; a < vocab_size; a++)
613 for (b = 0; b < window_hidden_size; b++)
614 syn_hidden_word_nce[a * window_hidden_size + b] = 0;
615 }
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100616
Marc Kupietz1006a272016-03-16 15:50:20 +0100617 if(type == 4) {
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100618 a = posix_memalign((void **) &syn_window_hidden, 128,
619 window_hidden_size * window_layer_size * sizeof(real));
620 if (syn_window_hidden == NULL) {
621 printf("Memory allocation failed\n");
622 exit(1);
623 }
624 for (a = 0; a < window_hidden_size * window_layer_size; a++) {
625 next_random = next_random * (unsigned long long) 25214903917 + 11;
626 syn_window_hidden[a] = (((next_random & 0xFFFF) / (real) 65536)
627 - 0.5) / (window_hidden_size * window_layer_size);
628 }
629 }
Marc Kupietz1006a272016-03-16 15:50:20 +0100630
631 if (read_net_file[0] == 0) {
632 for (a = 0; a < vocab_size; a++)
633 for (b = 0; b < layer1_size; b++) {
634 next_random = next_random * (unsigned long long) 25214903917
635 + 11;
636 syn0[a * layer1_size + b] = (((next_random & 0xFFFF)
637 / (real) 65536) - 0.5) / layer1_size;
638 }
Marc Kupietz313fcc52016-03-16 16:43:37 +0100639 } else if(type == 3 && negative > 0) {
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100640 FILE *fnet = fopen(read_net_file, "rb");
641 if (fnet == NULL) {
642 printf("Net parameter file not found\n");
643 exit(1);
644 }
Marc Kupietz57c0df12016-03-18 12:48:00 +0100645 printf("vocab-size: %lld, layer1_size: %lld, window_layer_size %d\n", vocab_size, layer1_size, window_layer_size);
646 read = fread(syn0, sizeof(real), vocab_size * layer1_size, fnet);
647 if(read != vocab_size * layer1_size) {
648 fprintf(stderr, "read-net failed %lld\n", read);
649 exit(-1);
650 }
651 read = fread(syn1neg_window, sizeof(real), vocab_size * window_layer_size, fnet);
652 if(read != (long long) vocab_size * window_layer_size) {
653 fprintf(stderr, "read-net failed, read %lld, expected: %lld\n", read ,
654 (long long) sizeof(real) * vocab_size * window_layer_size);
655 exit(-1);
656 }
657 fgetc(fnet);
658 if(!feof(fnet)) {
659 fprintf(stderr, "Remaining bytes in net-file after read-net. File position: %ld\n", ftell(fnet));
660 exit(-1);
661 }
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100662 fclose(fnet);
Marc Kupietz313fcc52016-03-16 16:43:37 +0100663 } else {
664 fprintf(stderr, "read-net only supported for type 3 with negative sampling\n");
665 exit(-1);
Marc Kupietzd6f9c712016-03-16 11:50:56 +0100666 }
667
668 CreateBinaryTree();
669}
670
671void *TrainModelThread(void *id) {
672 long long a, b, d, cw, word, last_word, sentence_length = 0,
673 sentence_position = 0;
674 long long word_count = 0, last_word_count = 0, sen[MAX_SENTENCE_LENGTH + 1];
675 long long l1, l2, c, target, label, local_iter = iter;
676 unsigned long long next_random = (long long) id;
677 real f, g;
678 clock_t now;
679 int input_len_1 = layer1_size;
680 int window_offset = -1;
681 if (type == 2 || type == 4) {
682 input_len_1 = window_layer_size;
683 }
684 real *neu1 = (real *) calloc(input_len_1, sizeof(real));
685 real *neu1e = (real *) calloc(input_len_1, sizeof(real));
686
687 int input_len_2 = 0;
688 if (type == 4) {
689 input_len_2 = window_hidden_size;
690 }
691 real *neu2 = (real *) calloc(input_len_2, sizeof(real));
692 real *neu2e = (real *) calloc(input_len_2, sizeof(real));
693
694 FILE *fi = fopen(train_file, "rb");
695 fseek(fi, file_size / (long long) num_threads * (long long) id, SEEK_SET);
696 while (1) {
697 if (word_count - last_word_count > 10000) {
698 word_count_actual += word_count - last_word_count;
699 last_word_count = word_count;
700 if ((debug_mode > 1)) {
701 now = clock();
702 printf(
703 "%cAlpha: %f Progress: %.2f%% Words/thread/sec: %.2fk ",
704 13, alpha,
705 word_count_actual / (real) (iter * train_words + 1)
706 * 100,
707 word_count_actual
708 / ((real) (now - start + 1)
709 / (real) CLOCKS_PER_SEC * 1000));
710 fflush(stdout);
711 }
712 alpha = starting_alpha
713 * (1 - word_count_actual / (real) (iter * train_words + 1));
714 if (alpha < starting_alpha * 0.0001)
715 alpha = starting_alpha * 0.0001;
716 }
717 if (sentence_length == 0) {
718 while (1) {
719 word = ReadWordIndex(fi);
720 if (feof(fi))
721 break;
722 if (word == -1)
723 continue;
724 word_count++;
725 if (word == 0)
726 break;
727 // The subsampling randomly discards frequent words while keeping the ranking same
728 if (sample > 0) {
729 real ran = (sqrt(vocab[word].cn / (sample * train_words))
730 + 1) * (sample * train_words) / vocab[word].cn;
731 next_random = next_random * (unsigned long long) 25214903917
732 + 11;
733 if (ran < (next_random & 0xFFFF) / (real) 65536)
734 continue;
735 }
736 sen[sentence_length] = word;
737 sentence_length++;
738 if (sentence_length >= MAX_SENTENCE_LENGTH)
739 break;
740 }
741 sentence_position = 0;
742 }
743 if (feof(fi) || (word_count > train_words / num_threads)) {
744 word_count_actual += word_count - last_word_count;
745 local_iter--;
746 if (local_iter == 0)
747 break;
748 word_count = 0;
749 last_word_count = 0;
750 sentence_length = 0;
751 fseek(fi, file_size / (long long) num_threads * (long long) id,
752 SEEK_SET);
753 continue;
754 }
755 word = sen[sentence_position];
756 if (word == -1)
757 continue;
758 for (c = 0; c < input_len_1; c++)
759 neu1[c] = 0;
760 for (c = 0; c < input_len_1; c++)
761 neu1e[c] = 0;
762 for (c = 0; c < input_len_2; c++)
763 neu2[c] = 0;
764 for (c = 0; c < input_len_2; c++)
765 neu2e[c] = 0;
766 next_random = next_random * (unsigned long long) 25214903917 + 11;
767 b = next_random % window;
768 if (type == 0) { //train the cbow architecture
769 // in -> hidden
770 cw = 0;
771 for (a = b; a < window * 2 + 1 - b; a++)
772 if (a != window) {
773 c = sentence_position - window + a;
774 if (c < 0)
775 continue;
776 if (c >= sentence_length)
777 continue;
778 last_word = sen[c];
779 if (last_word == -1)
780 continue;
781 for (c = 0; c < layer1_size; c++)
782 neu1[c] += syn0[c + last_word * layer1_size];
783 cw++;
784 }
785 if (cw) {
786 for (c = 0; c < layer1_size; c++)
787 neu1[c] /= cw;
788 if (hs)
789 for (d = 0; d < vocab[word].codelen; d++) {
790 f = 0;
791 l2 = vocab[word].point[d] * layer1_size;
792 // Propagate hidden -> output
793 for (c = 0; c < layer1_size; c++)
794 f += neu1[c] * syn1[c + l2];
795 if (f <= -MAX_EXP)
796 continue;
797 else if (f >= MAX_EXP)
798 continue;
799 else
800 f = expTable[(int) ((f + MAX_EXP)
801 * (EXP_TABLE_SIZE / MAX_EXP / 2))];
802 // 'g' is the gradient multiplied by the learning rate
803 g = (1 - vocab[word].code[d] - f) * alpha;
804 // Propagate errors output -> hidden
805 for (c = 0; c < layer1_size; c++)
806 neu1e[c] += g * syn1[c + l2];
807 // Learn weights hidden -> output
808 for (c = 0; c < layer1_size; c++)
809 syn1[c + l2] += g * neu1[c];
810 if (cap == 1)
811 for (c = 0; c < layer1_size; c++)
812 capParam(syn1, c + l2);
813 }
814 // NEGATIVE SAMPLING
815 if (negative > 0)
816 for (d = 0; d < negative + 1; d++) {
817 if (d == 0) {
818 target = word;
819 label = 1;
820 } else {
821 next_random = next_random
822 * (unsigned long long) 25214903917 + 11;
823 if (word_to_group != NULL
824 && word_to_group[word] != -1) {
825 target = word;
826 while (target == word) {
827 target = group_to_table[word_to_group[word]
828 * table_size
829 + (next_random >> 16) % table_size];
830 next_random = next_random
831 * (unsigned long long) 25214903917
832 + 11;
833 }
834 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
835 } else {
836 target =
837 table[(next_random >> 16) % table_size];
838 }
839 if (target == 0)
840 target = next_random % (vocab_size - 1) + 1;
841 if (target == word)
842 continue;
843 label = 0;
844 }
845 l2 = target * layer1_size;
846 f = 0;
847 for (c = 0; c < layer1_size; c++)
848 f += neu1[c] * syn1neg[c + l2];
849 if (f > MAX_EXP)
850 g = (label - 1) * alpha;
851 else if (f < -MAX_EXP)
852 g = (label - 0) * alpha;
853 else
854 g = (label
855 - expTable[(int) ((f + MAX_EXP)
856 * (EXP_TABLE_SIZE / MAX_EXP / 2))])
857 * alpha;
858 for (c = 0; c < layer1_size; c++)
859 neu1e[c] += g * syn1neg[c + l2];
860 for (c = 0; c < layer1_size; c++)
861 syn1neg[c + l2] += g * neu1[c];
862 if (cap == 1)
863 for (c = 0; c < layer1_size; c++)
864 capParam(syn1neg, c + l2);
865 }
866 // Noise Contrastive Estimation
867 if (nce > 0)
868 for (d = 0; d < nce + 1; d++) {
869 if (d == 0) {
870 target = word;
871 label = 1;
872 } else {
873 next_random = next_random
874 * (unsigned long long) 25214903917 + 11;
875 if (word_to_group != NULL
876 && word_to_group[word] != -1) {
877 target = word;
878 while (target == word) {
879 target = group_to_table[word_to_group[word]
880 * table_size
881 + (next_random >> 16) % table_size];
882 next_random = next_random
883 * (unsigned long long) 25214903917
884 + 11;
885 }
886 } else {
887 target =
888 table[(next_random >> 16) % table_size];
889 }
890 if (target == 0)
891 target = next_random % (vocab_size - 1) + 1;
892 if (target == word)
893 continue;
894 label = 0;
895 }
896 l2 = target * layer1_size;
897 f = 0;
898
899 for (c = 0; c < layer1_size; c++)
900 f += neu1[c] * syn1nce[c + l2];
901 if (f > MAX_EXP)
902 g = (label - 1) * alpha;
903 else if (f < -MAX_EXP)
904 g = (label - 0) * alpha;
905 else {
906 f = exp(f);
907 g =
908 (label
909 - f
910 / (noise_distribution[target]
911 * nce + f)) * alpha;
912 }
913 for (c = 0; c < layer1_size; c++)
914 neu1e[c] += g * syn1nce[c + l2];
915 for (c = 0; c < layer1_size; c++)
916 syn1nce[c + l2] += g * neu1[c];
917 if (cap == 1)
918 for (c = 0; c < layer1_size; c++)
919 capParam(syn1nce, c + l2);
920 }
921 // hidden -> in
922 for (a = b; a < window * 2 + 1 - b; a++)
923 if (a != window) {
924 c = sentence_position - window + a;
925 if (c < 0)
926 continue;
927 if (c >= sentence_length)
928 continue;
929 last_word = sen[c];
930 if (last_word == -1)
931 continue;
932 for (c = 0; c < layer1_size; c++)
933 syn0[c + last_word * layer1_size] += neu1e[c];
934 }
935 }
936 } else if (type == 1) { //train skip-gram
937 for (a = b; a < window * 2 + 1 - b; a++)
938 if (a != window) {
939 c = sentence_position - window + a;
940 if (c < 0)
941 continue;
942 if (c >= sentence_length)
943 continue;
944 last_word = sen[c];
945 if (last_word == -1)
946 continue;
947 l1 = last_word * layer1_size;
948 for (c = 0; c < layer1_size; c++)
949 neu1e[c] = 0;
950 // HIERARCHICAL SOFTMAX
951 if (hs)
952 for (d = 0; d < vocab[word].codelen; d++) {
953 f = 0;
954 l2 = vocab[word].point[d] * layer1_size;
955 // Propagate hidden -> output
956 for (c = 0; c < layer1_size; c++)
957 f += syn0[c + l1] * syn1[c + l2];
958 if (f <= -MAX_EXP)
959 continue;
960 else if (f >= MAX_EXP)
961 continue;
962 else
963 f = expTable[(int) ((f + MAX_EXP)
964 * (EXP_TABLE_SIZE / MAX_EXP / 2))];
965 // 'g' is the gradient multiplied by the learning rate
966 g = (1 - vocab[word].code[d] - f) * alpha;
967 // Propagate errors output -> hidden
968 for (c = 0; c < layer1_size; c++)
969 neu1e[c] += g * syn1[c + l2];
970 // Learn weights hidden -> output
971 for (c = 0; c < layer1_size; c++)
972 syn1[c + l2] += g * syn0[c + l1];
973 if (cap == 1)
974 for (c = 0; c < layer1_size; c++)
975 capParam(syn1, c + l2);
976 }
977 // NEGATIVE SAMPLING
978 if (negative > 0)
979 for (d = 0; d < negative + 1; d++) {
980 if (d == 0) {
981 target = word;
982 label = 1;
983 } else {
984 next_random = next_random
985 * (unsigned long long) 25214903917 + 11;
986 if (word_to_group != NULL
987 && word_to_group[word] != -1) {
988 target = word;
989 while (target == word) {
990 target =
991 group_to_table[word_to_group[word]
992 * table_size
993 + (next_random >> 16)
994 % table_size];
995 next_random =
996 next_random
997 * (unsigned long long) 25214903917
998 + 11;
999 }
1000 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1001 } else {
1002 target = table[(next_random >> 16)
1003 % table_size];
1004 }
1005 if (target == 0)
1006 target = next_random % (vocab_size - 1) + 1;
1007 if (target == word)
1008 continue;
1009 label = 0;
1010 }
1011 l2 = target * layer1_size;
1012 f = 0;
1013 for (c = 0; c < layer1_size; c++)
1014 f += syn0[c + l1] * syn1neg[c + l2];
1015 if (f > MAX_EXP)
1016 g = (label - 1) * alpha;
1017 else if (f < -MAX_EXP)
1018 g = (label - 0) * alpha;
1019 else
1020 g =
1021 (label
1022 - expTable[(int) ((f + MAX_EXP)
1023 * (EXP_TABLE_SIZE
1024 / MAX_EXP / 2))])
1025 * alpha;
1026 for (c = 0; c < layer1_size; c++)
1027 neu1e[c] += g * syn1neg[c + l2];
1028 for (c = 0; c < layer1_size; c++)
1029 syn1neg[c + l2] += g * syn0[c + l1];
1030 if (cap == 1)
1031 for (c = 0; c < layer1_size; c++)
1032 capParam(syn1neg, c + l2);
1033 }
1034 //Noise Contrastive Estimation
1035 if (nce > 0)
1036 for (d = 0; d < nce + 1; d++) {
1037 if (d == 0) {
1038 target = word;
1039 label = 1;
1040 } else {
1041 next_random = next_random
1042 * (unsigned long long) 25214903917 + 11;
1043 if (word_to_group != NULL
1044 && word_to_group[word] != -1) {
1045 target = word;
1046 while (target == word) {
1047 target =
1048 group_to_table[word_to_group[word]
1049 * table_size
1050 + (next_random >> 16)
1051 % table_size];
1052 next_random =
1053 next_random
1054 * (unsigned long long) 25214903917
1055 + 11;
1056 }
1057 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1058 } else {
1059 target = table[(next_random >> 16)
1060 % table_size];
1061 }
1062 if (target == 0)
1063 target = next_random % (vocab_size - 1) + 1;
1064 if (target == word)
1065 continue;
1066 label = 0;
1067 }
1068 l2 = target * layer1_size;
1069 f = 0;
1070 for (c = 0; c < layer1_size; c++)
1071 f += syn0[c + l1] * syn1nce[c + l2];
1072 if (f > MAX_EXP)
1073 g = (label - 1) * alpha;
1074 else if (f < -MAX_EXP)
1075 g = (label - 0) * alpha;
1076 else {
1077 f = exp(f);
1078 g = (label
1079 - f
1080 / (noise_distribution[target]
1081 * nce + f)) * alpha;
1082 }
1083 for (c = 0; c < layer1_size; c++)
1084 neu1e[c] += g * syn1nce[c + l2];
1085 for (c = 0; c < layer1_size; c++)
1086 syn1nce[c + l2] += g * syn0[c + l1];
1087 if (cap == 1)
1088 for (c = 0; c < layer1_size; c++)
1089 capParam(syn1nce, c + l2);
1090 }
1091 // Learn weights input -> hidden
1092 for (c = 0; c < layer1_size; c++)
1093 syn0[c + l1] += neu1e[c];
1094 }
1095 } else if (type == 2) { //train the cwindow architecture
1096 // in -> hidden
1097 cw = 0;
1098 for (a = 0; a < window * 2 + 1; a++)
1099 if (a != window) {
1100 c = sentence_position - window + a;
1101 if (c < 0)
1102 continue;
1103 if (c >= sentence_length)
1104 continue;
1105 last_word = sen[c];
1106 if (last_word == -1)
1107 continue;
1108 window_offset = a * layer1_size;
1109 if (a > window)
1110 window_offset -= layer1_size;
1111 for (c = 0; c < layer1_size; c++)
1112 neu1[c + window_offset] += syn0[c
1113 + last_word * layer1_size];
1114 cw++;
1115 }
1116 if (cw) {
1117 if (hs)
1118 for (d = 0; d < vocab[word].codelen; d++) {
1119 f = 0;
1120 l2 = vocab[word].point[d] * window_layer_size;
1121 // Propagate hidden -> output
1122 for (c = 0; c < window_layer_size; c++)
1123 f += neu1[c] * syn1_window[c + l2];
1124 if (f <= -MAX_EXP)
1125 continue;
1126 else if (f >= MAX_EXP)
1127 continue;
1128 else
1129 f = expTable[(int) ((f + MAX_EXP)
1130 * (EXP_TABLE_SIZE / MAX_EXP / 2))];
1131 // 'g' is the gradient multiplied by the learning rate
1132 g = (1 - vocab[word].code[d] - f) * alpha;
1133 // Propagate errors output -> hidden
1134 for (c = 0; c < window_layer_size; c++)
1135 neu1e[c] += g * syn1_window[c + l2];
1136 // Learn weights hidden -> output
1137 for (c = 0; c < window_layer_size; c++)
1138 syn1_window[c + l2] += g * neu1[c];
1139 if (cap == 1)
1140 for (c = 0; c < window_layer_size; c++)
1141 capParam(syn1_window, c + l2);
1142 }
1143 // NEGATIVE SAMPLING
1144 if (negative > 0)
1145 for (d = 0; d < negative + 1; d++) {
1146 if (d == 0) {
1147 target = word;
1148 label = 1;
1149 } else {
1150 next_random = next_random
1151 * (unsigned long long) 25214903917 + 11;
1152 if (word_to_group != NULL
1153 && word_to_group[word] != -1) {
1154 target = word;
1155 while (target == word) {
1156 target = group_to_table[word_to_group[word]
1157 * table_size
1158 + (next_random >> 16) % table_size];
1159 next_random = next_random
1160 * (unsigned long long) 25214903917
1161 + 11;
1162 }
1163 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1164 } else {
1165 target =
1166 table[(next_random >> 16) % table_size];
1167 }
1168 if (target == 0)
1169 target = next_random % (vocab_size - 1) + 1;
1170 if (target == word)
1171 continue;
1172 label = 0;
1173 }
1174 l2 = target * window_layer_size;
1175 f = 0;
1176 for (c = 0; c < window_layer_size; c++)
1177 f += neu1[c] * syn1neg_window[c + l2];
1178 if (f > MAX_EXP)
1179 g = (label - 1) * alpha;
1180 else if (f < -MAX_EXP)
1181 g = (label - 0) * alpha;
1182 else
1183 g = (label
1184 - expTable[(int) ((f + MAX_EXP)
1185 * (EXP_TABLE_SIZE / MAX_EXP / 2))])
1186 * alpha;
1187 for (c = 0; c < window_layer_size; c++)
1188 neu1e[c] += g * syn1neg_window[c + l2];
1189 for (c = 0; c < window_layer_size; c++)
1190 syn1neg_window[c + l2] += g * neu1[c];
1191 if (cap == 1)
1192 for (c = 0; c < window_layer_size; c++)
1193 capParam(syn1neg_window, c + l2);
1194 }
1195 // Noise Contrastive Estimation
1196 if (nce > 0)
1197 for (d = 0; d < nce + 1; d++) {
1198 if (d == 0) {
1199 target = word;
1200 label = 1;
1201 } else {
1202 next_random = next_random
1203 * (unsigned long long) 25214903917 + 11;
1204 if (word_to_group != NULL
1205 && word_to_group[word] != -1) {
1206 target = word;
1207 while (target == word) {
1208 target = group_to_table[word_to_group[word]
1209 * table_size
1210 + (next_random >> 16) % table_size];
1211 next_random = next_random
1212 * (unsigned long long) 25214903917
1213 + 11;
1214 }
1215 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1216 } else {
1217 target =
1218 table[(next_random >> 16) % table_size];
1219 }
1220 if (target == 0)
1221 target = next_random % (vocab_size - 1) + 1;
1222 if (target == word)
1223 continue;
1224 label = 0;
1225 }
1226 l2 = target * window_layer_size;
1227 f = 0;
1228 for (c = 0; c < window_layer_size; c++)
1229 f += neu1[c] * syn1nce_window[c + l2];
1230 if (f > MAX_EXP)
1231 g = (label - 1) * alpha;
1232 else if (f < -MAX_EXP)
1233 g = (label - 0) * alpha;
1234 else {
1235 f = exp(f);
1236 g =
1237 (label
1238 - f
1239 / (noise_distribution[target]
1240 * nce + f)) * alpha;
1241 }
1242 for (c = 0; c < window_layer_size; c++)
1243 neu1e[c] += g * syn1nce_window[c + l2];
1244 for (c = 0; c < window_layer_size; c++)
1245 syn1nce_window[c + l2] += g * neu1[c];
1246 if (cap == 1)
1247 for (c = 0; c < window_layer_size; c++)
1248 capParam(syn1nce_window, c + l2);
1249 }
1250 // hidden -> in
1251 for (a = 0; a < window * 2 + 1; a++)
1252 if (a != window) {
1253 c = sentence_position - window + a;
1254 if (c < 0)
1255 continue;
1256 if (c >= sentence_length)
1257 continue;
1258 last_word = sen[c];
1259 if (last_word == -1)
1260 continue;
1261 window_offset = a * layer1_size;
1262 if (a > window)
1263 window_offset -= layer1_size;
1264 for (c = 0; c < layer1_size; c++)
1265 syn0[c + last_word * layer1_size] += neu1e[c
1266 + window_offset];
1267 }
1268 }
1269 } else if (type == 3) { //train structured skip-gram
1270 for (a = 0; a < window * 2 + 1; a++)
1271 if (a != window) {
1272 c = sentence_position - window + a;
1273 if (c < 0)
1274 continue;
1275 if (c >= sentence_length)
1276 continue;
1277 last_word = sen[c];
1278 if (last_word == -1)
1279 continue;
1280 l1 = last_word * layer1_size;
1281 window_offset = a * layer1_size;
1282 if (a > window)
1283 window_offset -= layer1_size;
1284 for (c = 0; c < layer1_size; c++)
1285 neu1e[c] = 0;
1286 // HIERARCHICAL SOFTMAX
1287 if (hs)
1288 for (d = 0; d < vocab[word].codelen; d++) {
1289 f = 0;
1290 l2 = vocab[word].point[d] * window_layer_size;
1291 // Propagate hidden -> output
1292 for (c = 0; c < layer1_size; c++)
1293 f += syn0[c + l1]
1294 * syn1_window[c + l2 + window_offset];
1295 if (f <= -MAX_EXP)
1296 continue;
1297 else if (f >= MAX_EXP)
1298 continue;
1299 else
1300 f = expTable[(int) ((f + MAX_EXP)
1301 * (EXP_TABLE_SIZE / MAX_EXP / 2))];
1302 // 'g' is the gradient multiplied by the learning rate
1303 g = (1 - vocab[word].code[d] - f) * alpha;
1304 // Propagate errors output -> hidden
1305 for (c = 0; c < layer1_size; c++)
1306 neu1e[c] += g
1307 * syn1_window[c + l2 + window_offset];
1308 // Learn weights hidden -> output
1309 for (c = 0; c < layer1_size; c++)
1310 syn1[c + l2 + window_offset] += g
1311 * syn0[c + l1];
1312 if (cap == 1)
1313 for (c = 0; c < layer1_size; c++)
1314 capParam(syn1, c + l2 + window_offset);
1315 }
1316 // NEGATIVE SAMPLING
1317 if (negative > 0)
1318 for (d = 0; d < negative + 1; d++) {
1319 if (d == 0) {
1320 target = word;
1321 label = 1;
1322 } else {
1323 next_random = next_random
1324 * (unsigned long long) 25214903917 + 11;
1325 if (word_to_group != NULL
1326 && word_to_group[word] != -1) {
1327 target = word;
1328 while (target == word) {
1329 target =
1330 group_to_table[word_to_group[word]
1331 * table_size
1332 + (next_random >> 16)
1333 % table_size];
1334 next_random =
1335 next_random
1336 * (unsigned long long) 25214903917
1337 + 11;
1338 }
1339 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1340 } else {
1341 target = table[(next_random >> 16)
1342 % table_size];
1343 }
1344 if (target == 0)
1345 target = next_random % (vocab_size - 1) + 1;
1346 if (target == word)
1347 continue;
1348 label = 0;
1349 }
1350 l2 = target * window_layer_size;
1351 f = 0;
1352 for (c = 0; c < layer1_size; c++)
1353 f +=
1354 syn0[c + l1]
1355 * syn1neg_window[c + l2
1356 + window_offset];
1357 if (f > MAX_EXP)
1358 g = (label - 1) * alpha;
1359 else if (f < -MAX_EXP)
1360 g = (label - 0) * alpha;
1361 else
1362 g =
1363 (label
1364 - expTable[(int) ((f + MAX_EXP)
1365 * (EXP_TABLE_SIZE
1366 / MAX_EXP / 2))])
1367 * alpha;
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001368 if(debug_mode > 2 && ((long long) id) == 0) {
1369 printf("negative sampling %lld for input (word) %s (#%lld), target (last word) %s returned %s (#%lld), ", d, vocab[word].word, word, vocab[last_word].word, vocab[target].word, target);
1370 printf("label %lld, a %lld, gain %.4f\n", label, a-window, g);
1371 }
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001372 for (c = 0; c < layer1_size; c++)
1373 neu1e[c] +=
1374 g
1375 * syn1neg_window[c + l2
1376 + window_offset];
1377 for (c = 0; c < layer1_size; c++)
1378 syn1neg_window[c + l2 + window_offset] += g
1379 * syn0[c + l1];
1380 if (cap == 1)
1381 for (c = 0; c < layer1_size; c++)
1382 capParam(syn1neg_window,
1383 c + l2 + window_offset);
1384 }
1385 // Noise Constrastive Estimation
1386 if (nce > 0)
1387 for (d = 0; d < nce + 1; d++) {
1388 if (d == 0) {
1389 target = word;
1390 label = 1;
1391 } else {
1392 next_random = next_random
1393 * (unsigned long long) 25214903917 + 11;
1394 if (word_to_group != NULL
1395 && word_to_group[word] != -1) {
1396 target = word;
1397 while (target == word) {
1398 target =
1399 group_to_table[word_to_group[word]
1400 * table_size
1401 + (next_random >> 16)
1402 % table_size];
1403 next_random =
1404 next_random
1405 * (unsigned long long) 25214903917
1406 + 11;
1407 }
1408 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1409 } else {
1410 target = table[(next_random >> 16)
1411 % table_size];
1412 }
1413 if (target == 0)
1414 target = next_random % (vocab_size - 1) + 1;
1415 if (target == word)
1416 continue;
1417 label = 0;
1418 }
1419 l2 = target * window_layer_size;
1420 f = 0;
1421 for (c = 0; c < layer1_size; c++)
1422 f +=
1423 syn0[c + l1]
1424 * syn1nce_window[c + l2
1425 + window_offset];
1426 if (f > MAX_EXP)
1427 g = (label - 1) * alpha;
1428 else if (f < -MAX_EXP)
1429 g = (label - 0) * alpha;
1430 else {
1431 f = exp(f);
1432 g = (label
1433 - f
1434 / (noise_distribution[target]
1435 * nce + f)) * alpha;
1436 }
1437 for (c = 0; c < layer1_size; c++)
1438 neu1e[c] +=
1439 g
1440 * syn1nce_window[c + l2
1441 + window_offset];
1442 for (c = 0; c < layer1_size; c++)
1443 syn1nce_window[c + l2 + window_offset] += g
1444 * syn0[c + l1];
1445 if (cap == 1)
1446 for (c = 0; c < layer1_size; c++)
1447 capParam(syn1nce_window,
1448 c + l2 + window_offset);
1449 }
1450 // Learn weights input -> hidden
1451 for (c = 0; c < layer1_size; c++) {
1452 syn0[c + l1] += neu1e[c];
1453 if (syn0[c + l1] > 50)
1454 syn0[c + l1] = 50;
1455 if (syn0[c + l1] < -50)
1456 syn0[c + l1] = -50;
1457 }
1458 }
1459 } else if (type == 4) { //training senna
1460 // in -> hidden
1461 cw = 0;
1462 for (a = 0; a < window * 2 + 1; a++)
1463 if (a != window) {
1464 c = sentence_position - window + a;
1465 if (c < 0)
1466 continue;
1467 if (c >= sentence_length)
1468 continue;
1469 last_word = sen[c];
1470 if (last_word == -1)
1471 continue;
1472 window_offset = a * layer1_size;
1473 if (a > window)
1474 window_offset -= layer1_size;
1475 for (c = 0; c < layer1_size; c++)
1476 neu1[c + window_offset] += syn0[c
1477 + last_word * layer1_size];
1478 cw++;
1479 }
1480 if (cw) {
1481 for (a = 0; a < window_hidden_size; a++) {
1482 c = a * window_layer_size;
1483 for (b = 0; b < window_layer_size; b++) {
1484 neu2[a] += syn_window_hidden[c + b] * neu1[b];
1485 }
1486 }
1487 if (hs)
1488 for (d = 0; d < vocab[word].codelen; d++) {
1489 f = 0;
1490 l2 = vocab[word].point[d] * window_hidden_size;
1491 // Propagate hidden -> output
1492 for (c = 0; c < window_hidden_size; c++)
1493 f += hardTanh(neu2[c]) * syn_hidden_word[c + l2];
1494 if (f <= -MAX_EXP)
1495 continue;
1496 else if (f >= MAX_EXP)
1497 continue;
1498 else
1499 f = expTable[(int) ((f + MAX_EXP)
1500 * (EXP_TABLE_SIZE / MAX_EXP / 2))];
1501 // 'g' is the gradient multiplied by the learning rate
1502 g = (1 - vocab[word].code[d] - f) * alpha;
1503 // Propagate errors output -> hidden
1504 for (c = 0; c < window_hidden_size; c++)
1505 neu2e[c] += dHardTanh(neu2[c], g) * g
1506 * syn_hidden_word[c + l2];
1507 // Learn weights hidden -> output
1508 for (c = 0; c < window_hidden_size; c++)
1509 syn_hidden_word[c + l2] += dHardTanh(neu2[c], g) * g
1510 * neu2[c];
1511 }
1512 // NEGATIVE SAMPLING
1513 if (negative > 0)
1514 for (d = 0; d < negative + 1; d++) {
1515 if (d == 0) {
1516 target = word;
1517 label = 1;
1518 } else {
1519 next_random = next_random
1520 * (unsigned long long) 25214903917 + 11;
1521 if (word_to_group != NULL
1522 && word_to_group[word] != -1) {
1523 target = word;
1524 while (target == word) {
1525 target = group_to_table[word_to_group[word]
1526 * table_size
1527 + (next_random >> 16) % table_size];
1528 next_random = next_random
1529 * (unsigned long long) 25214903917
1530 + 11;
1531 }
1532 //printf("negative sampling %lld for word %s returned %s\n", d, vocab[word].word, vocab[target].word);
1533 } else {
1534 target =
1535 table[(next_random >> 16) % table_size];
1536 }
1537 if (target == 0)
1538 target = next_random % (vocab_size - 1) + 1;
1539 if (target == word)
1540 continue;
1541 label = 0;
1542 }
1543 l2 = target * window_hidden_size;
1544 f = 0;
1545 for (c = 0; c < window_hidden_size; c++)
1546 f += hardTanh(neu2[c])
1547 * syn_hidden_word_neg[c + l2];
1548 if (f > MAX_EXP)
1549 g = (label - 1) * alpha / negative;
1550 else if (f < -MAX_EXP)
1551 g = (label - 0) * alpha / negative;
1552 else
1553 g = (label
1554 - expTable[(int) ((f + MAX_EXP)
1555 * (EXP_TABLE_SIZE / MAX_EXP / 2))])
1556 * alpha / negative;
1557 for (c = 0; c < window_hidden_size; c++)
1558 neu2e[c] += dHardTanh(neu2[c], g) * g
1559 * syn_hidden_word_neg[c + l2];
1560 for (c = 0; c < window_hidden_size; c++)
1561 syn_hidden_word_neg[c + l2] += dHardTanh(neu2[c], g)
1562 * g * neu2[c];
1563 }
1564 for (a = 0; a < window_hidden_size; a++)
1565 for (b = 0; b < window_layer_size; b++)
1566 neu1e[b] += neu2e[a]
1567 * syn_window_hidden[a * window_layer_size + b];
1568 for (a = 0; a < window_hidden_size; a++)
1569 for (b = 0; b < window_layer_size; b++)
1570 syn_window_hidden[a * window_layer_size + b] += neu2e[a]
1571 * neu1[b];
1572 // hidden -> in
1573 for (a = 0; a < window * 2 + 1; a++)
1574 if (a != window) {
1575 c = sentence_position - window + a;
1576 if (c < 0)
1577 continue;
1578 if (c >= sentence_length)
1579 continue;
1580 last_word = sen[c];
1581 if (last_word == -1)
1582 continue;
1583 window_offset = a * layer1_size;
1584 if (a > window)
1585 window_offset -= layer1_size;
1586 for (c = 0; c < layer1_size; c++)
1587 syn0[c + last_word * layer1_size] += neu1e[c
1588 + window_offset];
1589 }
1590 }
1591 } else {
1592 printf("unknown type %i", type);
1593 exit(0);
1594 }
1595 sentence_position++;
1596 if (sentence_position >= sentence_length) {
1597 sentence_length = 0;
1598 continue;
1599 }
1600 }
1601 fclose(fi);
1602 free(neu1);
1603 free(neu1e);
1604 pthread_exit(NULL);
1605}
1606
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001607void ShowCollocations() {
Marc Kupietz71996e72016-03-18 13:40:24 +01001608 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001609 real f, max_f, maxmax_f;
Marc Kupietz71996e72016-03-18 13:40:24 +01001610 real *target_sums, bestf[MAX_CC], worstbest;
1611 long besti[MAX_CC];
1612 int N = 10;
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001613 a = posix_memalign((void **) &target_sums, 128, vocab_size * sizeof(real));
1614
1615 for (d = cc; d < vocab_size; d++) {
1616 for (b = 0; b < vocab_size; b++)
1617 target_sums[b]=0;
Marc Kupietz71996e72016-03-18 13:40:24 +01001618 for (b = 0; b < N; b++)
1619 bestf[b]=-1;
1620 worstbest = -1;
1621
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001622 maxmax_f = -1;
1623 maxmax_target = 0;
Marc Kupietz0a664c12016-03-18 13:18:22 +01001624 for (a = window * 2 + 1; a >=0; a--) {
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001625 if (a != window) {
1626 max_f = -1;
1627 window_offset = a * layer1_size;
1628 if (a > window)
1629 window_offset -= layer1_size;
1630 for(target = 0; target < vocab_size; target ++) {
1631 if(target == d)
1632 continue;
1633 f = 0;
1634 for (c = 0; c < layer1_size; c++)
1635 f += syn0[d* layer1_size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
1636 if (f < -MAX_EXP)
1637 continue;
1638 else if (f > MAX_EXP)
1639 continue;
1640 else
1641 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
1642 if(f > max_f) {
1643 max_f = f;
1644 max_target = target;
1645 }
Marc Kupietz0fb5d612016-03-18 11:01:21 +01001646 target_sums[target] += (1-target_sums[target]) * f;
Marc Kupietz71996e72016-03-18 13:40:24 +01001647 if(f > worstbest) {
1648 for (b = 0; b < N; b++) {
1649 if (f > bestf[b]) {
1650 for (e = N - 1; e > b; e--) {
1651 bestf[e] = bestf[e - 1];
1652 besti[e] = besti[e - 1];
1653 }
1654 bestf[b] = f;
1655 besti[b] = target;
1656 break;
1657 }
1658 }
1659 worstbest = bestf[N-1];
1660 }
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001661 }
1662 printf("%s (%.2f) ", vocab[max_target].word, max_f);
1663 if(max_f > maxmax_f) {
1664 maxmax_f = max_f;
1665 maxmax_target = max_target;
1666 }
1667 } else {
1668 printf("\x1b[1m%s\x1b[0m ", vocab[d].word);
1669 }
1670 }
1671 max_f = -1;
1672 for (b = 0; b < vocab_size; b++) {
1673 if(target_sums[b] > max_f) {
1674 max_f = target_sums[b];
1675 max_target = b;
1676 }
1677 }
1678 printf(" – max sum: %s (%.2f), max resp.: \x1b[1m%s\x1b[0m (%.2f)\n",
Marc Kupietz0fb5d612016-03-18 11:01:21 +01001679 vocab[max_target].word, max_f,
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001680 vocab[maxmax_target].word, maxmax_f);
Marc Kupietz71996e72016-03-18 13:40:24 +01001681 for(b=0; b<N && bestf[b]>-1; b++)
1682 printf("%-32s %.2f\n", vocab[besti[b]].word, bestf[b]);
1683 printf("\n");
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001684 }
1685}
1686
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001687void TrainModel() {
1688 long a, b, c, d;
1689 FILE *fo;
1690 pthread_t *pt = (pthread_t *) malloc(num_threads * sizeof(pthread_t));
1691 printf("Starting training using file %s\n", train_file);
1692 starting_alpha = alpha;
1693 if (read_vocab_file[0] != 0)
1694 ReadVocab();
1695 else
1696 LearnVocabFromTrainFile();
1697 if (save_vocab_file[0] != 0)
1698 SaveVocab();
1699 if (output_file[0] == 0)
1700 return;
1701 InitNet();
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001702 if(cc > 0)
1703 ShowCollocations();
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001704 if (negative > 0 || nce > 0)
1705 InitUnigramTable();
1706 if (negative_classes_file[0] != 0)
1707 InitClassUnigramTable();
1708 start = clock();
1709 for (a = 0; a < num_threads; a++)
1710 pthread_create(&pt[a], NULL, TrainModelThread, (void *) a);
1711 for (a = 0; a < num_threads; a++)
1712 pthread_join(pt[a], NULL);
1713 fo = fopen(output_file, "wb");
1714 if (classes == 0) {
1715 // Save the word vectors
1716 fprintf(fo, "%lld %lld\n", vocab_size, layer1_size);
1717 for (a = 0; a < vocab_size; a++) {
1718 fprintf(fo, "%s ", vocab[a].word);
1719 if (binary)
1720 for (b = 0; b < layer1_size; b++)
1721 fwrite(&syn0[a * layer1_size + b], sizeof(real), 1, fo);
1722 else
1723 for (b = 0; b < layer1_size; b++)
1724 fprintf(fo, "%lf ", syn0[a * layer1_size + b]);
1725 fprintf(fo, "\n");
1726 }
1727 } else {
1728 // Run K-means on the word vectors
1729 int clcn = classes, iter = 10, closeid;
1730 int *centcn = (int *) malloc(classes * sizeof(int));
1731 int *cl = (int *) calloc(vocab_size, sizeof(int));
1732 real closev, x;
1733 real *cent = (real *) calloc(classes * layer1_size, sizeof(real));
1734 for (a = 0; a < vocab_size; a++)
1735 cl[a] = a % clcn;
1736 for (a = 0; a < iter; a++) {
1737 for (b = 0; b < clcn * layer1_size; b++)
1738 cent[b] = 0;
1739 for (b = 0; b < clcn; b++)
1740 centcn[b] = 1;
1741 for (c = 0; c < vocab_size; c++) {
1742 for (d = 0; d < layer1_size; d++)
1743 cent[layer1_size * cl[c] + d] += syn0[c * layer1_size + d];
1744 centcn[cl[c]]++;
1745 }
1746 for (b = 0; b < clcn; b++) {
1747 closev = 0;
1748 for (c = 0; c < layer1_size; c++) {
1749 cent[layer1_size * b + c] /= centcn[b];
1750 closev += cent[layer1_size * b + c]
1751 * cent[layer1_size * b + c];
1752 }
1753 closev = sqrt(closev);
1754 for (c = 0; c < layer1_size; c++)
1755 cent[layer1_size * b + c] /= closev;
1756 }
1757 for (c = 0; c < vocab_size; c++) {
1758 closev = -10;
1759 closeid = 0;
1760 for (d = 0; d < clcn; d++) {
1761 x = 0;
1762 for (b = 0; b < layer1_size; b++)
1763 x += cent[layer1_size * d + b]
1764 * syn0[c * layer1_size + b];
1765 if (x > closev) {
1766 closev = x;
1767 closeid = d;
1768 }
1769 }
1770 cl[c] = closeid;
1771 }
1772 }
1773 // Save the K-means classes
1774 for (a = 0; a < vocab_size; a++)
1775 fprintf(fo, "%s %d\n", vocab[a].word, cl[a]);
1776 free(centcn);
1777 free(cent);
1778 free(cl);
1779 }
1780 fclose(fo);
1781 if (save_net_file[0] != 0)
1782 SaveNet();
1783}
1784
1785int ArgPos(char *str, int argc, char **argv) {
1786 int a;
1787 for (a = 1; a < argc; a++)
1788 if (!strcmp(str, argv[a])) {
1789 if (a == argc - 1) {
1790 printf("Argument missing for %s\n", str);
1791 exit(1);
1792 }
1793 return a;
1794 }
1795 return -1;
1796}
1797
1798int main(int argc, char **argv) {
1799 int i;
1800 if (argc == 1) {
1801 printf("WORD VECTOR estimation toolkit v 0.1c\n\n");
1802 printf("Options:\n");
1803 printf("Parameters for training:\n");
1804 printf("\t-train <file>\n");
1805 printf("\t\tUse text data from <file> to train the model\n");
1806 printf("\t-output <file>\n");
1807 printf(
1808 "\t\tUse <file> to save the resulting word vectors / word clusters\n");
1809 printf("\t-size <int>\n");
1810 printf("\t\tSet size of word vectors; default is 100\n");
1811 printf("\t-window <int>\n");
1812 printf("\t\tSet max skip length between words; default is 5\n");
1813 printf("\t-sample <float>\n");
1814 printf(
1815 "\t\tSet threshold for occurrence of words. Those that appear with higher frequency in the training data\n");
1816 printf(
1817 "\t\twill be randomly down-sampled; default is 1e-3, useful range is (0, 1e-5)\n");
1818 printf("\t-hs <int>\n");
1819 printf("\t\tUse Hierarchical Softmax; default is 0 (not used)\n");
1820 printf("\t-negative <int>\n");
1821 printf(
1822 "\t\tNumber of negative examples; default is 5, common values are 3 - 10 (0 = not used)\n");
1823 printf("\t-negative-classes <file>\n");
1824 printf("\t\tNegative classes to sample from\n");
1825 printf("\t-nce <int>\n");
1826 printf(
1827 "\t\tNumber of negative examples for nce; default is 0, common values are 3 - 10 (0 = not used)\n");
1828 printf("\t-threads <int>\n");
1829 printf("\t\tUse <int> threads (default 12)\n");
1830 printf("\t-iter <int>\n");
1831 printf("\t\tRun more training iterations (default 5)\n");
1832 printf("\t-min-count <int>\n");
1833 printf(
1834 "\t\tThis will discard words that appear less than <int> times; default is 5\n");
1835 printf("\t-alpha <float>\n");
1836 printf(
1837 "\t\tSet the starting learning rate; default is 0.025 for skip-gram and 0.05 for CBOW\n");
1838 printf("\t-classes <int>\n");
1839 printf(
1840 "\t\tOutput word classes rather than word vectors; default number of classes is 0 (vectors are written)\n");
1841 printf("\t-debug <int>\n");
1842 printf(
1843 "\t\tSet the debug mode (default = 2 = more info during training)\n");
1844 printf("\t-binary <int>\n");
1845 printf(
1846 "\t\tSave the resulting vectors in binary moded; default is 0 (off)\n");
1847 printf("\t-save-vocab <file>\n");
1848 printf("\t\tThe vocabulary will be saved to <file>\n");
1849 printf("\t-read-vocab <file>\n");
1850 printf(
1851 "\t\tThe vocabulary will be read from <file>, not constructed from the training data\n");
1852 printf("\t-read-net <file>\n");
1853 printf(
1854 "\t\tThe net parameters will be read from <file>, not initialized randomly\n");
1855 printf("\t-save-net <file>\n");
1856 printf("\t\tThe net parameters will be saved to <file>\n");
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001857 printf("\t-show-cc <int>\n");
1858 printf("\t\tShow words with their collocators starting from word rank <int>. Depends on -read-vocab and -read-net.\n");
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001859 printf("\t-type <int>\n");
1860 printf(
1861 "\t\tType of embeddings (0 for cbow, 1 for skipngram, 2 for cwindow, 3 for structured skipngram, 4 for senna type)\n");
1862 printf("\t-cap <int>\n");
1863 printf(
1864 "\t\tlimit the parameter values to the range [-50, 50]; default is 0 (off)\n");
1865 printf("\nExamples:\n");
1866 printf(
1867 "./word2vec -train data.txt -output vec.txt -size 200 -window 5 -sample 1e-4 -negative 5 -hs 0 -binary 0 -type 1 -iter 3\n\n");
1868 return 0;
1869 }
1870 output_file[0] = 0;
1871 save_vocab_file[0] = 0;
1872 read_vocab_file[0] = 0;
1873 save_net_file[0] = 0;
1874 read_net_file[0] = 0;
1875 negative_classes_file[0] = 0;
1876 if ((i = ArgPos((char *) "-size", argc, argv)) > 0)
1877 layer1_size = atoi(argv[i + 1]);
1878 if ((i = ArgPos((char *) "-train", argc, argv)) > 0)
1879 strcpy(train_file, argv[i + 1]);
1880 if ((i = ArgPos((char *) "-save-vocab", argc, argv)) > 0)
1881 strcpy(save_vocab_file, argv[i + 1]);
1882 if ((i = ArgPos((char *) "-read-vocab", argc, argv)) > 0)
1883 strcpy(read_vocab_file, argv[i + 1]);
1884 if ((i = ArgPos((char *) "-save-net", argc, argv)) > 0)
1885 strcpy(save_net_file, argv[i + 1]);
1886 if ((i = ArgPos((char *) "-read-net", argc, argv)) > 0)
1887 strcpy(read_net_file, argv[i + 1]);
1888 if ((i = ArgPos((char *) "-debug", argc, argv)) > 0)
1889 debug_mode = atoi(argv[i + 1]);
1890 if ((i = ArgPos((char *) "-binary", argc, argv)) > 0)
1891 binary = atoi(argv[i + 1]);
Marc Kupietz6b1f2ba2016-03-17 21:17:42 +01001892 if ((i = ArgPos((char *) "-show-cc", argc, argv)) > 0)
1893 cc = atoi(argv[i + 1]);
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001894 if ((i = ArgPos((char *) "-type", argc, argv)) > 0)
1895 type = atoi(argv[i + 1]);
1896 if ((i = ArgPos((char *) "-output", argc, argv)) > 0)
1897 strcpy(output_file, argv[i + 1]);
1898 if ((i = ArgPos((char *) "-window", argc, argv)) > 0)
1899 window = atoi(argv[i + 1]);
1900 if ((i = ArgPos((char *) "-sample", argc, argv)) > 0)
1901 sample = atof(argv[i + 1]);
1902 if ((i = ArgPos((char *) "-hs", argc, argv)) > 0)
1903 hs = atoi(argv[i + 1]);
1904 if ((i = ArgPos((char *) "-negative", argc, argv)) > 0)
1905 negative = atoi(argv[i + 1]);
1906 if ((i = ArgPos((char *) "-negative-classes", argc, argv)) > 0)
1907 strcpy(negative_classes_file, argv[i + 1]);
1908 if ((i = ArgPos((char *) "-nce", argc, argv)) > 0)
1909 nce = atoi(argv[i + 1]);
1910 if ((i = ArgPos((char *) "-threads", argc, argv)) > 0)
1911 num_threads = atoi(argv[i + 1]);
1912 if ((i = ArgPos((char *) "-iter", argc, argv)) > 0)
1913 iter = atoi(argv[i + 1]);
1914 if ((i = ArgPos((char *) "-min-count", argc, argv)) > 0)
1915 min_count = atoi(argv[i + 1]);
1916 if ((i = ArgPos((char *) "-classes", argc, argv)) > 0)
1917 classes = atoi(argv[i + 1]);
1918 if ((i = ArgPos((char *) "-cap", argc, argv)) > 0)
1919 cap = atoi(argv[i + 1]);
1920 if (type == 0 || type == 2 || type == 4)
1921 alpha = 0.05;
1922 if ((i = ArgPos((char *) "-alpha", argc, argv)) > 0)
1923 alpha = atof(argv[i + 1]);
1924 vocab = (struct vocab_word *) calloc(vocab_max_size,
1925 sizeof(struct vocab_word));
1926 vocab_hash = (int *) calloc(vocab_hash_size, sizeof(int));
1927 expTable = (real *) malloc((EXP_TABLE_SIZE + 1) * sizeof(real));
1928 for (i = 0; i < EXP_TABLE_SIZE; i++) {
1929 expTable[i] = exp((i / (real) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
1930 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
1931 }
1932 TrainModel();
1933 return 0;
1934}
1935