Changes

Jump to: navigation, search

DPS921/Franky

11 bytes removed, 20:32, 25 November 2018
Code
#include <random>
#include <chrono>
#include <fstream>
void gradient_descent(const double x[], const double y[], const size_t& epoches, const size_t& N, const double& learn_rate, double& m, double& b) {
double p;
double err;
size_t idx;
for(size_t i = 0; i < epoches * N; i++) {
idx = i % N;
p = b + m * x[idx];
err = p - y[idx];
b = b - learn_rate * err;
m = m - learn_rate * err * x[idx];
}
}
int main(int argc, char* argv[]) {
size_t N;
double correct_b = 1.0;
double correct_m = 0.5;
std::ofstream ofs("points.csv");
ofs << "x,y" << std::endl;
if (argc != 4) {
N = 1000;
x[i] = x_dist(generator);
y[i] = m_real[i] * x[i] + b_real[i];
ofs<< x[i] << "," << y[i] << '\n';
}
ofs.close();
ofs.open("prediction.csv");
ofs << "m,b" << std::endl;
// estimated b, m
double b = 0;
double m = 0;
// gradient descent
forgradient_descent(size_t i = 0; i < x, y, epoches * , N; i++) { int idx = i % N; double p = b+m * x[idx]; double err = p - y[idx]; b = b - , learn_rate * err; , m = m - learn_rate * err * x[idx]; ofs << m << "," << b << '\n'); }
tStop = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tStop - tStart);
delete[] y;
return 0;
}
}
</source>
 
====Performance====
44
edits

Navigation menu