(
intnum_features,
const double eta= 0.01f,
59std::cerr <<
"learning rate should be positive and nonzero" 61std::exit(EXIT_FAILURE);
69 for(
double&weight :
weights) weight = 1.f;
78 for(
inti = 0; i < ada.
weights.size(); i++) {
80 if(i < ada.
weights.size() - 1) {
95 int predict(
conststd::vector<double> &x,
double*out =
nullptr) {
103y = std::inner_product(x.begin(), x.end(),
weights.begin(), y);
105 if(out !=
nullptr) {
119 double fit(
conststd::vector<double> &x,
const int&y) {
126 intprediction_error = y - p;
127 doublecorrection_factor =
eta* prediction_error;
130 for(
inti = 0; i < x.size(); i++) {
131 weights[i] += correction_factor * x[i];
133 weights[x.size()] += correction_factor;
135 returncorrection_factor;
145 void fit(std::array<std::vector<double>, N>
const&X,
146std::array<int, N>
const&Y) {
147 doubleavg_pred_error = 1.f;
152avg_pred_error = 0.f;
155 for(
inti = 0; i < N; i++) {
156 doubleerr =
fit(X[i], Y[i]);
157avg_pred_error += std::abs(err);
163std::cout <<
"\tIter "<< iter <<
": Training weights: "<< *
this 164<<
"\tAvg error: "<< avg_pred_error << std::endl;
168std::cout <<
"Converged after "<< iter <<
" iterations." 171std::cout <<
"Did not converge after "<< iter <<
" iterations." 197 if(x.size() != (
weights.size() - 1)) {
198std::cerr << __func__ <<
": " 199<<
"Number of features in x does not match the feature " 200 "dimension in model!" 229std::array<std::vector<double>, N> X = {
230std::vector<double>({0, 1}), std::vector<double>({1, -2}),
231std::vector<double>({2, 3}), std::vector<double>({3, -1}),
232std::vector<double>({4, 1}), std::vector<double>({6, -5}),
233std::vector<double>({-7, -3}), std::vector<double>({-8, 5}),
234std::vector<double>({-9, 2}), std::vector<double>({-10, -15})};
235std::array<int, N> y = {1, -1, 1, -1, -1,
238std::cout <<
"------- Test 1 -------"<< std::endl;
239std::cout <<
"Model before fit: "<< ada << std::endl;
242std::cout <<
"Model after fit: "<< ada << std::endl;
244 intpredict = ada.
predict({5, -3});
245std::cout <<
"Predict for x=(5,-3): "<< predict;
246assert(predict == -1);
247std::cout <<
" ...passed"<< std::endl;
249predict = ada.
predict({5, 8});
250std::cout <<
"Predict for x=(5,8): "<< predict;
251assert(predict == 1);
252std::cout <<
" ...passed"<< std::endl;
267std::array<std::vector<double>, N> X;
268std::array<int, N> Y{};
273 intrange2 = range >> 1;
274 for(
inti = 0; i < N; i++) {
275 doublex0 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
276 doublex1 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
277X[i] = std::vector<double>({x0, x1});
278Y[i] = (x0 + 3. * x1) > -1 ? 1 : -1;
281std::cout <<
"------- Test 2 -------"<< std::endl;
282std::cout <<
"Model before fit: "<< ada << std::endl;
285std::cout <<
"Model after fit: "<< ada << std::endl;
287 intN_test_cases = 5;
288 for(
inti = 0; i < N_test_cases; i++) {
289 doublex0 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
290 doublex1 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
292 intpredict = ada.
predict({x0, x1});
294std::cout <<
"Predict for x=("<< x0 <<
","<< x1 <<
"): "<< predict;
296 intexpected_val = (x0 + 3. * x1) > -1 ? 1 : -1;
297assert(predict == expected_val);
298std::cout <<
" ...passed"<< std::endl;
318std::array<std::vector<double>, N> X;
319std::array<int, N> Y{};
324 intrange2 = range >> 1;
325 for(
inti = 0; i < N; i++) {
326 doublex0 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
327 doublex1 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
328 doublex2 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
329X[i] = std::vector<double>({x0, x1, x2, x0 * x0, x1 * x1, x2 * x2});
330Y[i] = ((x0 * x0) + (x1 * x1) + (x2 * x2)) <= 1.f ? 1 : -1;
333std::cout <<
"------- Test 3 -------"<< std::endl;
334std::cout <<
"Model before fit: "<< ada << std::endl;
337std::cout <<
"Model after fit: "<< ada << std::endl;
339 intN_test_cases = 5;
340 for(
inti = 0; i < N_test_cases; i++) {
341 doublex0 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
342 doublex1 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
343 doublex2 = (
static_cast<double>(std::rand() % range) - range2) / 100.f;
345 intpredict = ada.
predict({x0, x1, x2, x0 * x0, x1 * x1, x2 * x2});
347std::cout <<
"Predict for x=("<< x0 <<
","<< x1 <<
","<< x2
350 intexpected_val = ((x0 * x0) + (x1 * x1) + (x2 * x2)) <= 1.f ? 1 : -1;
351assert(predict == expected_val);
352std::cout <<
" ...passed"<< std::endl;
357int main(
intargc,
char**argv) {
358std::srand(std::time(
nullptr));
362eta = strtof(argv[1],
nullptr);
367std::cout <<
"Press ENTER to continue..."<< std::endl;
372std::cout <<
"Press ENTER to continue..."<< std::endl;
adaline(int num_features, const double eta=0.01f, const double accuracy=1e-5)
adaline(int num_features, const double eta=0.01f, const double accuracy=1e-5)
const double eta
learning rate of the algorithm
std::vector< double > weights
weights of the neural network
double fit(const std::vector< double > &x, const int &y)
void fit(std::array< std::vector< double >, N > const &X, std::array< int, N > const &Y)
const double accuracy
model fit convergence accuracy
int predict(const std::vector< double > &x, double *out=nullptr)
bool check_size_match(const std::vector< double > &x)
friend std::ostream & operator<<(std::ostream &out, const adaline &ada)
static void test2()
Self-implementations, 2nd test.
static void test1()
Self-test implementations, 1st test.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4