44
edits
Changes
→Code
#include <random>
#include <chrono>
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;
if (argc != 4) {
N = 1000;
x[i] = x_dist(generator);
y[i] = m_real[i] * x[i] + b_real[i];
}
// estimated b, m
double b = 0;
double m = 0;
// gradient descent
tStop = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tStop - tStart);
delete[] y;
return 0;
}
</source>
====Performance====