-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchgradientdescent.cpp
More file actions
146 lines (131 loc) · 3.91 KB
/
batchgradientdescent.cpp
File metadata and controls
146 lines (131 loc) · 3.91 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
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
using std::cout, std::endl, std::cin, std::vector, std::string, std::stod;
static vector<vector<double>> X; // features
static vector<double> Y; // targets
static vector<double> theta; // parameters
// Helper method
vector<string> split_string(string str, char delimiter){
vector<string> result;
string current = "";
for(int i = 0; i < str.size(); ++i){
if(str[i] == delimiter){
if(current != ""){
result.push_back(current);
current = "";
}
continue;
}
current += str[i];
}
if(current.size() != 0){
result.push_back(current);
}
return result;
}
void get_training_data() {
int num_of_training_ex = 0;
string x_values = "";
string y_value = "";
while (true) {
num_of_training_ex++;
cout << "Enter training example x-value(s) #" << num_of_training_ex << ", seperated by a comma (Type 'Q' to finish): ";
cin >> x_values;
if (x_values == "Q") {
break;
}
vector<string> x_values_vec = split_string(x_values, ',');
vector<double> x_values_to_push = {1};
for (int i = 0; i < x_values_vec.size(); ++i) {
x_values_to_push.push_back(stod(x_values_vec[i]));
}
X.push_back(x_values_to_push);
cout << endl;
cout << "Enter training example y-value #" << num_of_training_ex << ": ";
cin >> y_value;
Y.push_back(stod(y_value));
}
for (int i = 0; i < X[0].size(); ++i) { //setting thetas to 0's
theta.push_back(0);
}
}
// Hypothesis function
double h(vector<double> &x){
double result = 0;
for (int i = 0; i < theta.size(); ++i){
result += theta[i] * x[i];
}
return result;
}
// Cost/error function
double J(){
double result = 0;
for (int i = 0; i < X.size(); ++i){
result += pow(h(X[i]) - Y[i], 2);
}
return result/(2 * X.size());
}
void find_optimal_theta_parameters() {
double learning_rate = 0.01;
bool is_converged = false;
int iters = 0;
double epsilon = 0.00000001;
while (!is_converged) {
double old_cost = J();
vector<double> temp(theta.size(), 0);
for (int j = 0; j < theta.size(); ++j) {
double result = 0;
for (int i = 0; i < X.size(); ++i) {
result += (h(X[i]) - Y[i]) * X[i][j];
}
result /= (2 * X.size());
temp[j] = theta[j] - learning_rate * result;
}
theta = temp;
iters++;
if (iters == 100000 || fabs(J() - old_cost) <= epsilon) {
is_converged = true;
}
}
}
void print_equation() {
cout << "Predicted equation: Y = ";
for (int i = 0; i < theta.size(); ++i) {
if (i == 0) {
cout << theta[i] << "+ ";
}else if (i == theta.size() - 1){
cout << theta[i] << "x" << i << endl;;
}else{
cout << theta[i] << "x" << i << "+ ";
}
}
}
void print_error() {
cout << "Total error: " << J() << endl;
}
void predict() { // Asks for inputs and displays coressponding prediction
string x_values;
while (true) {
cout << "Enter your x-values that you want to predict for (comma is delimiter, Q to quit): ";
cin >> x_values;
if (x_values == "Q") {
break;
}
vector<string> x_values_string_vec = split_string(x_values, ',');
vector<double> x_values_vec = {1};
for (int i = 0; i < x_values_string_vec.size(); ++i) {
x_values_vec.push_back(stod(x_values_string_vec[i]));
}
cout << "Model's prediction: " << h(x_values_vec) << endl;
}
}
int main() {
get_training_data();
find_optimal_theta_parameters();
print_equation();
print_error();
predict();
return 0;
}