-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlr.cpp
176 lines (156 loc) · 3.99 KB
/
mlr.cpp
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
// SWAMI KARUPPASWAMI THUNNAI
#include <iomanip>
#include <sstream>
#include "mlr.h"
#include "matrix.h"
#include "json.h"
using json = nlohmann::json;
LinearRegression::LinearRegression()
{
verbose = DEBUG;
}
LinearRegression::LinearRegression(const LinearRegression& copyFromThis)
{
this->X = copyFromThis.X;
this->y = copyFromThis.y;
this->verbose = copyFromThis.verbose;
}
LinearRegression::LinearRegression(LinearRegression&& moveFromThis)
{
this->X = moveFromThis.X;
this->y = moveFromThis.y;
this->verbose = moveFromThis.verbose;
moveFromThis.X.clear();
moveFromThis.y.clear();
}
LinearRegression::LinearRegression(std::string model_name)
{
std::ifstream file;
file.open(model_name);
if (!file.is_open()) throw "Model cannot be loaded because it cannot be opened!";
json j;
file >> j;
file.close();
std::vector<double> b = j["bias"];
bias = b;
}
LinearRegression& LinearRegression::operator=(const LinearRegression ©FromThis)
{
this->X = copyFromThis.X;
this->y = copyFromThis.y;
this->verbose = copyFromThis.verbose;
return *this;
}
LinearRegression& LinearRegression::operator=(LinearRegression &&moveFromThis)
{
this->X = moveFromThis.X;
this->y = moveFromThis.y;
moveFromThis.X.clear();
moveFromThis.y.clear();
return *this;
}
void LinearRegression::print(std::string message)
{
if (verbose) std::cout << message << "\n";
}
void LinearRegression::fit()
{
// Adding bias to X
for (unsigned long int i = 0; i < X.size(); i++)
{
std::vector<double> row;
row.push_back(1);
for (double j : X[i])
{
row.push_back(j);
}
X[i] = row;
}
// X'
std::vector<std::vector<double>> X_transpose;
// X'X
std::vector<std::vector<double>> X_transpose_X;
matrix<double> mat;
print("Finding the transpose");
X_transpose = mat.transpose(X);
print("Finding X'X");
X_transpose_X = mat.mul(X_transpose, X);
print("Found!");
std::vector<double> rows(X_transpose_X[0].size(), 1);
// (X'X)-1
std::vector<std::vector<double>> inverse_of_X_transpose_X(X_transpose_X.size(), rows);
// find the inverse
print("Finding (X'X)^-1");
// Print the matrix shapes for debug messages
std::stringstream s1;
s1 << "Shape of X transpose X: " << X_transpose_X.size() << "," << X_transpose_X[0].size();
print(s1.str());
std::stringstream s2;
s2 << "Shape of inverse of X transpose X: " << inverse_of_X_transpose_X.size() << "," << inverse_of_X_transpose_X[0].size();
print(s2.str());
mat.inverse(X_transpose_X, inverse_of_X_transpose_X);
// Reshape y
print("Reshaping y");
std::vector<std::vector<double>> y_reshaped;
for (double i : y)
{
std::vector<double> row;
row.push_back(i);
y_reshaped.push_back(row);
}
std::vector<std::vector<double>> X_transpose_y = mat.mul(X_transpose, y_reshaped);
std::vector<std::vector<double>> b = mat.mul(inverse_of_X_transpose_X, X_transpose_y);
for (std::vector<double> i : b)
{
bias.push_back(i[0]);
}
if(TrainingFinish)
{
TrainingFinish(true);
std::cout<<"connected.."<<std::endl;
}
print("Found");
}
double LinearRegression::predict(std::vector<double> test)
{
double prediction = 0.0;
prediction += bias[0];
for (unsigned long int i = 0; i < test.size(); i++)
{
double value = bias[i + 1] * test[i];
prediction += value;
}
return prediction;
}
void LinearRegression::save_model(std::string model_name)
{
json j;
j["bias"] = bias;
std::ofstream file;
file.open(model_name);
if (file.is_open())
{
file << std::setw(4) << j << std::endl;
file.close();
}
else
{
throw "File cannot be opened for saving the model. May be the file is opened in some other place or you might not have proper permissions.";
}
}
std::vector<double> LinearRegression::get_bias()
{
return bias;
}
void LinearRegression::Train()
{
std::lock_guard<std::mutex> LG(_trainingMutex);
X = GetMultipleInput();
y = GetOutput();
TrainFuture = std::async(std::launch::async,&LinearRegression::fit,this);
}
std::vector<double> LinearRegression::Predict(std::vector<double> predictThis)
{
std::lock_guard<std::mutex> LG(_trainingMutex);
return {predict(predictThis)};
}