-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.cpp
236 lines (208 loc) · 7.44 KB
/
Options.cpp
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
/*
* Recursive Neural Networks: neural networks for data structures
*
* Copyright (C) 2018 Alessandro Vullo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "General.h"
#include "Options.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
void Options::parse_args(int argc, char* argv[])
throw(BadOptionSetting) {
// prepend program name to usage string
_usage = string(argv[0]) + " " + _usage;
// parse command line switches, search only for global configuration file option
// derived classes will consider application specific flags
for(int i=1; i<argc; i++) {
string option(argv[i]);
if(option == "-c") {
args["config"] = string(argv[i+1]);
continue;
}
}
// echo usage in case required application parameters are not given
map<string, string>::const_iterator it;
it = args.find("config");
if(it == args.end())
throw BadOptionSetting("Must specify global configuration file; use -c <config file> command line flag");
// read global configuration file and set globally visible parameters
ifstream ifs((*it).second.c_str());
if(!ifs)
throw BadOptionSetting(string("Could not open ") + (*it).second.c_str() + "\n\n" + _usage);
string line, dummy;
while(getline(ifs, line)) {
if(line[0] == '#') continue; // skip comments
size_t pos;
istringstream iss(line);
pos = line.find("input_dimension");
if(pos != string::npos) {
iss >> dummy >> _input_dim;
assert(_input_dim > 0);
continue;
}
pos = line.find("output_dimension");
if(pos != string::npos) {
iss >> dummy >> _output_dim;
assert(_output_dim > 0);
continue;
}
pos = line.find("domain_outdegree");
if(pos != string::npos) {
iss >> dummy >> _domain_outdegree;
assert(_domain_outdegree > 0);
continue;
}
pos = line.find("layers_number_units");
if(pos != string::npos) {
_lnunits.clear();
iss >> dummy >> _r >> _s;
int i = 0, lnu;
while(i<_r+_s) {
iss >> lnu;
_lnunits.push_back(lnu);
++i;
}
assert(_lnunits.size() == (uint)_r+_s);
continue;
}
pos = line.find("rnn_weights_precision");
if(pos != string::npos) {
iss >> dummy >> _precision;
assert(_precision > 0);
continue;
}
pos = line.find("domain");
if(pos != string::npos) {
string d;
iss >> dummy >> d;
if(d == "DOAG") { _domain = DOAG; }
else if(d == "SEQUENCE") { _domain = SEQUENCE; }
else if(d == "LINEARCHAIN") { _domain = LINEARCHAIN; }
else if(d == "NARYTREE") { _domain = NARYTREE; }
else if(d == "UG") { _domain = UG; }
else if(d == "GRID2D") { _domain = GRID2D; }
else { throw BadOptionSetting("Unrecognised domain type"); }
continue;
}
pos = line.find("transduction");
if(pos != string::npos) {
string trans;
iss >> dummy >> trans;
if(trans == "SUPER_SOURCE") { _transduction = SUPER_SOURCE; }
else if(trans == "IO_ISOMORPH") { _transduction = IO_ISOMORPH; }
else { throw BadOptionSetting("Transduction type not recognized"); }
continue;
}
pos = line.find("problem");
if(pos != string::npos) {
string prob;
iss >> dummy >> prob;
if(prob == "REGRESSION") { _problem = REGRESSION; }
else if(prob == "BINARYCLASS") { _problem = BINARYCLASS; }
else if(prob == "MULTICLASS") { _problem = MULTICLASS; }
else { throw BadOptionSetting("Problem type not recognized"); }
continue;
}
}
// raise exception if config parameters are not set
if(!_input_dim)
throw BadOptionSetting("Must set input_dimension to a positive value");
if(!_output_dim)
throw BadOptionSetting("Must set output_dimension to a positive value");
if(!_domain_outdegree)
throw BadOptionSetting("Must set domain_outdegree to a positive value");
if(!_r)
throw BadOptionSetting("Must set number of layers in folding network to a positive value");
if(!_s)
throw BadOptionSetting("Must set number of layers in transforming network to a positive value");
if(!_lnunits.size())
throw BadOptionSetting("Couldn't set architecture of the folding and tranforming networks");
if(_transduction != SUPER_SOURCE && _transduction != IO_ISOMORPH)
throw BadOptionSetting("Invalid transduction type: " + _transduction);
if(_problem & ~(REGRESSION | BINARYCLASS | MULTICLASS))
throw BadOptionSetting("Invalid problem type");
}
void RNNTrainingOptions::parse_args(int argc, char* argv[])
throw(Options::BadOptionSetting) {
// parse configuration file first
Options::parse_args(argc, argv);
/*** parse command line and find application specific options ***/
for (int i = 1; i < argc; i++) {
// parse switches
if (argv[i][0] == '-') {
string arg(argv[i]);
if(arg == "-c") {
// this is already read by base object
// TODO: should avoid considering it here
++i;
} else if(arg == "-n") {
args["netname"] = string(argv[++i]);
} else if(arg == "-l") {
args["eta"] = string(argv[++i]);
} else if(arg == "--alpha") {
args["alpha"] = string(argv[++i]);
} else if(arg == "-ni") {
args["ni"] = string(argv[++i]);
} else if(arg == "-e") {
args["epochs"] = string(argv[++i]);
} else if(arg == "-s") {
args["savedelta"] = string(argv[++i]);
} else if(arg == "-r") {
args["random_net"] = string("1");
} else if(arg == "-o") {
args["onlinelearning"] = string("1");
} else if(arg == "--training-set") {
args["training_set"] = string(argv[++i]);
} else if(arg == "--test-set") {
args["test_set"] = string(argv[++i]);
} else if(arg == "--validation-set") {
args["validation_set"] = string(argv[++i]);
} else if(arg == "--threshold-error") {
args["threshold_error"] = string(argv[++i]);
} else {
cerr << "Unknown switch " << argv[i] << "\n";
throw BadOptionSetting(_usage);
}
}
}
}
Options* Options::instance() throw(BadOptionSetting) {
if(_instance == 0) {
try {
string option_type(getenv("RNNOPTIONTYPE"));
if(option_type == "train")
_instance = new RNNTrainingOptions;
else
throw BadOptionSetting("Invalid RNNOPTIONTYPE value");
} catch (logic_error& e) {
cerr << e.what() << endl;
throw BadOptionSetting("Must set RNNOPTIONTYPE envirnoment variable");
}
}
return _instance;
}
// static member initialization
Options* Options::_instance = 0;