-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.hpp
367 lines (308 loc) · 10.2 KB
/
train.hpp
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
/*
* =====================================================================================
*
* Filename: train.hpp
*
* Description:
*
* Version: 1.0
* Created: 2013-02-15 16:40:41
* Revision: none
* Compiler: gcc
*
* Author: Didzis Gosko (dg), [email protected]
* Organization:
*
* =====================================================================================
*/
#ifndef __TRAIN_HPP__
#define __TRAIN_HPP__
#include <functional>
#include <memory>
#include <iostream>
#include <list>
#include <fstream>
#include <string>
#include <boost/filesystem.hpp>
#include "collins0.hpp"
// Mērķis ir izveidot bāzes klasi, kura spēj kombinēt straumes tā, lai tās būtu secīgi viena pēc otras
// Izdarīt to pilnīgi caurspīdīgi būs sarežģīti (tā šķiet), tāpēc var izveidot kaut ko līdzīgu next()
//
// Pielietojums:
//
// while(streams::stream& stream = streams.next())
// {
// // darbības ar stream objektu šeit (līdz iztērē)
// }
//
// abstrakta bāzes klase
class streams
{
public:
typedef std::basic_istream<char> stream;
streams() : cnull(0) {}
virtual stream& next() = 0;
protected:
std::istream cnull; // pēdējais next() izvads - apzīmē kā beigas
};
// atvasinātā klase cin straumei
class cinstream : public streams
{
public:
cinstream() { done = false; }
stream& next() { return !done ? std::cin : cnull; }
private:
bool done;
};
// atvasinātā klase ievadfailu straumei
class filestreams : public streams
{
public:
filestreams(const std::vector<boost::filesystem::path> paths) { current = nullptr; for(auto& path : paths) files.emplace_back(path.string()); }
stream& next()
{
if(current)
{
delete current;
current = nullptr;
}
if(files.empty())
return cnull;
current = new std::ifstream(files.front());
files.pop_front();
return *current;
}
private:
std::ifstream* current;
std::list<std::string> files;
};
//
// Funkcija, kas ielasa kokus no CoLL faila
//
void readFile(IndexMap& idMap, Trees& trees, const std::string& path, bool useGeneralTags = false);
//
// ConfigValue - šabolons konfigurācijas vērtībām
//
template <typename T, typename R=const T>
class ConfigValue
{
public:
ConfigValue() { rval = NULL; }
ConfigValue(R& r) { rval = &rval; }
// ConfigValue(const T& value) { rval = NULL; _value = value; if(onChange) onChange(); }
const ConfigValue<T,R>& operator=(const ConfigValue<T,R>& s) { _value = s._value; return *this; }
R& operator=(const T& value) { _value = value; if(onChange) onChange(); if(rval) return *rval; return value; }
R& set(const T& value) { _value = value; if(onChange) onChange(); if(rval) return *rval; return value; }
operator const T&() const { return _value; }
operator T&() { return _value; }
const T& value() const { return _value; }
T& operator ()() { return _value; }
const T& operator ()() const { return _value; }
T& value() { return _value; }
std::function<void ()> onChange;
private:
T _value;
R* rval;
};
//
// ConfigValueByPtr - šabolons konfigurācijas vērtībām (glabā kā pointeri)
//
template <typename T, typename R=T>
class ConfigValueByPtr
{
public:
ConfigValueByPtr() { _value = NULL; rval = NULL; }
ConfigValueByPtr(R& r) { rval = &rval; }
// ConfigValueByPtr(T& value) { _value = &value; if(onChange) onChange(); }
const ConfigValueByPtr<T,R>& operator=(const ConfigValueByPtr<T,R>& s) { _value = s._value; return *this; }
T& operator=(T& value) { _value = &value; if(onChange) onChange(); if(rval) return *rval; return value; }
// TODO: throw exception
operator const T&() const { return *_value; }
operator T&() { return *_value; }
const T& value() const { return _value; }
T& value() { return *_value; }
T& operator ()() { return *_value; }
const T& operator ()() const { return *_value; }
operator bool() const { return _value != NULL; }
bool valid() const { return _value != NULL; }
std::function<void ()> onChange;
private:
T* _value;
R* rval;
};
//
// TrainCase - viens treniņa notikums
//
class TrainCases;
class TrainCase
{
public:
// Argumenti treniņam
class Arguments
{
public:
Arguments() {
setupCallbacks();
// noklusēti būs maksimālā kopa
limit = 0;
allowNonProjective = true;
iterations = 5;
trainStart = 0;
trainStop = 0;
checkStart = 0;
checkStop = 0;
featureVectorSize = 300000000;
seed = 0;
permutate = true;
useGeneralTags = false;
quiet = false;
ner = false;
}
Arguments(const Arguments& arguments) {
trainCoNLL = arguments.trainCoNLL;
checkCoNLL = arguments.checkCoNLL;
setupCallbacks();
trainLimit = arguments.trainLimit;
checkLimit = arguments.checkLimit;
trainStart = arguments.trainStart;
checkStart = arguments.checkStart;
trainStop = arguments.trainStop;
checkStop = arguments.checkStop;
iterations = arguments.iterations;
allowTrainNonProjective = arguments.allowTrainNonProjective;
allowCheckNonProjective = arguments.allowCheckNonProjective;
featureVectorSize = arguments.featureVectorSize;
seed = arguments.seed;
permutate = arguments.permutate;
useGeneralTags = arguments.useGeneralTags;
quiet = arguments.quiet;
ner = arguments.ner;
trainTrees = arguments.trainTrees;
checkTrees = arguments.checkTrees;
idMap = arguments.idMap;
_trainTrees = arguments._trainTrees;
_checkTrees = arguments._checkTrees;
_idMap = arguments._idMap;
}
Arguments& setLimit(int value) { limit = value; return *this; }
Arguments& setTrainLimit(int value) { trainLimit = value; return *this; }
Arguments& setCheckLimit(int value) { checkLimit = value; return *this; }
Arguments& setIterations(int value) { iterations = value; return *this; }
Arguments& setTrainCoNLL(std::string filename) { trainCoNLL = filename; return *this; }
Arguments& setCheckCoNLL(std::string filename) { checkCoNLL = filename; return *this; }
Arguments& setTrainTrees(Trees& value) { trainTrees = value; return *this; }
Arguments& setCheckTrees(Trees& value) { checkTrees = value; return *this; }
Arguments& setTrainStart(int value) { trainStart = value; return *this; }
Arguments& setTrainStop(int value) { trainStart = value; return *this; }
Arguments& setCheckStart(int value) { checkStart = value; return *this; }
Arguments& setCheckStop(int value) { checkStart = value; return *this; }
Arguments& setFeatureVecorSize(int value) { featureVectorSize = value; return *this; }
Arguments& setSeed(int value) { seed = value; return *this; }
Arguments& setPermutate(bool value) { permutate = value; return *this; }
Arguments& setUseGeneralTags(bool value) { useGeneralTags = value; return *this; }
Arguments& setIDMap(IndexMap& value) { idMap = value; return *this; }
Arguments& setQuiet(bool value) { quiet = value; return *this; }
Arguments& setNER(bool value) { ner = value; return *this; }
ConfigValue<int> featureVectorSize;
ConfigValue<int> trainStart;
ConfigValue<int> trainStop;
ConfigValue<int> checkStart;
ConfigValue<int> checkStop;
ConfigValue<int> iterations;
ConfigValue<int> limit;
ConfigValue<int> trainLimit;
ConfigValue<int> checkLimit;
ConfigValue<bool> allowNonProjective;
ConfigValue<bool> allowTrainNonProjective;
ConfigValue<bool> allowCheckNonProjective;
ConfigValue<bool> quiet;
ConfigValue<std::string> trainCoNLL;
ConfigValue<std::string> checkCoNLL;
ConfigValueByPtr<Trees> trainTrees;
ConfigValueByPtr<Trees> checkTrees;
ConfigValue<int> seed;
ConfigValue<bool> permutate;
ConfigValue<bool> useGeneralTags;
ConfigValueByPtr<IndexMap> idMap;
ConfigValue<bool> ner;
IndexMap& getIDMap()
{
if(idMap.valid())
return idMap;
if(!_idMap.get())
_idMap.reset(new IndexMap());
return *_idMap.get();
}
private:
void setupCallbacks()
{
limit.onChange = [this]() { trainLimit = limit; checkLimit = limit; };
allowNonProjective.onChange = [this]() { allowTrainNonProjective = allowNonProjective; allowCheckNonProjective = allowNonProjective; };
trainCoNLL.onChange = [this]() {
_trainTrees.reset(new Trees());
trainTrees = *_trainTrees;
readFile(getIDMap(), trainTrees, trainCoNLL, useGeneralTags());
};
checkCoNLL.onChange = [this]() {
_checkTrees.reset(new Trees());
checkTrees = *_checkTrees;
readFile(getIDMap(), checkTrees, checkCoNLL, useGeneralTags());
};
}
std::shared_ptr<Trees> _trainTrees;
std::shared_ptr<Trees> _checkTrees;
std::shared_ptr<IndexMap> _idMap;
};
template <typename... OtherArguments>
TrainCase(const Arguments& args, const OtherArguments&... otherArgs) : arguments(args), featureVector(0) { run(); }
FeatureVector featureVector;
private:
class CheckResult
{
public:
CheckResult(int index, int matched, int size, double duration) { _index = index; _matched = matched; _size = size; _duration = duration; }
int index() const { return _index; }
int matched() const { return _matched; }
int size() const { return _size; }
double duratoin() const { return _duration; }
private:
int _index;
int _matched;
int _size;
double _duration;
};
void run();
void train(FeatureVector& featureVector);
void check(const FeatureVector& featureVector);
Arguments arguments;
// FeatureVector featureVector;
int collisions;
int trainTreeCount;
int checkTreeCount;
int checkUASTotalMatches;
int checkUASTotalCount;
// rezultātu glabātuve
double trainTime;
double checkTime;
std::vector<CheckResult> checkResults;
friend class TrainCases;
};
//
// Treniņu gadījumu apvienojums
//
class TrainCases
{
public:
TrainCases(int reserve = 100) { trainCases.reserve(reserve); }
template <typename... OtherArguments>
void operator()(const TrainCase::Arguments& args, const OtherArguments&... otherArgs) { trainCases.emplace_back(args); }
const TrainCase& last() const { return trainCases.back(); }
void summary();
private:
std::vector<TrainCase> trainCases;
};
bool train(TrainCase::Arguments& arguments, FeatureVector& featureVector, IndexMap& idMap, streams& istreams);
bool verify(TrainCase::Arguments& arguments, const FeatureVector& featureVector, const IndexMap& idMap, streams& istreams);
bool parse(TrainCase::Arguments& arguments, const FeatureVector& featureVector, const IndexMap& idMap,
streams& istreams, std::basic_ostream<char>& ostream);
#endif // __TRAIN_HPP__