forked from oir/deep-recurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
executable file
·288 lines (251 loc) · 6.37 KB
/
Copy pathutils.cpp
File metadata and controls
executable file
·288 lines (251 loc) · 6.37 KB
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
#ifndef UTILS_CPP
#define UTILS_CPP
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <cassert>
#include <sys/stat.h>
#include "Eigen/Dense"
#define uint unsigned int
using namespace Eigen;
using namespace std;
namespace Eigen {
istream& operator>>(istream& s, MatrixXd& m) {
for (uint i=0; i<m.rows(); i++)
for (uint j=0; j<m.cols(); j++)
s >> m(i,j);
return s;
}
istream& operator>>(istream& s, VectorXd& m) {
for (uint i=0; i<m.size(); i++)
s >> m(i);
return s;
}
}
MatrixXd confusion_matrix(const vector<string> & y_hat, const vector<string> & y, uint num_classes) {
MatrixXd conf_mat = MatrixXd::Zero(num_classes, num_classes);
for (int i = 0; i < y.size(); i++) {
conf_mat(stoi(y[i]), stoi(y_hat[i])) += 1;
}
return conf_mat;
}
MatrixXd softmax(const MatrixXd &x) {
RowVectorXd m = x.colwise().maxCoeff();
MatrixXd t = (x.rowwise() - m).array().exp();
return t.array().rowwise() / t.colwise().sum().array();
}
MatrixXd smaxentp(const MatrixXd &y, const MatrixXd &r) {
return y-r;
}
MatrixXd relu(const MatrixXd &x) {
return x.array().max(0);
}
MatrixXd relup(const MatrixXd &x) {
return (x.array() > 0).cast<double>();
}
MatrixXd sigmoid(const MatrixXd &x) {
return 1.0 / (1.0 + (-x).array().exp());
}
MatrixXd sigmoidp(const MatrixXd &x) {
return x.array() * (1.0 - x.array());
}
MatrixXd fast_sigmoid(const MatrixXd &x) {
return x.array() / (1.0 + x.array().abs());
}
MatrixXd fast_sigmoidp(const MatrixXd &x) {
return 1.0 / (1.0 + x.array().abs()).pow(2);
}
MatrixXd _tanh(const MatrixXd &x) {
return (2.0 / (1.0 + (-2.0 * x).array().exp())) - 1.0;
}
MatrixXd _tanhp(const MatrixXd &x) {
return 1.0 - x.array().pow(2);
}
MatrixXd clip(const MatrixXd &x) {
return x.array().min(1e10).max(-1e10);
}
double str2double(const string& s) {
istringstream i(s);
double x;
if (!(i >> x))
return 0;
return x;
}
class LookupTable {
public:
void load(string fname, uint n, uint d, bool noUnknown=false);
//ColXpr operator[](string word);
VectorXd operator[](string word);
void gradAdd(string word, VectorXd v);
void update();
private:
double lr;
map<string, uint> table; // word -> index
MatrixXd data; // index -> vector representation
MatrixXd gdata; // gradients
MatrixXd adata; // adagrad past
set<uint> modifiedCols;
};
void LookupTable::load(string fname, uint n, uint d, bool noUnknown) {
ifstream in(fname.c_str());
assert(in.is_open());
string line;
if (noUnknown) n++;
data = MatrixXd(d,n);
gdata = adata = MatrixXd::Zero(d,n);
adata.fill(1e-6);
uint j=0;
while(std::getline(in, line)) {
std::istringstream ss(line);
std::istream_iterator<std::string> begin(ss), end;
//putting all the tokens in the vector
std::vector<std::string> tokens(begin, end);
for (uint i=0; i<d; i++)
data(i,j) = str2double(tokens[i+1]);
table[tokens[0]] = j;
j++;
if (j == n)
break;
}
if (noUnknown) {
VectorXd v = data.rowwise().mean();
data.col(n-1) = v;
table["*UNKNOWN*"] = n-1;
}
double min = data.minCoeff();
//cout << "Lookup table min: " << min << endl;
//data = data.array()-min;
}
VectorXd LookupTable::operator[](string word) {
map<string,uint>::iterator it;
// this might not be the best place for this,
// if i'm calling this frequently
if (word == "-LRB-")
word = "(";
else if (word == "-RRB-")
word = ")";
else if (word == "-LSB-")
word = "(";
else if (word == "-RSB-")
word = ")";
else if (word == "-LCB-")
word = "(";
else if (word == "-RCB-")
word = ")";
it = table.find(word);
if (it != table.end()) // exists
return data.col(table[word]);
else
return data.col(table["*UNKNOWN*"]);
}
void LookupTable::gradAdd(string word, VectorXd v) {
map<string,uint>::iterator it;
if (word == "-LRB-")
word = "(";
else if (word == "-RRB-")
word = ")";
else if (word == "-LSB-")
word = "(";
else if (word == "-RSB-")
word = ")";
else if (word == "-LCB-")
word = "(";
else if (word == "-RCB-")
word = ")";
it = table.find(word);
if (it != table.end()) {// exists
gdata.col(table[word]) += v;
modifiedCols.insert(table[word]);
} else {
gdata.col(table["*UNKNOWN*"]) += v;
modifiedCols.insert(table[word]);
}
}
void LookupTable::update() {
lr = 0.001;
for (auto i : modifiedCols) {
adata.col(i) = (adata.col(i).cwiseProduct(adata.col(i)) +
gdata.col(i).cwiseProduct(gdata.col(i))).cwiseSqrt();
data.col(i) -= lr*gdata.col(i).cwiseQuotient(adata.col(i));
gdata.col(i).setZero();
}
modifiedCols.clear();
}
// index of max in a vector
uint argmax(const VectorXd& x) {
double max = x(0);
uint maxi = 0;
for (uint i=1; i<x.size(); i++) {
if (x(i) > max) {
max = x(i);
maxi = i;
}
}
return maxi;
}
// this is used for randomly initializing an Eigen matrix
double urand(double dummy) {
double min = -0.01, max = 0.01;
return (double(rand())/RAND_MAX)*(max-min) + min;
}
// KFY shuffle (uniformly randomly) of a vector
template <class T>
void shuffle(vector<T>& v) {
for (uint i=v.size()-1; i>0; i--) {
uint j = (rand() % i);
T tmp = v[i];
v[i] = v[j];
v[j] = tmp;
}
}
template <class T, class T2>
void shuffle(vector<T>& v, vector<T2>& w) {
assert(w.size() == v.size());
for (uint i=v.size()-1; i>0; i--) {
uint j = (rand() % i);
T tmp = v[i];
v[i] = v[j];
v[j] = tmp;
T2 tmp2 = w[i];
w[i] = w[j];
w[j] = tmp2;
}
}
bool isWhitespace(std::string str) {
for(uint i=0; i<str.size(); i++) {
if (!isspace(str[i]))
return false;
}
return true;
}
vector<string> split(const string &s, char delim) {
stringstream ss(s);
string item;
vector<string> elems;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
string filename(string filepath) {
unsigned int a = filepath.rfind('.');
unsigned int b = filepath.rfind('/');
string fname = filepath.substr(b+1,a-b-1);
return fname;
}
bool conditional_mkdir(const string &path) {
struct stat s;
if (stat(path.c_str(), &s) == 0 && S_ISDIR(s.st_mode)) return true;
string mkcmd = "mkdir -p " + path;
system(mkcmd.c_str());
return true;
}
#endif