forked from isi-nlp/LSTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp.old
235 lines (194 loc) · 5.09 KB
/
util.cpp.old
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <deque>
#include <vector>
#include <boost/unordered_map.hpp>
#include <boost/algorithm/string.hpp>
#include "maybe_omp.h"
#ifdef EIGEN_USE_MKL_ALL
#include <mkl.h>
#endif
#include "util.h"
extern double drand48();
using namespace Eigen;
using namespace std;
using namespace boost::random;
namespace nplm
{
void readSentFile(const string &file, vector<vector<intern_string> > &sentences)
{
std::cerr << "Reading sentences from: " << file << std::endl;
std::ifstream TRAININ;
TRAININ.open(file.c_str());
if (! TRAININ)
{
std::cerr << "Error: can't read from file " << file<< std::endl;
exit(-1);
}
std::string line;
while (getline(TRAININ, line))
{
vector<string> words;
splitBySpace(line, words);
vector<intern_string> intern_words(words.begin(), words.end());
sentences.push_back(intern_words);
if (sentences.size() % 1000000 == 0)
cerr << sentences.size() << "...";
}
TRAININ.close();
}
void splitBySpace(const std::string &line, std::vector<std::string> &items)
{
string copy(line);
boost::trim_if(copy, boost::is_any_of(" \t"));
if (copy == "")
{
items.clear();
return;
}
boost::split(items, copy, boost::is_any_of(" \t"), boost::token_compress_on);
}
void readWeightsFile(ifstream &TRAININ, vector<float> &weights) {
string line;
while (getline(TRAININ, line) && line != "")
{
vector<string> items;
splitBySpace(line, items);
if (items.size() != 1)
{
cerr << "Error: weights file should have only one weight per line" << endl;
exit(-1);
}
weights.push_back(boost::lexical_cast<float>(items[0]));
}
}
void readWordsFile(ifstream &TRAININ, vector<string> &word_list)
{
string line;
while (getline(TRAININ, line) && line != "")
{
vector<string> words;
splitBySpace(line, words);
if (words.size() != 1)
{
cerr << "Error: vocabulary file must have only one word per line" << endl;
exit(-1);
}
word_list.push_back(words[0]);
}
}
void readWordsFile(const string &file, vector<string> &word_list)
{
cerr << "Reading word list from: " << file<< endl;
ifstream TRAININ;
TRAININ.open(file.c_str());
if (! TRAININ)
{
cerr << "Error: can't read word list from file " << file<< endl;
exit(-1);
}
readWordsFile(TRAININ, word_list);
TRAININ.close();
}
void writeWordsFile(const vector<string> &words, ofstream &file)
{
for (int i=0; i<words.size(); i++)
{
file << words[i] << endl;
}
}
void writeWordsFile(const vector<string> &words, const string &filename)
{
ofstream OUT;
OUT.open(filename.c_str());
if (! OUT)
{
cerr << "Error: can't write to file " << filename << endl;
exit(-1);
}
writeWordsFile(words, OUT);
OUT.close();
}
// Read a data file of unknown size into a flat vector<int>.
// If this takes too much memory, we should create a vector of minibatches.
void readDataFile(const string &filename, int &ngram_size, vector<int> &data, int minibatch_size)
{
cerr << "Reading minibatches from file " << filename << ": ";
ifstream DATAIN(filename.c_str());
if (!DATAIN)
{
cerr << "Error: can't read data from file " << filename<< endl;
exit(-1);
}
vector<int> data_vector;
string line;
long long int n_lines = 0;
while (getline(DATAIN, line))
{
vector<string> ngram;
splitBySpace(line, ngram);
if (ngram_size == 0)
ngram_size = ngram.size();
if (ngram.size() != ngram_size)
{
cerr << "Error: expected " << ngram_size << " fields in instance, found " << ngram.size() << endl;
exit(-1);
}
for (int i=0;i<ngram_size;i++)
data.push_back(boost::lexical_cast<int>(ngram[i]));
n_lines++;
if (minibatch_size && n_lines % (minibatch_size * 10000) == 0)
cerr << n_lines/minibatch_size << "...";
}
cerr << "done." << endl;
DATAIN.close();
}
double logadd(double x, double y)
{
if (x > y)
return x + log1p(std::exp(y-x));
else
return y + log1p(std::exp(x-y));
}
#ifdef USE_CHRONO
void Timer::start(int i)
{
m_start[i] = clock_type::now();
}
void Timer::stop(int i)
{
m_total[i] += clock_type::now() - m_start[i];
}
void Timer::reset(int i) { m_total[i] = duration_type(); }
double Timer::get(int i) const
{
return boost::chrono::duration<double>(m_total[i]).count();
}
Timer timer(20);
#endif
int setup_threads(int n_threads)
{
#ifdef _OPENMP
if (n_threads)
omp_set_num_threads(n_threads);
n_threads = omp_get_max_threads();
if (n_threads > 1)
cerr << "Using " << n_threads << " threads" << endl;
Eigen::initParallel();
Eigen::setNbThreads(n_threads);
#ifdef MKL_SINGLE
// Set the threading layer to match the compiler.
// This lets MKL automatically go single-threaded in parallel regions.
#ifdef __INTEL_COMPILER
mkl_set_threading_layer(MKL_THREADING_INTEL);
#elif defined __GNUC__
mkl_set_threading_layer(MKL_THREADING_GNU);
#endif
mkl_set_num_threads(n_threads);
#endif
#endif
return n_threads;
}
} // namespace nplm