-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStochastic_Gradient_Descent.cpp
More file actions
235 lines (201 loc) · 6.28 KB
/
Stochastic_Gradient_Descent.cpp
File metadata and controls
235 lines (201 loc) · 6.28 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
#include <iostream>
#include <math.h>
#include <vector>
#include <limits>
#include <fstream>
#include <ios>
#include <sstream>
using namespace std;
class Data
{
vector<string> feature_names;
vector<vector<float>> features;
vector<float> target;
void readCSV()
{
fstream fin;
fin.open(".\\train_dataset2.csv", ios::in);
vector<float> temp_vec;
string word, line;
int ctr = 0;
while (fin >> line)
{
temp_vec.clear();
stringstream s(line);
if (ctr == 0)
{
while (getline(s, word, ','))
{
feature_names.push_back(word);
}
ctr++;
continue;
}
while (getline(s, word, ','))
{
float val = stof(word);
temp_vec.push_back(val);
}
features.push_back(temp_vec);
}
for (int i = 0; i < features.size(); i++)
{
target.push_back(features[i].back());
features[i].pop_back();
}
}
friend class LinearRegression;
};
class LinearRegression : private Data
{
float bias, bias_gradient;
vector<float> weights, weight_gradients;
// Function to calculate the loss
float meanSquaredError(float y_true, float y_predicted, int total_samples)
{
float loss = pow((y_true - y_predicted), 2) / total_samples;
return loss;
}
void initializeWeightsAndGradients()
{
for (int i = 0; i < features[0].size(); i++)
{
weights.push_back(1);
weight_gradients.push_back(0);
}
}
float computingPrediction(int i)
{
float y_predicted = 0;
for (int j = 0; j < weights.size(); j++)
{
y_predicted += (weights[j] * features[i][j]);
}
y_predicted += this->bias;
return y_predicted;
}
void updatingGradients(float y_predicted, int row)
{
this->bias_gradient = 0;
weight_gradients.clear();
this->bias_gradient += ((-2 * (target[row] - y_predicted)) / target.size());
for (int col = 0; col < features[0].size(); col++)
{
weight_gradients[col] += ((-2 * ((target[row] - y_predicted) * features[row][col])) / target.size());
}
}
void updatingWeightsAndBias(float learning_rate)
{
for (int j = 0; j < weights.size(); j++)
{
weights[j] = weights[j] - ((learning_rate) * (weight_gradients[j]));
}
this->bias = this->bias - ((learning_rate) * (bias_gradient));
}
void gradientDescent(float learning_rate, int epochs)
{
int total_samples = target.size();
float current_loss = __FLT_MAX__;
initializeWeightsAndGradients();
// Run for the number of epochs specified to train the model
for (int e = 0; e < epochs; e++)
{
for (int i = 0; i < total_samples; i++)
{
// Initializing the weights and bias
float y_predicted = computingPrediction(i);
// Calculate the loss
float loss = meanSquaredError(target[i], y_predicted, total_samples);
// cout << "Delta: " << target[i]-y_predicted << endl;
// Compare the present loss with the previous
// If less than the previous then try to further optimize
// Else skip optimization
if (loss < current_loss)
{
updatingGradients(y_predicted, i);
updatingWeightsAndBias(learning_rate);
current_loss = loss;
}
// Here print the weights and bias and loss for every iteration
}
cout << "Epoch: " << e << "\t"
<< "Loss: " << current_loss << endl;
}
cout << "Training complete" << endl;
cout << "=============================================================" << endl << endl;
}
float predict(int size, vector<float> features)
{
float y_predicted = 0;
for (int i = 0; i < weights.size(); i++)
{
y_predicted += (weights[i] * features[i]);
}
y_predicted += this->bias;
return y_predicted;
}
public:
LinearRegression()
{
this->bias = 0;
readCSV();
}
// Enter the learning rate based on how fast you want your model to learn
void fit(float learning_rate, int epochs)
{
this->gradientDescent(learning_rate, epochs);
}
// Returns the predicted value on providing the necessary info
float predict()
{
vector<float> user_features(features[0].size());
for (int i = 0; i < features[0].size(); i++)
{
cout << "Enter " << feature_names[i] << ": ";
cin >> user_features[i];
}
float result = predict(user_features.size(), user_features);
cout << "Predicted Value: " << fixed <<result << endl << endl;
return result;
}
void score()
{
float ctr = 0;
for (int i = 0; i < target.size(); i++)
{
float predicted_value = predict(features[0].size(), features[i]);
float difference = target[i] * 0.75;
float ul = target[i] + difference;
float ll = target[i] - difference;
if ((predicted_value > ll) && (predicted_value < ul))
{
ctr++;
}
}
float score = ctr / target.size();
cout << "Score: " << score << endl
<< "Matching records: " << ctr << endl << endl;
}
void showWeightsAndBias()
{
for (int i = 0; i < weights.size(); i++)
{
cout << "Weight " << i + 1 << ": " << weights[i] << endl;
}
cout << "Bias: " << this->bias;
}
};
int main()
{
LinearRegression nn;
float learning_rate = 0.0001;
int epoch = 100;
nn.fit(learning_rate, epoch);
cout << "Displaying Model Performance: " << endl;
nn.score();
cout<< "Computing a prediction for input data: " << endl;
nn.predict();
cout << "Showing current weights and bias(s) of the model: " << endl;
nn.showWeightsAndBias();
return 0;
}