Skip to content

Commit f94a330

Browse files
committed
use STL's inner_product
1 parent 1c05a58 commit f94a330

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

machine_learning/adaline_learning.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <ctime>
2828
#include <iostream>
2929
#include <vector>
30+
#include <numeric>
3031

3132
#define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn
3233

@@ -81,7 +82,8 @@ class adaline {
8182

8283
double y = weights.back(); // assign bias value
8384

84-
for (int i = 0; i < x.size(); i++) y += x[i] * weights[i];
85+
// for (int i = 0; i < x.size(); i++) y += x[i] * weights[i];
86+
y = std::inner_product(x.begin(), x.end(), weights.begin(), y);
8587

8688
return y >= 0 ? 1 : -1; // quantizer: apply ADALINE threshold function
8789
}
@@ -229,7 +231,7 @@ void test2(double eta = 0.01) {
229231
Y[i] = (x0 + x1) > -1 ? 1 : -1;
230232
}
231233

232-
std::cout << "------- Test 1 -------" << std::endl;
234+
std::cout << "------- Test 2 -------" << std::endl;
233235
std::cout << "Model before fit: " << ada << std::endl;
234236

235237
ada.fit(X, Y);

0 commit comments

Comments
 (0)