-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utils.hpp
145 lines (127 loc) · 3.82 KB
/
Utils.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
#pragma once
#include "Matrix.hpp"
#include <string>
#include <vector>
#include <fstream>
namespace Utils{
inline bool isSpace(const char& c){
return (c == ' ' || c == '\t');
}
inline void split(const std::string& str, std::vector<std::string>& res){
bool tok = false;
int beg = 0;
res.clear();
for (int i = 0, len = str.length(); i < len; ++i){
if (!tok && !Utils::isSpace(str[i])){
beg = i;
tok = true;
}
if (tok && (i == len-1 || Utils::isSpace(str[i]))){
tok = false;
res.push_back(isSpace(str[i]) ? str.substr(beg, i-beg) : str.substr(beg, i-beg+1));
}
}
}
inline void split(const std::string& str, std::vector<std::string>& res, const char sep){
bool tok = false;
int beg = 0;
res.clear();
for (int i = 0, len = str.length(); i < len; ++i){
if (!tok && str[i] != sep){
beg = i;
tok = true;
}
if (tok && (i == len-1 || str[i] == sep)){
tok = false;
res.push_back((str[i] == sep) ? str.substr(beg, i-beg) : str.substr(beg, i-beg+1));
}
}
}
inline void save(std::ofstream& ofs, const MatD& params){
Real val = 0.0;
for (int i = 0; i < params.cols(); ++i){
for (int j = 0; j < params.rows(); ++j){
val = params.coeff(j, i);
ofs.write((char*)&val, sizeof(Real));
}
}
}
inline void save(std::ofstream& ofs, const VecD& params){
Real val = 0.0;
for (int i = 0; i < params.rows(); ++i){
val = params.coeff(i, 0);
ofs.write((char*)&val, sizeof(Real));
}
}
inline void load(std::ifstream& ifs, MatD& params){
Real val = 0.0;
for (int i = 0; i < params.cols(); ++i){
for (int j = 0; j < params.rows(); ++j){
ifs.read((char*)&val, sizeof(Real));
params.coeffRef(j, i) = val;
}
}
}
inline void load(std::ifstream& ifs, VecD& params){
Real val = 0.0;
for (int i = 0; i < params.rows(); ++i){
ifs.read((char*)&val, sizeof(Real));
params.coeffRef(i, 0) = val;
}
}
inline void procArg(int argc, char** argv, int& embeddingDim, int& windowSize, int& numNegative, Real& learningRate, int& wminFreq, int& cminFreq, std::string& trainFile, std::string& outputFile){
for (int i = 1; i < argc; i+=2){
std::string arg = (std::string)argv[i];
if (arg == "-help"){
printf("### Options ###\n");
printf("-edim the dimensionality of embeddings (default: 100)\n");
printf("-window the context window size (default: 1)\n");
printf("-neg the number of negative samples for negative sampling learning (default: 15)\n");
printf("-lr the initial learning rate (default: 0.025)\n");
printf("-wminfreq the threshold to cut rare words (default: 10)\n");
printf("-cminfreq the threshold to cut rare char n-grams (default: 10)\n");
printf("-train the file name for training (default: ./sample_data/sample.txt)\n");
printf("-output the file name for output files (default: ./result)\n");
exit(1);
}
else if (arg == "-edim"){
assert(i+1 < argc);
embeddingDim = atoi(argv[i+1]);
assert(embeddingDim > 0);
}
else if (arg == "-window"){
assert(i+1 < argc);
windowSize = atoi(argv[i+1]);
assert(windowSize > 0);
}
else if (arg == "-neg"){
assert(i+1 < argc);
numNegative = atoi(argv[i+1]);
assert(numNegative > 0);
}
else if (arg == "-lr"){
assert(i+1 < argc);
learningRate = atof(argv[i+1]);
assert(learningRate > 0.0);
}
else if (arg == "-wminfreq"){
assert(i+1 < argc);
wminFreq = atoi(argv[i+1]);
assert(wminFreq > 0);
}
else if (arg == "-cminfreq"){
assert(i+1 < argc);
cminFreq = atoi(argv[i+1]);
assert(cminFreq > 0);
}
else if (arg == "-train"){
assert(i+1 < argc);
trainFile = (std::string)argv[i+1];
}
else if (arg == "-output"){
assert(i+1 < argc);
outputFile = (std::string)argv[i+1];
}
}
}
};