forked from facebookresearch/fastText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.cc
293 lines (273 loc) · 9.71 KB
/
args.cc
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
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "args.h"
#include <stdlib.h>
#include <iostream>
#include <stdexcept>
namespace fasttext {
Args::Args() {
lr = 0.05;
dim = 100;
ws = 5;
epoch = 5;
minCount = 5;
minCountLabel = 0;
neg = 5;
wordNgrams = 1;
loss = loss_name::ns;
model = model_name::sg;
bucket = 2000000;
minn = 3;
maxn = 6;
thread = 12;
lrUpdateRate = 100;
t = 1e-4;
label = "__label__";
verbose = 2;
pretrainedVectors = "";
saveOutput = false;
qout = false;
retrain = false;
qnorm = false;
cutoff = 0;
dsub = 2;
}
std::string Args::lossToString(loss_name ln) const {
switch (ln) {
case loss_name::hs:
return "hs";
case loss_name::ns:
return "ns";
case loss_name::softmax:
return "softmax";
}
return "Unknown loss!"; // should never happen
}
std::string Args::boolToString(bool b) const {
if (b) {
return "true";
} else {
return "false";
}
}
std::string Args::modelToString(model_name mn) const {
switch (mn) {
case model_name::cbow:
return "cbow";
case model_name::sg:
return "sg";
case model_name::sup:
return "sup";
}
return "Unknown model name!"; // should never happen
}
void Args::parseArgs(const std::vector<std::string>& args) {
std::string command(args[1]);
if (command == "supervised") {
model = model_name::sup;
loss = loss_name::softmax;
minCount = 1;
minn = 0;
maxn = 0;
lr = 0.1;
} else if (command == "cbow") {
model = model_name::cbow;
}
for (int ai = 2; ai < args.size(); ai += 2) {
if (args[ai][0] != '-') {
std::cerr << "Provided argument without a dash! Usage:" << std::endl;
printHelp();
exit(EXIT_FAILURE);
}
try {
if (args[ai] == "-h") {
std::cerr << "Here is the help! Usage:" << std::endl;
printHelp();
exit(EXIT_FAILURE);
} else if (args[ai] == "-input") {
input = std::string(args.at(ai + 1));
} else if (args[ai] == "-output") {
output = std::string(args.at(ai + 1));
} else if (args[ai] == "-lr") {
lr = std::stof(args.at(ai + 1));
} else if (args[ai] == "-lrUpdateRate") {
lrUpdateRate = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-dim") {
dim = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-ws") {
ws = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-epoch") {
epoch = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-minCount") {
minCount = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-minCountLabel") {
minCountLabel = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-neg") {
neg = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-wordNgrams") {
wordNgrams = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-loss") {
if (args.at(ai + 1) == "hs") {
loss = loss_name::hs;
} else if (args.at(ai + 1) == "ns") {
loss = loss_name::ns;
} else if (args.at(ai + 1) == "softmax") {
loss = loss_name::softmax;
} else {
std::cerr << "Unknown loss: " << args.at(ai + 1) << std::endl;
printHelp();
exit(EXIT_FAILURE);
}
} else if (args[ai] == "-bucket") {
bucket = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-minn") {
minn = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-maxn") {
maxn = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-thread") {
thread = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-t") {
t = std::stof(args.at(ai + 1));
} else if (args[ai] == "-label") {
label = std::string(args.at(ai + 1));
} else if (args[ai] == "-verbose") {
verbose = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-pretrainedVectors") {
pretrainedVectors = std::string(args.at(ai + 1));
} else if (args[ai] == "-saveOutput") {
saveOutput = true;
ai--;
} else if (args[ai] == "-qnorm") {
qnorm = true;
ai--;
} else if (args[ai] == "-retrain") {
retrain = true;
ai--;
} else if (args[ai] == "-qout") {
qout = true;
ai--;
} else if (args[ai] == "-cutoff") {
cutoff = std::stoi(args.at(ai + 1));
} else if (args[ai] == "-dsub") {
dsub = std::stoi(args.at(ai + 1));
} else {
std::cerr << "Unknown argument: " << args[ai] << std::endl;
printHelp();
exit(EXIT_FAILURE);
}
} catch (std::out_of_range) {
std::cerr << args[ai] << " is missing an argument" << std::endl;
printHelp();
exit(EXIT_FAILURE);
}
}
if (input.empty() || output.empty()) {
std::cerr << "Empty input or output path." << std::endl;
printHelp();
exit(EXIT_FAILURE);
}
if (wordNgrams <= 1 && maxn == 0) {
bucket = 0;
}
}
void Args::printHelp() {
printBasicHelp();
printDictionaryHelp();
printTrainingHelp();
printQuantizationHelp();
}
void Args::printBasicHelp() {
std::cerr
<< "\nThe following arguments are mandatory:\n"
<< " -input training file path\n"
<< " -output output file path\n"
<< "\nThe following arguments are optional:\n"
<< " -verbose verbosity level [" << verbose << "]\n";
}
void Args::printDictionaryHelp() {
std::cerr
<< "\nThe following arguments for the dictionary are optional:\n"
<< " -minCount minimal number of word occurences [" << minCount << "]\n"
<< " -minCountLabel minimal number of label occurences [" << minCountLabel << "]\n"
<< " -wordNgrams max length of word ngram [" << wordNgrams << "]\n"
<< " -bucket number of buckets [" << bucket << "]\n"
<< " -minn min length of char ngram [" << minn << "]\n"
<< " -maxn max length of char ngram [" << maxn << "]\n"
<< " -t sampling threshold [" << t << "]\n"
<< " -label labels prefix [" << label << "]\n";
}
void Args::printTrainingHelp() {
std::cerr
<< "\nThe following arguments for training are optional:\n"
<< " -lr learning rate [" << lr << "]\n"
<< " -lrUpdateRate change the rate of updates for the learning rate [" << lrUpdateRate << "]\n"
<< " -dim size of word vectors [" << dim << "]\n"
<< " -ws size of the context window [" << ws << "]\n"
<< " -epoch number of epochs [" << epoch << "]\n"
<< " -neg number of negatives sampled [" << neg << "]\n"
<< " -loss loss function {ns, hs, softmax} [" << lossToString(loss) << "]\n"
<< " -thread number of threads [" << thread << "]\n"
<< " -pretrainedVectors pretrained word vectors for supervised learning ["<< pretrainedVectors <<"]\n"
<< " -saveOutput whether output params should be saved [" << boolToString(saveOutput) << "]\n";
}
void Args::printQuantizationHelp() {
std::cerr
<< "\nThe following arguments for quantization are optional:\n"
<< " -cutoff number of words and ngrams to retain [" << cutoff << "]\n"
<< " -retrain whether embeddings are finetuned if a cutoff is applied [" << boolToString(retrain) << "]\n"
<< " -qnorm whether the norm is quantized separately [" << boolToString(qnorm) << "]\n"
<< " -qout whether the classifier is quantized [" << boolToString(qout) << "]\n"
<< " -dsub size of each sub-vector [" << dsub << "]\n";
}
void Args::save(std::ostream& out) {
out.write((char*) &(dim), sizeof(int));
out.write((char*) &(ws), sizeof(int));
out.write((char*) &(epoch), sizeof(int));
out.write((char*) &(minCount), sizeof(int));
out.write((char*) &(neg), sizeof(int));
out.write((char*) &(wordNgrams), sizeof(int));
out.write((char*) &(loss), sizeof(loss_name));
out.write((char*) &(model), sizeof(model_name));
out.write((char*) &(bucket), sizeof(int));
out.write((char*) &(minn), sizeof(int));
out.write((char*) &(maxn), sizeof(int));
out.write((char*) &(lrUpdateRate), sizeof(int));
out.write((char*) &(t), sizeof(double));
}
void Args::load(std::istream& in) {
in.read((char*) &(dim), sizeof(int));
in.read((char*) &(ws), sizeof(int));
in.read((char*) &(epoch), sizeof(int));
in.read((char*) &(minCount), sizeof(int));
in.read((char*) &(neg), sizeof(int));
in.read((char*) &(wordNgrams), sizeof(int));
in.read((char*) &(loss), sizeof(loss_name));
in.read((char*) &(model), sizeof(model_name));
in.read((char*) &(bucket), sizeof(int));
in.read((char*) &(minn), sizeof(int));
in.read((char*) &(maxn), sizeof(int));
in.read((char*) &(lrUpdateRate), sizeof(int));
in.read((char*) &(t), sizeof(double));
}
void Args::dump(std::ostream& out) const {
out << "dim" << " " << dim << std::endl;
out << "ws" << " " << ws << std::endl;
out << "epoch" << " " << epoch << std::endl;
out << "minCount" << " " << minCount << std::endl;
out << "neg" << " " << neg << std::endl;
out << "wordNgrams" << " " << wordNgrams << std::endl;
out << "loss" << " " << lossToString(loss) << std::endl;
out << "model" << " " << modelToString(model) << std::endl;
out << "bucket" << " " << bucket << std::endl;
out << "minn" << " " << minn << std::endl;
out << "maxn" << " " << maxn << std::endl;
out << "lrUpdateRate" << " " << lrUpdateRate << std::endl;
out << "t" << " " << t << std::endl;
}
}