forked from tjdolan121/TextClassifierLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVCClassifier.cpp
More file actions
249 lines (209 loc) · 6.85 KB
/
SVCClassifier.cpp
File metadata and controls
249 lines (209 loc) · 6.85 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
/*++
Revision History:
Date: Jun 28, 2024.
Author: Rajas Chavadekar.
Desc: Created.
--*/
#include "SVCClassifier.h"
#include <fstream>
#include <iostream>
#include "GlobalData.h"
SVCClassifier::SVCClassifier(BaseVectorizer* pvec)
{
pVec = pvec;
}
SVCClassifier::~SVCClassifier()
{
delete pVec;
}
double SVCClassifier::predict_margin(const std::vector<double>& features) const
{
double margin = bias;
for (ml_size_t i = 0; i < features.size(); ++i)
{
margin += weights[i] * features[i];
}
return margin;
}
void SVCClassifier::setHyperparameters(std::string hyperparameters)
{
std::string token;
std::istringstream tokenStream(hyperparameters);
// "bias=0.0,epochs=15,learning_rate=0.01,l1_regularization_param=0.005,l2_regularization_param=0.0"
bias = 0.0;
epochs = 15;
learning_rate = 0.01;
l1_regularization_param = 0.005;
l2_regularization_param = 0.0;
pVec->ngrams = 1;
while (std::getline(tokenStream, token, ',')) {
std::istringstream pairStream(token);
std::string key;
double value;
if (std::getline(pairStream, key, '=') && pairStream >> value) {
cout << key << " = " << value << endl;
if (key == "ngrams") {
pVec->ngrams = value;
}
else if (key == "minfrequency") {
minfrequency = value;
}
else if (key == "bias") {
bias = value;
}
else if (key == "epochs") {
epochs = value;
}
else if (key == "learning_rate") {
learning_rate = value;
}
else if (key == "l1_regularization_param") {
l1_regularization_param = value;
}
else if (key == "l2_regularization_param") {
l2_regularization_param = value;
}
}
}
}
void SVCClassifier::fit(std::string abs_filepath_to_features, std::string abs_filepath_to_labels)
{
if (minfrequency > 0)
{
pVec->scanForSparseHistogram(abs_filepath_to_features, minfrequency);
}
pVec->fit(abs_filepath_to_features, abs_filepath_to_labels);
ml_size_t num_features = pVec->word_array.size();
weights.assign(num_features, 0.0);
std::vector<std::shared_ptr<Sentence>> sentences = pVec->sentences;
std::vector<int> labels(sentences.size());
std::ifstream label_file(abs_filepath_to_labels);
std::string label;
for (ml_size_t i = 0; i < labels.size(); ++i)
{
label_file >> labels[i];
// Convert labels to +1 or -1 for SVM
labels[i] = labels[i] == 1 ? 1 : -1;
}
label_file.close();
for (int epoch = 0; epoch < epochs; ++epoch)
{
for (ml_size_t i = 0; i < sentences.size(); ++i)
{
std::vector<double> features;
const auto& sentence_map = sentences[i]->sentence_map;
features = pVec->getFrequencies(sentence_map);
double y_true = labels[i];
double margin = predict_margin(features);
if (y_true * margin < 1)
{
for (ml_size_t j = 0; j < features.size(); ++j)
{
weights[j] += learning_rate * (y_true * features[j] - l1_regularization_param * (weights[j] > 0 ? 1 : -1) - 2 * l2_regularization_param * weights[j]);
}
bias += learning_rate * y_true;
}
else
{
for (ml_size_t j = 0; j < features.size(); ++j)
{
weights[j] += learning_rate * (-l1_regularization_param * (weights[j] > 0 ? 1 : -1) - 2 * l2_regularization_param * weights[j]);
}
}
}
}
}
Prediction SVCClassifier::predict(std::string sentence, bool preprocess)
{
GlobalData vars;
Prediction result;
std::vector<string> processed_input = pVec->buildSentenceVector(sentence, preprocess);
std::vector<double> feature_vector = pVec->getSentenceFeatures(processed_input);
double margin = predict_margin(feature_vector);
result.probability = 1.0 / (1.0 + std::exp(-margin));
if (margin > 0)
{
result.label = vars.POS;
}
else
{
result.label = vars.NEG;
}
return result;
}
void SVCClassifier::predict(std::string abs_filepath_to_features, std::string abs_filepath_to_labels, bool preprocess)
{
std::ifstream in(abs_filepath_to_features);
std::ofstream out(abs_filepath_to_labels);
std::string feature_input;
if (!in)
{
std::cerr << "ERROR: Cannot open features file.\n";
return;
}
if (!out)
{
std::cerr << "ERROR: Cannot open labels file.\n";
return;
}
#ifdef BENCHMARK
double sumduration = 0.0;
double sumstrlen = 0.0;
ml_size_t num_rows = 0;
#endif
while (getline(in, feature_input))
{
#ifdef BENCHMARK
auto start = std::chrono::high_resolution_clock::now();
#endif
Prediction result = predict(feature_input, preprocess);
#ifdef BENCHMARK
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration = end - start;
double milliseconds = duration.count();
sumduration += milliseconds;
sumstrlen += feature_input.length();
num_rows++;
#endif
out << result.label << "," << result.probability << std::endl;
}
#ifdef BENCHMARK
double avgduration = sumduration / num_rows;
cout << "Average Time per Text = " << avgduration << " ms" << endl;
double avgstrlen = sumstrlen / num_rows;
cout << "Average Length of Text (chars) = " << avgstrlen << endl;
#endif
in.close();
out.close();
}
void SVCClassifier::save(const std::string& filename) const
{
std::ofstream outFile(filename, std::ios::binary);
if (!outFile.is_open())
{
std::cerr << "Failed to open file for writing." << std::endl;
return;
}
pVec->save(outFile);
ml_size_t weight_size = weights.size();
outFile.write(reinterpret_cast<const char*>(&weight_size), sizeof(weight_size));
outFile.write(reinterpret_cast<const char*>(weights.data()), weight_size * sizeof(double));
outFile.write(reinterpret_cast<const char*>(&bias), sizeof(bias));
outFile.close();
}
void SVCClassifier::load(const std::string& filename)
{
std::ifstream inFile(filename, std::ios::binary);
if (!inFile.is_open())
{
std::cerr << "Failed to open file for reading." << std::endl;
return;
}
pVec->load(inFile);
ml_size_t weight_size;
inFile.read(reinterpret_cast<char*>(&weight_size), sizeof(weight_size));
weights.resize(weight_size);
inFile.read(reinterpret_cast<char*>(weights.data()), weight_size * sizeof(double));
inFile.read(reinterpret_cast<char*>(&bias), sizeof(bias));
inFile.close();
}