forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_args.cc
214 lines (186 loc) · 7.56 KB
/
parse_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
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights
embodied in the content of this file are licensed under the BSD
(revised) open source license
*/
#include <stdio.h>
#include "cache.h"
#include "io.h"
#include "parse_regressor.h"
#include "parser.h"
#include "parse_args.h"
#include "sender.h"
#include "network.h"
const float default_decay = 1. / sqrt(2.);
po::variables_map parse_args(int argc, char *argv[], boost::program_options::options_description& desc,
gd_vars& vars, float& eta_decay_rate,
size_t &passes, regressor &r, parser* par,
string& final_regressor_name)
{
vars.init();
global.program_name = argv[0];
// Declare the supported options.
desc.add_options()
("audit,a", "print weights of features")
("bit_precision,b", po::value<size_t>(),
"number of bits in the feature table")
("cache,c", "Use a cache. The default is <data>.cache")
("cache_file", po::value< vector<string> >(), "The location(s) of cache_file.")
("data,d", po::value< string >()->default_value(""), "Example Set")
("daemon", "read data from port 39523")
("decay_learning_rate", po::value<float>(&eta_decay_rate)->default_value(default_decay),
"Set Decay factor for learning_rate between passes")
("final_regressor,f", po::value< string >(), "Final regressor")
("help,h","Output Arguments")
("initial_regressor,i", po::value< vector<string> >(), "Initial regressor(s)")
("initial_t", po::value<float>(&vars.t)->default_value(1.), "initial t value")
("min_prediction", po::value<float>(&vars.min_prediction)->default_value(0), "Smallest prediction to output")
("max_prediction", po::value<float>(&vars.max_prediction)->default_value(1), "Largest prediction to output")
("multisource", po::value<size_t>(), "multiple sources for daemon input")
("noop","do no learning")
("port", po::value<size_t>(),"port to listen on")
("power_t", po::value<float>(&vars.power_t)->default_value(0.), "t power value")
("predictto", po::value< string > (), "host to send predictions to")
("learning_rate,l", po::value<float>(&vars.eta)->default_value(0.1),
"Set Learning Rate")
("passes", po::value<size_t>(&passes)->default_value(1),
"Number of Training Passes")
("predictions,p", po::value< string >(), "File to output predictions to")
("quadratic,q", po::value< vector<string> > (),
"Create and use quadratic features")
("quiet", "Don't output diagnostics")
("raw_predictions,r", po::value< string >(),
"File to output unnormalized predictions to")
("sendto", po::value< vector<string> >(), "send example to <hosts>")
("testonly,t", "Ignore label information and just test")
("thread_bits", po::value<size_t>(&global.thread_bits)->default_value(0), "log_2 threads")
("loss_function", po::value<string>()->default_value("squaredloss"), "Specify the loss function to be used, uses squaredloss by default. Currently available ones are squaredloss, hingeloss, logloss and quantilesloss.")
("quantiles_tau", po::value<double>()->default_value(0.0), "Parameter \\tau associated with Quantiles loss. Unless mentioned this parameter would default to a value of 0.0")
("unique_id", po::value<size_t>(&global.unique_id)->default_value(0),"unique id used for cluster parallel");
global.example_number = 0;
global.weighted_examples = 0.;
global.old_weighted_examples = 0.;
global.weighted_labels = 0.;
global.total_features = 0;
global.sum_loss = 0.0;
global.sum_loss_since_last_dump = 0.0;
global.dump_interval = exp(1.);
global.num_bits = 18;
global.default_bits = true;
global.final_prediction_sink = -1;
global.raw_prediction = -1;
global.local_prediction = -1;
global.print = print_result;
global.audit = false;
global.reg = r;
po::positional_options_description p;
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help") || argc == 1) {
cerr << "\n" << desc << "\n";
exit(1);
}
if (vm.count("bit_precision"))
{
global.default_bits = false;
global.num_bits = vm["bit_precision"].as< size_t>();
}
if (global.num_bits > 31) {
cerr << "The system limits at 31 bits of precision!\n" << endl;
exit(1);
}
if (vm.count("quiet"))
global.quiet = true;
else
global.quiet = false;
if (vm.count("quadratic"))
{
global.pairs = vm["quadratic"].as< vector<string> >();
if (!global.quiet)
{
cerr << "creating quadratic features for pairs: ";
for (vector<string>::iterator i = global.pairs.begin(); i != global.pairs.end();i++) {
cerr << *i << " ";
if (i->length() > 2)
cerr << endl << "warning, ignoring characters after the 2nd.\n";
if (i->length() < 2) {
cerr << endl << "error, quadratic features must involve two sets.\n";
exit(0);
}
}
cerr << endl;
}
}
parse_regressor_args(vm, r, final_regressor_name, global.quiet);
string loss_function;
if(vm.count("loss_function"))
loss_function = vm["loss_function"].as<string>();
else
loss_function = "squaredloss";
double loss_parameter = 0.0;
if(vm.count("quantiles_tau"))
loss_parameter = vm["quantiles_tau"].as<double>();
r.loss = getLossFunction(loss_function, loss_parameter);
global.loss = r.loss;
vars.eta *= pow(vars.t, vars.power_t);
if (eta_decay_rate != default_decay && passes == 1)
cerr << "Warning: decay_learning_rate has no effect when there is only one pass" << endl;
if (pow(eta_decay_rate, passes) < 0.0001 )
cerr << "Warning: the learning rate for the last pass is multiplied by: " << pow(eta_decay_rate, passes)
<< " adjust to --decay_learning_rate larger to avoid this." << endl;
parse_source_args(vm,par,global.quiet,passes);
if (!global.quiet)
{
cerr << "Num weight bits = " << global.num_bits << endl;
cerr << "learning rate = " << vars.eta << endl;
cerr << "initial_t = " << vars.t << endl;
cerr << "power_t = " << vars.power_t << endl;
if (passes > 1)
cerr << "decay_learning_rate = " << eta_decay_rate << endl;
}
if (vm.count("predictions")) {
if (!global.quiet)
cerr << "predictions = " << vm["predictions"].as< string >() << endl;
if (strcmp(vm["predictions"].as< string >().c_str(), "stdout") == 0)
global.final_prediction_sink = 1;//stdout
else
{
const char* fstr = (vm["predictions"].as< string >().c_str());
global.final_prediction_sink = fileno(fopen(fstr,"w"));
if (global.final_prediction_sink < 0)
cerr << "Error opening the predictions file: " << fstr << endl;
}
}
if (vm.count("raw_predictions")) {
if (!global.quiet)
cerr << "raw predictions = " << vm["raw_predictions"].as< string >() << endl;
if (strcmp(vm["raw_predictions"].as< string >().c_str(), "stdout") == 0)
global.raw_prediction = 1;//stdout
else
global.raw_prediction = fileno(fopen(vm["raw_predictions"].as< string >().c_str(), "w"));
}
if (vm.count("audit"))
global.audit = true;
parse_send_args(vm, global.pairs, global.thread_bits);
if (vm.count("testonly"))
{
if (!global.quiet)
cerr << "only testing" << endl;
global.training = false;
}
else
{
global.training = true;
if (!global.quiet)
cerr << "learning_rate set to " << vars.eta << endl;
}
if (vm.count("predictto"))
{
if (!global.quiet)
cerr << "predictto = " << vm["predictto"].as< string >() << endl;
global.local_prediction = open_socket(vm["predictto"].as< string > ().c_str(), global.unique_id);
}
return vm;
}