-
Notifications
You must be signed in to change notification settings - Fork 2
/
word2vec_multiclass.c
1880 lines (1712 loc) · 51.5 KB
/
word2vec_multiclass.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.
/*
*Word alpha ---ok
*Word alpha + global alpha ---ok
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <assert.h>
#define MAX_STRING 1000
#define EXP_TABLE_SIZE 1000
#define MAX_EXP 6
#define MAX_SENTENCE_LENGTH 1000
#define MAX_CODE_LENGTH 40
const int vocab_hash_size = 30000000; // Maximum 30 * 0.7 = 21M words in the vocabulary
typedef float real; // Precision of float numbers
/**
* cn:
* word:
* code:
* codelen:
*/
struct vocab_word {
long long cn;
long long actual_read;
long long id_old;
real alpha;
int *point;
char *word, *code, codelen;
};
char train_file[MAX_STRING], output_file[MAX_STRING],
model_output_file[MAX_STRING], model_input_file[MAX_STRING],output_file_other[MAX_STRING],vec_input_file[MAX_STRING];
char save_vocab_file[MAX_STRING], read_vocab_file[MAX_STRING];
struct vocab_word *vocab,*vocab_new;
int save_binary = 0,read_binary = 0,cbow = 0, debug_mode = 2, window = 5, min_count = 5,
num_threads = 1, min_reduce = 1;
int *vocab_hash;
long long vocab_max_size = 1000, vocab_size = 0, layer1_size = 600,
vocab_size_old = 0;
long long train_words = 0,train_words_old=0, word_count_actual = 0, file_size = 0, classes = 0;
real alpha = 0.025, starting_alpha, sample = 0;
real *syn0, *syn1, *syn1neg, *expTable, *syn1_old,*syn1neg_old, *syn0_old;
clock_t start;
int *word_to_group;
int *group_to_table;
int class_number;
char negative_classes_file[MAX_STRING];
int hs = 1, negative = 0, update = 0;
const int table_size = 1e8;
//int table_size = 1e8;
const long long max_w = 50;
int *table;
long long *words_count;
int iterations=1;
void InitUnigramTable() {
int a, i;
long long train_words_pow = 0;
real d1, power = 0.75;
//printf("%lld %d\n",train_words_pow,vocab[0].cn);
table = (int *) malloc(table_size * sizeof(int));
if (table == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
for (a = 0; a < vocab_size; a++){
train_words_pow += pow(vocab[a].cn, power);
//printf("%lld\n",train_words_pow);
}
i = 0;
d1 = pow(vocab[i].cn, power) / (real) train_words_pow;
for (a = 0; a < table_size; a++) {
table[a] = i;
if (a / (real) table_size > d1) {
i++;//指向下一个word
d1 += pow(vocab[i].cn, power) / (real) train_words_pow;
}
if (i >= vocab_size)
i = vocab_size - 1;
}
}
/*
void InitUnigramTable_test() {
int a, i;
long long train_words_pow = 0;
real d1, power = 0.75;
//modification
table_size=vocab_size;
table = (int *) malloc(table_size * sizeof(int));
for (a = 0; a < table_size; a++) {
table[a] = a;
}
}
*/
void InitClassUnigramTable() {
long long a,c;
printf("loading class unigrams \n");
FILE *fin = fopen(negative_classes_file, "rb");
if (fin == NULL) {
printf("ERROR: class file not found!\n");
exit(1);
}
word_to_group = (int *)malloc(vocab_size * sizeof(int));
// memset(word_to_group,0,sizeof(int) * vocab_size);
for(a = 0; a < vocab_size; a++) word_to_group[a] = -1;
char class[MAX_STRING];
char prev_class[MAX_STRING];
prev_class[0] = 0;
char word[MAX_STRING];
class_number = -1;
while (1) {
if (feof(fin)) break;
ReadWord(class, fin);
ReadWord(word, fin);
int word_index = SearchVocab(word);
if (word_index != -1){
if(strcmp(class, prev_class) != 0){
class_number++;
strcpy(prev_class, class);
}
word_to_group[word_index] = class_number;
}
ReadWord(word, fin);
}
class_number++;
fclose(fin);
group_to_table = (int *)malloc(table_size * class_number * sizeof(int));//sampling a word in the group as same as current word
long long train_words_pow = 0;
real d1, power = 0.75;
for(c = 0; c < class_number; c++){
long long offset = c * table_size;
train_words_pow = 0;
for (a = 0; a < vocab_size; a++)
if(word_to_group[a] == c)
train_words_pow += pow(vocab[a].cn, power);
int i = 0;
while(word_to_group[i] != c && i < vocab_size)
i++;
d1 = pow(vocab[i].cn, power) / (real)train_words_pow;
for (a = 0; a < table_size; a++) {
group_to_table[offset + a] = i;
if (a / (real)table_size > d1) {
i++;
while(word_to_group[i] != c && i < vocab_size)
i++;
d1 += pow(vocab[i].cn, power) / (real)train_words_pow;
}
if (i >= vocab_size)
while(word_to_group[i] != c && i >= 0)
i--;
}
}
}
// Reads a single word from a file, assuming space + tab + EOL to be word boundaries
void ReadWord(char *word, FILE *fin) {
int a = 0, ch;
while (!feof(fin)) {
ch = fgetc(fin);
if (ch == 13)
continue;
if ((ch == ' ') || (ch == '\t') || (ch == '\n')) {
if (a > 0) {
if (ch == '\n')
ungetc(ch, fin);
break;
}
if (ch == '\n') {
strcpy(word, (char *) "</s>");
return;
} else
continue;
}
word[a] = ch;
a++;
if (a >= MAX_STRING - 1)
a--; // Truncate too long words
}
word[a] = 0;
}
// Returns hash value of a word
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;
}
// Returns position of a word in the vocabulary; if the word is not found, returns -1
int SearchVocab(char *word) {
unsigned int hash = GetWordHash(word);
while (1) {
if (vocab_hash[hash] == -1)
return -1;
if (!strcmp(word, vocab[vocab_hash[hash]].word))
return vocab_hash[hash];
hash = (hash + 1) % vocab_hash_size;
}
return -1;
}
// Reads a word and returns its index in the vocabulary
int ReadWordIndex(FILE *fin) {
char word[MAX_STRING];
ReadWord(word, fin);
//printf("%s ",word);
if (feof(fin))
return -1;
return SearchVocab(word);
}
//int count1=0;
// Adds a word to the vocabulary
int AddWordToVocab(char *word) {
unsigned int hash, length = strlen(word) + 1;
if (length > MAX_STRING)
length = MAX_STRING;
vocab[vocab_size].word = (char *) calloc(length, sizeof(char));
strcpy(vocab[vocab_size].word, word);
// vocab[vocab_size].cn = 1;
// vocab[vocab_size].actual_read=0;
// vocab[vocab_size].alpha=alpha;
//++count1;
//printf("%lld %lld %f\n",vocab[vocab_size].cn,vocab[vocab_size].actual_read,vocab[vocab_size].alpha);
//vocab[vocab_size].id_old = vocab_size; //瀹氫箟璇ord鍦ㄤ箣鍓峷ocab閲岀殑涓嬫爣
//printf("%s %lld\n",vocab[vocab_size].word,vocab[vocab_size].id_old);
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));
vocab_new = (struct vocab_word *) realloc(vocab,
vocab_max_size * sizeof(struct vocab_word));
if(!vocab_new){
printf("Realloc vocab mem failed!!\n");
exit(1);
}
vocab=vocab_new;
}
hash = GetWordHash(word);
while (vocab_hash[hash] != -1)
hash = (hash + 1) % vocab_hash_size;
vocab_hash[hash] = vocab_size - 1;
return vocab_size - 1;
}
// Used later for sorting by word counts
int VocabCompare(const void *a, const void *b) {
return ((struct vocab_word *) b)->cn - ((struct vocab_word *) a)->cn;
}
// Sorts the vocabulary by frequency using word counts
void SortVocab() {
int a, size;
unsigned int hash;
// Sort the vocabulary(order by vocab's count desc) and keep </s> at the first position
qsort(&vocab[1], vocab_size - 1, sizeof(struct vocab_word), VocabCompare);
/*
for(a = 0 ; a < vocab_size ; ++a){
printf("%lld %s %lld %lld\n",a,vocab[a].word,vocab[a].cn,vocab[a].actual_read);
}
exit(-1);
*/
for (a = 0; a < vocab_hash_size; a++)
vocab_hash[a] = -1;
size = vocab_size; //
train_words = 0;
long k=0;
for (a = 0; a < size; a++) {
// Words occuring less than min_count times will be discarded from the vocab
if (vocab[a].cn < min_count) {
printf("Reduce vocab\n");
vocab_size--;
free(vocab[vocab_size].word); //free
} 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; //
//vocab[a].id_old=k;
//k++;
train_words += vocab[a].cn;//count the sum words of prev training file and current file
}
}
train_words -= train_words_old;
vocab = (struct vocab_word *) realloc(vocab,
(vocab_size + 1) * sizeof(struct vocab_word));
if(!vocab){
printf("Reallocate Failed!!\n");\
exit(1);
}
/*
for(a = 0 ; a < vocab_size ; ++a){
printf("%lld %s %lld %lld\n",a,vocab[a].word,vocab[a].cn,vocab[a].actual_read);
}
exit(-1);
*/
// Allocate memory for the binary tree construction
printf("Vocab Size from:%lld to %lld,max size:%lld\n",vocab_size_old,vocab_size,vocab_max_size);
real factor=0;
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));
factor = 1-vocab[a].actual_read/(real)(vocab[a].cn+1);
if(factor <= 0){
vocab[a].alpha=alpha;
}else{
vocab[a].alpha=alpha*factor;
}
//vocab[a].alpha = alpha * (1-vocab[a].actual_read/(real)(vocab[a].cn+1));
//printf("%lld %s %lld %lld\n",a,vocab[a].word,vocab[a].cn,vocab[a].actual_read);
//printf("%lld %s %f %lld %lld\n",a,vocab[a].word,factor,vocab[a].cn,vocab[a].actual_read);
}
//exit(-1);
}
// 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;
vocab[b].alpha = vocab[a].alpha;
vocab[b].actual_read = vocab[a].actual_read;
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++;
}
// Create binary Huffman tree using the word counts
// Frequent words will have short uniqe binary codes
//ocab_size * 2 + 1
//ocab_size * 2 + 1
//count:
void CreateBinaryTree() {
long long a, b, i, min1i, min2i, pos1, pos2, point[MAX_CODE_LENGTH];
char code[MAX_CODE_LENGTH];
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));
//
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 = vocab_size - 1;
pos2 = vocab_size;
// Following algorithm constructs the Huffman tree by adding one node at a time
//printf("Test1!!\n");
for (a = 0; a < vocab_size - 1; a++) {
// First, find two smallest nodes 'min1, min2'
if (pos1 >= 0) {
if (count[pos1] < count[pos2]) {
min1i = pos1;
pos1--;
} else {
min1i = pos2;
pos2++;
}
} else {
min1i = pos2;
pos2++;
}
if (pos1 >= 0) {
if (count[pos1] < count[pos2]) {
min2i = pos1;
pos1--;
} else {
min2i = pos2;
pos2++;
}
} else {
min2i = pos2;
pos2++;
}
count[vocab_size + a] = count[min1i] + count[min2i];
parent_node[min1i] = vocab_size + a; //瀹氫箟鐖惰妭鐐�
parent_node[min2i] = vocab_size + a; //瀹氫箟鐖惰妭鐐�
binary[min2i] = 1;
}
//printf("Test2!!\n");
//printf("Vocab Size:%lld!!\n",vocab_size);
// Now assign binary code to each vocabulary word
for (a = 0; a < vocab_size; a++) {
b = a;
i = 0;
//code[i]
//exit(-1);
//
while (1) {
//printf("%lld %lld",binary[b],b);
code[i] = binary[b];//binary[b] is the code of the bth node,b is the index of the path from leaf node to tree node
point[i] = b;//b is the index of its parent
i++;
b = parent_node[b];
if (b == vocab_size * 2 - 2)//root node of the binary tree
break;
}
//codelen
vocab[a].codelen = i;
//
vocab[a].point[0] = vocab_size - 2;
for (b = 0; b < i; b++) {//the code array of the vocab[a] if from tree node to leaf node
vocab[a].code[i - b - 1] = code[b];//vocab[a].code[0] is root
vocab[a].point[i - b] = point[b] - vocab_size;
}
}
// printf("Test3!!\n");
// exit(-1);
free(count);
free(binary);
free(parent_node);
}
void ReadVocab_Update() {
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\t%lld%c", &vocab[a].cn,&vocab[a].id_old,&c);
vocab[a].actual_read=vocab[a].cn;
//vocab[a].alpha=alpha * (1-vocab[a].actual_read/(real)(vocab[a].cn+1));
train_words_old += vocab[a].cn;
//printf("%s %lld %lld%c",word,vocab[a].cn,vocab[a].id_old,c);
//printf("%s ==> %lld\n", vocab[a].word, vocab[a].cn);
i++;
}
//train_words = vocab_size;
vocab_size_old = vocab_size;
if (debug_mode > 0) {
printf("Read Prev Vocab File !!!\n");
printf("Prev Vocab size: %lld\n", vocab_size_old);
printf("Words in prev train file: %lld\n", train_words_old);
}
//file_size = ftell(fin);
fclose(fin);
}
void LearnVocabFromTrainFile_Update() {
char word[MAX_STRING];
FILE *fin;
long long a, i;
fin = fopen(train_file, "rb");
if (fin == NULL) {
printf("ERROR: training data file not found!\n");
exit(1);
}
//train_words += vocab_size;
train_words = 0;
int new_words=0;
while (1) {
ReadWord(word, fin);
if (feof(fin))
break;
train_words++;
if ((debug_mode > 1) && (train_words % 100000 == 0)) {
printf("%lldK%c", train_words / 1000, 13);
fflush(stdout);
}
i = SearchVocab(word);
if (i == -1) {
a = AddWordToVocab(word);
//printf("%s----------------------%lld\n",word,a);
vocab[a].cn = 1;
vocab[a].id_old=a;
vocab[a].actual_read=0;
//vocab[a].alpha=alpha;
//vocab[a].alpha=alpha * (1-vocab[a].actual_read/(real)(vocab[a].cn+1));
++new_words;
} else
vocab[i].cn++;
if (vocab_size > vocab_hash_size * 0.7)
ReduceVocab();
}
printf("Vocab Size from:%lld to %lld,max size:%lld\n",vocab_size_old,vocab_size,vocab_max_size);
printf("New Added words: %d\n",new_words);
/*
for(a = 0 ; a < vocab_size ; ++a){
printf("%lld %s %lld %lld\n",a,vocab[a].word,vocab[a].cn,vocab[a].actual_read);
}
*/
//exit(-1);
//printf("######################\m");
SortVocab();
if (debug_mode > 0) {
printf("Read Training File !!!\n");
printf("Vocab size: %lld\n", vocab_size);
printf("Words in train file: %lld\n", train_words);
}
file_size = ftell(fin);
fclose(fin);
}
void LearnVocabFromTrainFile() {
char word[MAX_STRING];
FILE *fin;
long long a, i;
for (a = 0; a < vocab_hash_size; a++) vocab_hash[a] = -1;
fin = fopen(train_file, "rb");
if (fin == NULL) {
printf("ERROR: training data file not found!\n");
exit(1);
}
vocab_size = 0;
a=AddWordToVocab((char *)"</s>");
vocab[a].id_old=0;
while (1) {
ReadWord(word, fin);
if (feof(fin)) break;
train_words++;
if ((debug_mode > 1) && (train_words % 100000 == 0)) {
printf("%lldK%c", train_words / 1000, 13);
fflush(stdout);
}
i = SearchVocab(word);
if (i == -1) {
// printf("%s\n",word);
a = AddWordToVocab(word);
//printf("%s %lld %lld %lld\n",word,vocab_size,vocab_max_size,a);
vocab[a].id_old = a;
//vocab[a].cn = 1;
} else vocab[i].cn++;
if (vocab_size > vocab_hash_size * 0.7) ReduceVocab();
}
SortVocab();
if (debug_mode > 0) {
printf("Vocab size: %lld\n", vocab_size);
printf("Words in train file: %lld\n", train_words);
}
// printf("Counts : %d\n",count1);
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 %lld %lld %f\n", vocab[i].word, vocab[i].id_old,vocab[i].cn,
// vocab[i].id_old,vocab[i].alpha);
fprintf(fo, "%s %lld %lld\n", vocab[i].word, vocab[i].cn,
vocab[i].id_old);
//printf("%f\n",vocab[i].alpha);
}
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);
}
void InitNet() {
long long a, b;
words_count = (long long *) malloc(vocab_size * sizeof(long long));
for(a = 0 ; a < vocab_size ; ++a){
words_count[a]=vocab[a].cn;
}
a = posix_memalign((void **) &syn0, 128,
(long long) vocab_size * layer1_size * sizeof(real));
if (a) {
printf("Memory allocation failed\n");
exit(1);
}
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 (b = 0; b < layer1_size; b++)
for (a = 0; a < vocab_size; a++)
syn1[a * layer1_size + b] = 0;
}
if (negative > 0) {
a = posix_memalign((void **) &syn1neg, 128,
(long long) vocab_size * layer1_size * sizeof(real));
if (syn1neg == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
for (b = 0; b < layer1_size; b++)
for (a = 0; a < vocab_size; a++)
syn1neg[a * layer1_size + b] = 0;
}
for (b = 0; b < layer1_size; b++)
for (a = 0; a < vocab_size; a++)
{
syn0[a * layer1_size + b] = (rand() / (real) RAND_MAX - 0.5)
/ layer1_size;
}
/*
for(a = 0 ; a < vocab_size ; ++a){
vocab[a].alpha=(1-vocab[a].actual_read/(real)(vocab[a].cn+1));
}
*/
// CreateBinaryTree();
printf("Complete CreateBinaryTree!!\n");
}
void InitVec(){
int read_lines = 0;
int read_counts = 0;
char ch;
long long a, b,c;
long long words, size;
// char* vocab_word_point1 = (char *) malloc((long long) max_w * sizeof(char));
char vocab_word_point1[max_w];
// memset(vocab_word_point1,0,max_w * sizeof(char));
FILE *fin=fopen(vec_input_file,"rb");
if (fin == NULL) {
printf("Model file not found\n");
exit(1);
}
printf("%lld %lld %lld\n",vocab_size_old,vocab_size,layer1_size);
// syn0_old= (real*) malloc((long long) vocab_size_old * layer1_size * sizeof(real));
// syn0=(real*) malloc((long long) vocab_size * layer1_size * sizeof(real));
a = posix_memalign((void **)&syn0_old, 128, (long long)vocab_size_old * layer1_size * sizeof(real));
a = posix_memalign((void **)&syn0, 128, (long long)vocab_size * layer1_size * sizeof(real));
if(syn0_old == NULL){
printf("Memory allocation failed\n");
exit(1);
}
if(syn0 == NULL){
printf("Memory allocation failed\n");
exit(1);
}
fscanf(fin, "%lld", &words);
fscanf(fin, "%lld", &size);
printf("%lld %lld\n",words,size);
assert(vocab_size_old == words);
assert(layer1_size == size);
// layer1_size=size;
if (read_binary) {
for (a = 0; a < vocab_size_old; a++) {
//fscanf(fin, "%s%c", vocab_word_point, &ch);
//printf("%s ",vocab_word_point);
//unsigned long long next_random = 1;
c=0;
while (1) {
vocab_word_point1[c] = fgetc(fin);
if (feof(fin) || (vocab_word_point1[c] == ' '))
break;
if ((c < max_w) && (vocab_word_point1[c] != '\n'))
c++;
}
vocab_word_point1[c]='\0';
//string word=string(vocab_word_point1);
memset(vocab_word_point1,0,max_w);
for (b = 0; b < layer1_size; b++) {
fread(&syn0_old[a * layer1_size + b], sizeof(real), 1, fin);
//printf("%f ",syn0_old[a * layer1_size + b]);
++read_counts;
}
//ch = fgetc(fin);
//printf("\n");
++read_lines;
}
} else {
for (a = 0; a < vocab_size_old; a++) {
//fscanf(fin, "%s%c", vocab_word_point1, &ch);
c=0;
while (1) {
vocab_word_point1[c] = fgetc(fin);
if (feof(fin) || (vocab_word_point1[c] == ' '))
break;
if ((c < max_w) && (vocab_word_point1[c] != '\n'))
c++;
}
vocab_word_point1[c]='\0';
//string word=string(vocab_word_point1);
memset(vocab_word_point1,0,max_w);
for (b = 0; b < layer1_size; b++) {
fscanf(fin, "%f", &syn0_old[a * layer1_size + b]);
read_counts++;
}
ch = fgetc(fin);//
++read_lines;
}
}
for (a = 0; a < vocab_size; a++) {
long long id = vocab[a].id_old;
if (id < vocab_size_old) {
for (b = 0; b < layer1_size; b++) {
syn0[a * layer1_size + b] = syn0_old[id * layer1_size + b];
//syn0[a * layer1_size + b] = (rand() / (real) RAND_MAX - 0.5) / layer1_size;
//read_counts++;
}
//++read_lines;
} else {
for (b = 0; b < layer1_size; b++)
{
syn0[a * layer1_size + b] = (rand() / (real) RAND_MAX - 0.5) / layer1_size;
//read_counts++;
}
//++read_lines;
}
}
printf("Read %d Lines of Vec File!!\n", read_lines);
printf("Read %d Counts of Vec File!!\n", read_counts);
fflush(fin);
fclose(fin);
//free(syn0_old);
//free(vocab_word_point);
}
void InitNegativeSamplingVec(){
FILE *fin = fopen(model_input_file, "rb");
unsigned long long next_random = 1;
int read_lines = 0;
int read_counts = 0;
char ch;
long long a, b;
long long words, size;
//char* vocab_word_point1 = (char *) malloc((long long) max_w * sizeof(char));
char vocab_word_point1[max_w];
if (fin == NULL) {
printf("Model file not found\n");
exit(1);
}
//syn1neg_old= (real*) malloc((long long) vocab_size_old * layer1_size * sizeof(real));
//syn1neg=(real*) malloc((long long) vocab_size * layer1_size * sizeof(real));
a = posix_memalign((void **)&syn1neg_old, 128, (long long)vocab_size_old * layer1_size * sizeof(real));
a = posix_memalign((void **)&syn1neg, 128, (long long)vocab_size * layer1_size * sizeof(real));
if(syn1neg_old == NULL){
printf("Memory allocation failed\n");
exit(1);
}
if(syn1neg == NULL){
printf("Memory allocation failed\n");
exit(1);
}
fscanf(fin, "%lld", &words);
fscanf(fin, "%lld", &size);
printf("%lld %lld\n",words,size);
assert(vocab_size_old == words);
assert(layer1_size == size);
//exit(-1);
if (read_binary) {
for (a = 0; a < vocab_size_old; a++) {
fscanf(fin, "%s%c", vocab_word_point1, &ch);
//printf("%s ",vocab_word_point);
for (b = 0; b < layer1_size; b++) {
fread(&syn1neg_old[a * layer1_size + b], sizeof(real), 1,
fin);
//printf("%f ",syn1neg_old[a * layer1_size + b]);
++read_counts;
}
//ch = fgetc(fin);
//printf("\n");
++read_lines;
}
} else {
long c=0;
for (a = 0; a < vocab_size_old; a++) {
//fscanf(fin, "%s%c", vocab_word_point1, &ch);
c=0;
while (1) {
vocab_word_point1[c] = fgetc(fin);
if (feof(fin) || (vocab_word_point1[c] == ' '))
break;
if ((c < max_w) && (vocab_word_point1[c] != '\n'))
c++;
}
vocab_word_point1[c]='\0';
//string word=string(vocab_word_point1);
memset(vocab_word_point1,0,max_w);
//printf("%s\n",vocab_word_point1);
for (b = 0; b < layer1_size; b++) {
fscanf(fin, "%f", &syn1neg_old[a * layer1_size + b]);
read_counts++;
}
ch = fgetc(fin);
++read_lines;
}
}
for (a = 0; a < vocab_size; a++) {
long long id = vocab[a].id_old;
if (id < vocab_size_old) {
for (b = 0; b < layer1_size; b++) {
syn1neg[a * layer1_size + b] = syn1neg_old[id * layer1_size + b];
//read_counts++;
}
//++read_lines;
} else {
for (b = 0; b < layer1_size; b++) {
//syn1neg[a * layer1_size + b] = (rand() / (real) RAND_MAX - 0.5) / layer1_size;
syn1neg[a * layer1_size + b] = 0;
//read_counts++;
}
//++read_lines;
}
}
//exit(-1);
//free(syn1neg_old);
//free(vocab_word_point);
fflush(fin);
fclose(fin);
printf("Read %d Lines of Model File!!\n", read_lines);
printf("Read %d Counts of Model File!!\n", read_counts);
}
void InitNetOnline() {
InitVec();
// exit(-1);
InitNegativeSamplingVec();
//exit(-1);
/*
if(hs){
a = posix_memalign((void **) &syn1_old, 128,
(long long) vocab_size_old * layer1_size * sizeof(real));
a = posix_memalign((void **) &syn1, 128,
(long long) vocab_size * layer1_size * sizeof(real));
if (syn1_old == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
fscanf(fin, "%lld", &words);
fscanf(fin, "%lld", &size);
printf("%lld %lld\n",words,size);
vocab_size_old = words;
layer1_size=size;
if (binary) {
for (a = 0; a < vocab_size_old; a++) {
fscanf(fin, "%s%c", vocab_word_point, &ch);
for (b = 0; b < layer1_size; b++) {
fread(&syn1_old[a * layer1_size + b], sizeof(real), 1,
fin);
++read_counts;
}
ch = fgetc(fin);
++read_lines;
}
for (a = 0; a < vocab_size_old; a++) {
fscanf(fin, "%s%c", vocab_word_point, &ch);
for (b = 0; b < layer1_size; b++) {
fread(&syn0_old[a * layer1_size + b], sizeof(real), 1, fin);
++read_counts;
}
ch = fgetc(fin);
++read_lines;
}