-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc2vecc.cpp
More file actions
162 lines (147 loc) · 5.41 KB
/
Copy pathdoc2vecc.cpp
File metadata and controls
162 lines (147 loc) · 5.41 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
#include <cinttypes>
#include <cstdlib>
#include <iostream>
#include "components/model_config.hpp"
#include "components/vocab.hpp"
#include "libs/huffman.hpp"
#include "model.hpp"
#include "utils/arg_parser.hpp"
using llf = double;
using llu = uint64_t;
const llf kDefaultCBOWLearningRate = 0.05;
const llf kDefaultSkipGramLearningRate = 0.025;
int gpu_id, debug;
llu min_count;
std::string vocab_output, vocab_source, output_file, test_file;
void PrintUsage() {
std::cout << R"(GPU accelerated Doc2VecC implementation
Options:
Parameters for training:
-train <file>
Use text data from <file> to train the model; default is data.txt
-word <file>
Use <file> to save the resulting word vectors; default is wordvec.txt
-output <file>
Use <file> to save the resulting document vectors; default is docvec.txt
-test <file>
Predict text data from <file> with model; default is test.txt
-size <int>
Set size of word vectors; default is 100
-window <int>
Set max skip length between words; default is 5
-sample <float>
Set threshold for occurrence of words. Those that appear with higher frequency in the training data
will be randomly down-sampled; default is 1e-3, useful range is (0, 1e-5)
-hs <int>
Use Hierarchical Softmax; default is 0 (not used)
-negative <int>
Number of negative examples; default is 5, common values are 3 - 10 (0 = not used)
-iter <int>
Run more training iterations (default 10)
-min-count <int>
This will discard words that appear less than <int> times; default is 5
-alpha <float>
Set the starting learning rate; default is 0.025 for skip-gram and 0.05 for CBOW
-debug <int>
Set the debug mode (default = 2 = more info during training)
-binary <int>
Save the resulting vectors in binary moded; default is 0 (off)
-save-vocab <file>
The vocabulary will be saved to <file>
-read-vocab <file>
The vocabulary will be read from <file>, not constructed from the training data
-cbow <int>
Use the continuous bag of words model; default is 1 (use 0 for skip-gram model)
-sentence-sample <float>
The rate to sample words out of a document for documenet representation; default is 0.1
-vocab-limit <int>
The size limit of vocabulary dictionary; default is 21000000
Examples:
./doc2vecc -train data.txt -output docvec.txt -word wordvec.txt -sentence-sample 0.1 -size 200 -window 5 -sample 1e-4 -negative 5 -hs 0 -binary 0 -cbow 1 -iter 3)"
<< std::endl;
}
ArgParser GetParser() {
ArgParser parser;
parser.add_argument("-train", "data.txt");
parser.add_argument("-word", "wordvec.txt");
parser.add_argument("-output", "docvec.txt");
parser.add_argument("-test", "test.txt");
parser.add_argument("-size", "100");
parser.add_argument("-window", "5");
parser.add_argument("-sample", "1e-3");
parser.add_argument("-hs", "0");
parser.add_argument("-negative", "5");
parser.add_argument("-iter", "10");
parser.add_argument("-min-count", "5");
parser.add_argument("-alpha");
parser.add_argument("-debug", "2");
parser.add_argument("-binary", "0");
parser.add_argument("-save-vocab");
parser.add_argument("-read-vocab");
parser.add_argument("-cbow", "1");
parser.add_argument("-sentence-sample", "0.1");
parser.add_argument("-vocab-limit", "21000000");
return parser;
}
ModelConfig ParseArgs(const ArgParser &arg_parser) {
vocab_output = arg_parser.getopt("-save-vocab");
vocab_source = arg_parser.getopt("-read-vocab");
output_file = arg_parser.getopt("-output");
test_file = arg_parser.getopt("-test");
debug = stoi(arg_parser.getopt("-debug"));
min_count = stoull(arg_parser.getopt("-min-count"));
ModelConfig conf;
conf.train_file = arg_parser.getopt("-train");
conf.wordembedding_file = arg_parser.getopt("-word");
conf.layer_size = stoi(arg_parser.getopt("-size"));
conf.window_size = stoi(arg_parser.getopt("-window"));
conf.sample_rate = stod(arg_parser.getopt("-sample"));
conf.hierarchical_softmax = static_cast<bool>(stoi(arg_parser.getopt("-hs")));
conf.negative_sample = stoi(arg_parser.getopt("-negative"));
conf.iterations = stoull(arg_parser.getopt("-iter"));
conf.cbow = static_cast<bool>(stoi(arg_parser.getopt("-cbow")));
if (conf.cbow) {
conf.alpha = kDefaultCBOWLearningRate;
} else {
conf.alpha = kDefaultSkipGramLearningRate;
}
if (not arg_parser.getopt("-alpha").empty()) {
conf.alpha = stod(arg_parser.getopt("-alpha"));
}
conf.binary = static_cast<bool>(stoi(arg_parser.getopt("-binary")));
conf.rp_sample = stod(arg_parser.getopt("-sentence-sample"));
return conf;
}
int main(int argc, const char **argv) {
if (argc == 1) {
PrintUsage();
return 0;
}
ArgParser arg_parser = GetParser();
arg_parser.parse_arg(argc, argv);
ModelConfig conf = ParseArgs(arg_parser);
Vocab vocab;
llu words_count = 0;
if (not vocab_source.empty()) {
words_count = vocab.RestoreFromSavedFile(vocab_source);
} else {
words_count = vocab.BuildFromFile(conf.train_file);
}
vocab.Reduce(min_count);
vocab.Sort();
std::cout << "Vocab size: " << vocab.size() << " \n";
std::cout << "Words in train file: " << words_count << std::endl;
if (not vocab_output.empty()) {
vocab.SaveToFile(vocab_output);
}
if (output_file.empty()) {
return 0;
}
VocabWord *words;
BuildBinaryTree(vocab, &words);
llf *model;
TrainModel(vocab, conf, words, model);
PredictModel(model, conf.sample_rate, conf.layer_size, vocab, words,
test_file, output_file);
return 0;
}