Last active
October 9, 2021 20:54
-
-
Save mu578/89632c35ab9981da43dffacefac3a372 to your computer and use it in GitHub Desktop.
No comment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ML Reminder, educational. | |
static | |
float __linear_learner_gradient_descent( | |
const unsigned int n | |
, const float * x | |
, const float * y | |
, const float rate | |
, float * a | |
, float * b | |
) { | |
unsigned int i = 0; | |
float db = 0.0f; | |
float da = 0.0f; | |
float cost = 0.0f; | |
for (; i < n; i++) { | |
const float h = ((*a) * x[i] + (*b)) - y[i]; | |
cost += h * h; | |
db += h; | |
da += h * x[i]; | |
} | |
cost /= n; | |
(*a) = (*a) - rate * (da) / n; | |
(*b) = (*b) - rate * (db) / n; | |
return cost; | |
} | |
struct linear_learner | |
{ | |
unsigned int u_n; | |
unsigned int u_iter; | |
float u_rate; | |
float u_a; | |
float u_b; | |
const float * u_x; | |
const float * u_y; | |
float * u_cost; | |
} | |
int linear_learner_init(struct linear_learner * learner | |
, const unsigned int n | |
, const unsigned int niter | |
, const float rate | |
, const float * x | |
, const float * y | |
, float * cost | |
) { | |
//!# -- x[n] | |
//!# -- y[n] | |
//!# -- cost[niter] | |
//!# | |
const int have_learner = learner != NULL ? 1 : 0; | |
const int have_n = n > 2 && n < UINT_MAX ? 1 : 0; | |
const int have_niter = niter > 0 && n < UINT_MAX ? 1 : 0; | |
const int have_x = x != NULL ? 1 : 0; | |
const int have_y = y != NULL ? 1 : 0; | |
const int have_cost = cost != NULL ? 1 : 0; | |
const int have_parameters = ( | |
have_learner | |
&& have_n | |
&& have_niter | |
&& have_x && have_y | |
&& have_cost | |
) ? 1 : 0; | |
if (have_parameters) { | |
learner->u_n = n; | |
learner->u_iter = niter; | |
learner->u_rate = rate; | |
learner->u_a = 1.0f; | |
learner->u_b = 0.0f; | |
learner->u_x = x + 0; | |
learner->u_y = y + 0; | |
learner->u_cost = cost + 0; | |
return 0; | |
} | |
return -1; | |
} | |
int linear_learner_fit(struct linear_learner * learner) | |
{ | |
const int have_learner = learner != NULL ? 1 : 0; | |
unsigned int i = 0; | |
if (have_learner) { | |
for (; i < learner->u_iter; i++){ | |
learner->u_cost[i] = __linear_learner_gradient_descent( | |
learner->u_n | |
, learner->u_x | |
, learner->u_y | |
, learner->u_rate | |
, &learner->u_a | |
, &learner->u_b | |
); | |
} | |
return 0; | |
} | |
return -1; | |
} | |
const float * linear_learner_cost(const struct linear_learner * learner) | |
{ | |
const int have_learner = learner != NULL ? 1 : 0; | |
if (have_learner) { | |
return learner->u_cost; | |
} | |
return NULL; | |
} | |
float linear_learner_predict_one(const struct linear_learner * learner, const float x) | |
{ | |
const int have_learner = learner != NULL ? 1 : 0; | |
if (have_learner) { | |
return learner->u_a * x + learner->u_b; | |
} | |
return 0.0f; | |
} | |
/* EOF */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment