Repository navigation
Expand file tree
/
Copy pathEncoder.cs
More file actions
900 lines (753 loc) · 28.9 KB
/
Copy pathEncoder.cs
File metadata and controls
900 lines (753 loc) · 28.9 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
//using AdvUtils;
namespace Txt2Vec
{
public enum WORD_SOURCE
{
CORPUS,
PRETRAINED_MODEL
}
public class vocab_word
{
public string word;
public int cnt;
public WORD_SOURCE source;
}
public class Encoder
{
const int EXP_TABLE_SIZE = 1000;
const int MAX_EXP = 6;
const int MAX_CODE_LENGTH = 40;
StreamReader srTrainCorpus = null;
Dictionary<string, int> word2id;
List<vocab_word> vocab;
int vocab_size = 0;
long train_words = 0;
long word_count_actual = 0;
long next_save_step = 10000000;
long next_save_trained_words = 10000000;
long sentence_count = 0;
public long iter = 5;
public int layer1_size = 200;
public double starting_alpha = 0.025;
public double sample = 0;
public int min_count = 5;
public int num_threads = 1;
public int cbow = 1, window = 5;
public int classes = 1;
public int debug_mode = 0;
public long savestep = 100000000;
public int negative = 5;
public string strPreTrainedModelFileName = null;
public int onlyUpdateCorpusWord = 0;
double[] syn0;
double[] syn1;
double[] totalNeu_e;
double[] expTable;
object[] syn0Locker;
object[] syn1Locker;
Random rand = new Random(DateTime.Today.Millisecond);
public int[] accFreqTable;
public int accTotalFreq = 0;
public int accFactor = 1;
void InitAccTermFreq()
{
//Logger.WriteLine("Initializing acculumate term frequency...");
accFreqTable = new int[vocab_size];
accTotalFreq = 0;
//Keep accTotalFreq is less than int.MaxValue
accFactor = 1 + (int)(train_words / int.MaxValue);
//Logger.WriteLine("Acculumate factor: {0}", accFactor);
int i = 0;
foreach (vocab_word word in vocab)
{
accTotalFreq += (word.cnt / accFactor);
accFreqTable[i] = accTotalFreq;
i++;
}
//Logger.WriteLine("Acculumated total frequency : {0}", accTotalFreq);
}
int SearchAccTermTable(int freq)
{
int mid = vocab_size >> 1;
int left = 0, right = vocab_size - 1;
while (true)
{
if (accFreqTable[mid] < freq)
{
left = mid + 1;
}
else if (accFreqTable[mid] > freq)
{
if (mid == 0)
{
return 0;
}
if (accFreqTable[mid - 1] < freq)
{
return mid;
}
right = mid - 1;
}
else
{
return mid;
}
mid = (left + right) >> 1;
}
}
public Encoder()
{
word2id = new Dictionary<string, int>();
vocab = new List<vocab_word>();
expTable = new double[EXP_TABLE_SIZE + 1];
for (int i = 0; i < EXP_TABLE_SIZE; i++)
{
expTable[i] = Math.Exp((i / (double)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
}
}
// Returns position of a word in the vocabulary; if the word is not found, returns -1
int SearchVocab(string word)
{
if (word2id.ContainsKey(word) == false)
{
return -1;
}
return word2id[word];
}
public class VocabComparer : IComparer<vocab_word>
{
public int Compare(vocab_word x, vocab_word y)
{
return y.cnt.CompareTo(x.cnt);
}
}
// Shrink the vocabulary by frequency using word counts
void ShrinkVocab()
{
// Sort the vocabulary
vocab.Sort(new VocabComparer());
word2id.Clear();
int size = vocab_size;
train_words = 0;
for (int a = 0; a < size; a++)
{
// Words occuring less than min_count times will be discarded from the vocab
if (vocab[a].cnt < min_count)
{
vocab_size--;
vocab[a].word = null;
}
else
{
word2id.Add(vocab[a].word, a);
train_words += vocab[a].cnt;
}
}
vocab.RemoveRange(vocab_size, vocab.Count - vocab_size);
}
void LoadPreTrainModelSyn(string strModelFileName, double[] syn)
{
Model preTrainedModel = new Model();
preTrainedModel.LoadModel(strModelFileName, false);
if (preTrainedModel.VectorSize != layer1_size)
{
throw new Exception("The layer size is inconsistent between given parameter and pre-trained model.");
}
string[] allTerms = preTrainedModel.GetAllTerms();
foreach (string strTerm in allTerms)
{
int wordId = SearchVocab(strTerm);
if (wordId < 0)
{
//Ingore the dropped term
continue;
}
float[] vector = preTrainedModel.GetVector(strTerm);
for (int i = 0; i < layer1_size;i++)
{
syn[i + wordId * layer1_size] = vector[i];
}
}
}
void LoadVocabFromPreTrainModel(string strModelFileName)
{
Model preTrainedModel = new Model();
preTrainedModel.LoadModel(strModelFileName, false);
layer1_size = preTrainedModel.VectorSize;
//Logger.WriteLine("Apply the following options from pr-trained model file {0}", preTrainedModel);
//Logger.WriteLine("Vector Size: {0}", layer1_size);
string[] allTerms = preTrainedModel.GetAllTerms();
foreach (string strTerm in allTerms)
{
//Add terms in pre-trained model into vocabulary
//If the term is already added from corpus or given dictionary, we ignore it
if (word2id.ContainsKey(strTerm) == false)
{
Term term = preTrainedModel.GetTerm(strTerm);
vocab_word word = new vocab_word();
word.word = strTerm;
word.cnt = 0;
word.source = WORD_SOURCE.PRETRAINED_MODEL;
word2id.Add(word.word, vocab_size);
vocab.Add(word);
vocab_size++;
}
}
}
public void LoadVocabFromFile(string vocab_file)
{
StreamReader sr = new StreamReader(vocab_file);
string strLine = null;
word2id = new Dictionary<string, int>();
vocab = new List<vocab_word>();
vocab_size = 0;
while ((strLine = sr.ReadLine()) != null)
{
string[] items = strLine.Split('\t');
vocab_word word = new vocab_word();
word.word = items[0];
word.source = WORD_SOURCE.CORPUS;
word2id.Add(word.word, vocab_size);
vocab.Add(word);
vocab_size++;
}
sr.Close();
}
private void GetTrainWordSize(string train_file)
{
StreamReader fin = new StreamReader(train_file);
string strLine = null;
train_words = 0;
foreach (vocab_word vw in vocab)
{
vw.cnt = 0;
}
while ((strLine = fin.ReadLine()) != null)
{
//Append the end of sentence
strLine = strLine.Trim();
string[] items = strLine.Split();
foreach (string item in items)
{
int wordId = SearchVocab(item);
if (wordId >= 0)
{
vocab[wordId].cnt++;
if (vocab[wordId].source == WORD_SOURCE.PRETRAINED_MODEL && onlyUpdateCorpusWord == 1)
{
continue;
}
train_words++;
if (debug_mode > 0 && train_words % 1000000 == 0)
{
//Logger.WriteLine("{0}M... ", train_words / 1000000);
}
}
}
}
fin.Close();
}
public void LearnVocabFromTrainFile(string train_file)
{
StreamReader fin = new StreamReader(train_file);
string strLine = null;
vocab_size = 0;
int i = 0;
while ((strLine = fin.ReadLine()) != null)
{
//Append the end of sentence
strLine = strLine.Trim();
string[] items = strLine.Split();
foreach (string word in items)
{
//This term is normal word
train_words++;
if (debug_mode > 0 && train_words % 1000000 == 0)
{
//Logger.WriteLine("{0}M... ", train_words / 1000000);
}
i = SearchVocab(word);
if (i == -1)
{
word2id.Add(word, vocab_size);
vocab_word voc_word = new vocab_word();
voc_word.word = word;
voc_word.cnt = 1;
voc_word.source = WORD_SOURCE.CORPUS;
vocab.Add(voc_word);
vocab_size++;
}
else
{
vocab[i].cnt++;
}
}
}
fin.Close();
}
public void SaveVocab(string save_vocab_file)
{
StreamWriter fo = new StreamWriter(save_vocab_file);
for (int i = 0; i < vocab_size; i++)
{
fo.WriteLine("{0}\t{1}", vocab[i].word, vocab[i].cnt);
}
fo.Close();
}
void InitNet()
{
syn0Locker = new object[vocab_size];
syn1Locker = new object[vocab_size];
for (int i = 0; i < vocab_size; i++)
{
syn0Locker[i] = new object();
syn1Locker[i] = new object();
}
totalNeu_e = new double[layer1_size];
syn0 = new double[vocab_size * layer1_size];
for (long b = 0; b < layer1_size; b++)
{
for (long a = 0; a < vocab_size; a++)
{
syn0[a * layer1_size + b] = (rand.NextDouble() - 0.5) / layer1_size;
}
}
syn1 = new double[vocab_size * layer1_size];
}
object rdlocker = new object();
object wrlocker = new object();
object locker_rand = new object();
int RandNext(int max)
{
lock (locker_rand)
{
return rand.Next(max);
}
}
double RandNextDouble()
{
lock (locker_rand)
{
return rand.NextDouble();
}
}
void TrainModelThread()
{
int word_count = 0, last_word_count = 0;
double alpha = starting_alpha * (1 - word_count_actual / (double)(iter * train_words + 1));
while (true)
{
if (word_count - last_word_count > 10000)
{
last_word_count = word_count;
if (debug_mode > 0)
{
double sumErr = 0;
for (int i = 0; i < layer1_size; i++)
{
sumErr += (totalNeu_e[i] / word_count_actual);
}
//Logger.WriteLine("Alpha: {0:0.0000} Prog: {1:0.00}% Words: {2}K Sent: {3}K Error: {4}", alpha,
//word_count_actual / (double)(iter * train_words + 1) * 100, word_count_actual / 1024, sentence_count / 1024, sumErr);
}
if (word_count_actual > next_save_trained_words)
{
long old_next_save_trained_words = next_save_trained_words;
lock (rdlocker)
{
if (old_next_save_trained_words == next_save_trained_words)
{
//Logger.WriteLine("Saving temporary word vector into file...");
Model.SaveModel("vector_tmp.bin", vocab_size, layer1_size, vocab, syn0);
Model.SaveModel("vector_tmp_bin.syn1", vocab_size, layer1_size, vocab, syn1);
next_save_trained_words += next_save_step;
}
}
}
}
alpha = starting_alpha * (1 - word_count_actual / (double)(iter * train_words + 1));
if (alpha < starting_alpha * 0.0001)
{
alpha = starting_alpha * 0.0001;
}
//Read a line from training corpus
string strLine = "";
lock (rdlocker)
{
strLine = srTrainCorpus.ReadLine();
}
if (strLine == null)
{
break;
}
Interlocked.Increment(ref sentence_count);
//Parse each word in current sentence
string[] strWords = strLine.Split();
bool bIgnore = true;
for (int i = 0; i < strWords.Length; i++)
{
int wordId = SearchVocab(strWords[i]);
if (wordId < 0)
{
continue;
}
if (vocab[wordId].source == WORD_SOURCE.CORPUS)
{
bIgnore = false;
break;
}
if (vocab[wordId].source == WORD_SOURCE.PRETRAINED_MODEL && onlyUpdateCorpusWord == 0)
{
bIgnore = false;
break;
}
}
if (bIgnore == true)
{
continue;
}
for (int sentence_position = 0; sentence_position < strWords.Length; sentence_position++)
{
string strPredictedWord = strWords[sentence_position];
int word = SearchVocab(strPredictedWord);
if (word < 0)
{
//Ingore the dropped term
continue;
}
if (vocab[word].source == WORD_SOURCE.CORPUS ||
(vocab[word].source == WORD_SOURCE.PRETRAINED_MODEL && onlyUpdateCorpusWord == 0))
{
word_count++;
Interlocked.Increment(ref word_count_actual);
}
int rnd_window = RandNext(window);
if (cbow != 0)
{
TrainByCBOW(sentence_position, rnd_window, strWords, alpha, word);
}
else
{
TrainBySkipGram(sentence_position, rnd_window, strWords, alpha, word);
}
}
}
}
private void TrainBySkipGram(int sentence_position, int b, string[] sen, double alpha, int word)
{
double[] neu1e = new double[layer1_size];
//train skip-gram
for (int a = b; a < window * 2 + 1 - b; a++)
{
int c = sentence_position - window + a;
if (c < 0 || c >= sen.Length || c == sentence_position)
{
//Invalidated position. out of sentence boundary
continue;
}
string strNGram = sen[c];
int wordId = SearchVocab(strNGram);
if (wordId == -1)
{
continue;
}
int l1 = wordId * layer1_size;
for (c = 0; c < layer1_size; c++)
{
neu1e[c] = 0;
}
lock (syn0Locker[wordId])
{
//Negative sampling
int target = 0;
int label = 1;
for (int d = 0; d < negative + 1; d++)
{
if (d == 0)
{
target = word;
label = 1;
}
else
{
target = SearchAccTermTable(RandNext(accTotalFreq));
if (target == word)
{
continue;
}
label = 0;
}
long l2 = target * layer1_size;
double f = 0;
double g;
lock (syn1Locker[target])
{
for (c = 0; c < layer1_size; c++)
{
f += syn0[c + l1] * syn1[c + l2];
}
if (f > MAX_EXP)
{
g = (label - 1) * alpha;
}
else if (f < -MAX_EXP)
{
g = (label - 0) * alpha;
}
else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;
for (c = 0; c < layer1_size; c++)
{
neu1e[c] += g * syn1[c + l2];
}
if (onlyUpdateCorpusWord == 1 && vocab[target].source == WORD_SOURCE.PRETRAINED_MODEL)
{
continue;
}
for (c = 0; c < layer1_size; c++)
{
syn1[c + l2] += g * syn0[c + l1];
}
}
}
if (onlyUpdateCorpusWord == 1 && vocab[wordId].source == WORD_SOURCE.PRETRAINED_MODEL)
{
continue;
}
// Learn weights input -> hidden
for (c = 0; c < layer1_size; c++)
{
syn0[c + l1] += neu1e[c];
}
}
for (int i = 0; i < neu1e.Length; i++)
{
totalNeu_e[i] += Math.Abs(neu1e[i] / (window * 2 + 1 - b * 2));
}
}
}
private void TrainByCBOW(int sentence_position, int b, string[] sen, double alpha, int word)
{
double[] neu1 = new double[layer1_size];
double[] neu1e = new double[layer1_size];
int cw = 0;
List<int> wordIdList = new List<int>();
//train the cbow architecture
// in -> hidden
for (int a = b; a < window * 2 + 1 - b; a++)
{
int c = sentence_position - window + a;
if (c < 0 || c >= sen.Length || c == sentence_position)
{
//Invalidated position. out of sentence boundary
continue;
}
//Generate ngram string and word id
string strNGram = null;
int wordId = -1;
strNGram = sen[c];
wordId = SearchVocab(strNGram);
if (wordId < 0)
{
//Ingore the dropped term
continue;
}
//The subsampling randomly discards frequent words while keeping the ranking same
if (sample > 0)
{
double ran = (Math.Sqrt(vocab[wordId].cnt / (sample * train_words)) + 1) * (sample * train_words) / vocab[wordId].cnt;
if (ran < RandNextDouble())
{
continue;
}
}
if (onlyUpdateCorpusWord == 0 || (onlyUpdateCorpusWord == 1 && vocab[wordId].source == WORD_SOURCE.CORPUS))
{
//Terms that need to update their syn0
wordIdList.Add(wordId);
}
lock (syn0Locker[wordId])
{
for (int t = 0; t < layer1_size; t++)
{
neu1[t] += syn0[t + wordId * layer1_size];
}
}
cw++;
}
if (wordIdList.Count == 0)
{
//No term need to update its syn, return
return;
}
double synUpdateFactor = (double)(cw) / (double)(wordIdList.Count);
for (int c = 0; c < layer1_size; c++)
{
neu1[c] /= cw;
}
int target = 0;
int label = 1;
for (int d = 0; d < negative + 1; d++)
{
if (d == 0)
{
target = word;
label = 1;
}
else
{
target = SearchAccTermTable(RandNext(accTotalFreq));
if (target == word)
{
continue;
}
label = 0;
}
long l2 = target * layer1_size;
double f = 0;
lock (syn1Locker[target])
{
for (int c = 0; c < layer1_size; c++)
{
f += neu1[c] * syn1[c + l2];
}
double g = 0;
if (f > MAX_EXP) g = (label - 1) * alpha;
else if (f < -MAX_EXP) g = (label - 0) * alpha;
else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;
for (int c = 0; c < layer1_size; c++)
{
neu1e[c] += g * syn1[c + l2];
}
if (onlyUpdateCorpusWord == 1 && vocab[target].source == WORD_SOURCE.PRETRAINED_MODEL)
{
continue;
}
for (int c = 0; c < layer1_size; c++)
{
syn1[c + l2] += g * neu1[c];
}
}
}
// hidden -> in
foreach (int wordId in wordIdList)
{
lock (syn0Locker[wordId])
{
for (int c = 0; c < layer1_size; c++)
{
syn0[c + wordId * layer1_size] += (neu1e[c] * synUpdateFactor);
}
}
}
for (int i = 0; i < neu1e.Length; i++)
{
totalNeu_e[i] += Math.Abs(neu1e[i]);
}
}
public void TrainModel(string train_file, string output_file, string vocab_file)
{
if (debug_mode > 0)
{
//Logger.WriteLine("Starting training using file {0}", train_file);
}
if ((vocab_file != null && File.Exists(vocab_file) == true) ||
strPreTrainedModelFileName != null)
{
if (vocab_file != null && File.Exists(vocab_file) == true)
{
//Logger.WriteLine("Loading vocabulary {0} from file...", vocab_file);
LoadVocabFromFile(vocab_file);
}
if (strPreTrainedModelFileName != null)
{
//Logger.WriteLine("Load vocabulary from pre-trained model file {0}", strPreTrainedModelFileName);
LoadVocabFromPreTrainModel(strPreTrainedModelFileName);
}
//Vocaburary is loaded from given dict, then we need to calculate how many words need to be train
//Logger.WriteLine("Calculating how many words need to be train...");
GetTrainWordSize(train_file);
//Logger.WriteLine("Total training words : {0}", train_words);
}
else
{
//We have no input vocabulary, so we get vocabulary from training corpus
//Logger.WriteLine("Generate vocabulary from training corpus {0}...", train_file);
LearnVocabFromTrainFile(train_file);
}
//filter out words which frequenct is lower
ShrinkVocab();
//If vocabulary is specified in parameter list, but not existed in folder, we need to create it
if (vocab_file != null && vocab_file.Length > 0 && File.Exists(vocab_file) == false)
{
if (debug_mode > 0)
{
//Logger.WriteLine("Saving vocabulary into file...");
}
SaveVocab(vocab_file);
}
next_save_step = savestep;
next_save_trained_words = next_save_step;
if (output_file == null)
{
//Logger.WriteLine("No specified output file name");
return;
}
//Initialize neural network
InitNet();
//Generate word's frequency distribution for negative samping
InitAccTermFreq();
//Load pre-trained model syn0
if (strPreTrainedModelFileName != null)
{
//Logger.WriteLine("Loading syn0 from pre-trained model...");
LoadPreTrainModelSyn(strPreTrainedModelFileName, syn0);
//Logger.WriteLine("Loading syn1 from pre-trained model...");
LoadPreTrainModelSyn(strPreTrainedModelFileName + ".syn1", syn1);
}
if (File.Exists(train_file) == true)
{
string strCurTrainFile = train_file;
for (int j = 0; j < iter; j++)
{
totalNeu_e = new double[layer1_size];
//Logger.WriteLine("Starting training iteration {0}/{1}...", j + 1, iter);
srTrainCorpus = new StreamReader(strCurTrainFile, Encoding.UTF8, true, 102400000);
List<Thread> threadList = new List<Thread>();
for (int i = 0; i < num_threads; i++)
{
Thread thread = new Thread(new ThreadStart(TrainModelThread));
thread.Start();
threadList.Add(thread);
}
//Wait all threads finish their jobs
for (int i = 0; i < num_threads; i++)
{
threadList[i].Join();
}
srTrainCorpus.Close();
double sumErr = 0;
for (int i = 0; i < layer1_size; i++)
{
sumErr += (totalNeu_e[i] / word_count_actual);
}
//Logger.WriteLine("Error: {0}", sumErr);
}
}
else
{
//Logger.WriteLine("Train train file isn't existed.");
return;
}
Model.SaveModel(output_file, vocab_size, layer1_size, vocab, syn0);
Model.SaveModel(output_file + ".syn1", vocab_size, layer1_size, vocab, syn1);
}
}
}