|
| adaline (int num_features, const double eta=0.01f, const double accuracy=1e-5) |
|
int | predict (const std::vector< double > &x, double *out=nullptr) |
|
double | fit (const std::vector< double > &x, const int &y) |
|
template<size_t N> |
void | fit (std::array< std::vector< double >, N > const &X, std::array< int, N > const &Y) |
|
int | activation (double x) |
|
|
const double | eta |
| learning rate of the algorithm
|
|
const double | accuracy |
| model fit convergence accuracy
|
|
std::vector< double > | weights |
| weights of the neural network
|
|
Definition at line 46 of file adaline_learning.cpp.
◆ adaline()
machine_learning::adaline::adaline |
( |
int | num_features, |
|
|
const double | eta = 0.01f, |
|
|
const double | accuracy = 1e-5 ) |
|
inlineexplicit |
Default constructor
- Parameters
-
[in] | num_features | number of features present |
[in] | eta | learning rate (optional, default=0.1) |
[in] | convergence | accuracy (optional, default= \(1\times10^{-5}\)) |
Definition at line 55 of file adaline_learning.cpp.
59 std::cerr << "learning rate should be positive and nonzero"
60 << std::endl;
61 std::exit(EXIT_FAILURE);
62 }
63
65 num_features +
66 1);
67
68
69 for (
double &weight :
weights) weight = 1.f;
70
71 }
const double eta
learning rate of the algorithm
std::vector< double > weights
weights of the neural network
const double accuracy
model fit convergence accuracy
◆ activation()
int machine_learning::adaline::activation |
( |
double | x | ) |
|
|
inline |
Defines activation function as Heaviside's step function.
\[
f(x) = \begin{cases}
-1 & \forall x \le 0\\
1 & \forall x > 0
\end{cases}
\]
- Parameters
-
x | input value to apply activation on |
- Returns
- activation output
Definition at line 186 of file adaline_learning.cpp.
186{ return x > 0 ? 1 : -1; }
◆ check_size_match()
bool machine_learning::adaline::check_size_match |
( |
const std::vector< double > & | x | ) |
|
|
inlineprivate |
convenient function to check if input feature vector size matches the model weights size
- Parameters
-
[in] | x | fecture vector to check |
- Returns
true
size matches
-
false
size does not match
Definition at line 196 of file adaline_learning.cpp.
196 {
197 if (x.size() != (
weights.size() - 1)) {
198 std::cerr << __func__ << ": "
199 << "Number of features in x does not match the feature "
200 "dimension in model!"
201 << std::endl;
202 return false;
203 }
204 return true;
205 }
◆ fit() [1/2]
double machine_learning::adaline::fit |
( |
const std::vector< double > & | x, |
|
|
const int & | y ) |
|
inline |
Update the weights of the model using supervised learning for one feature vector
- Parameters
-
[in] | x | feature vector |
[in] | y | known output value |
- Returns
- correction factor
Definition at line 119 of file adaline_learning.cpp.
119 {
121 return 0;
122 }
123
124
126 int prediction_error = y - p;
127 double correction_factor =
eta * prediction_error;
128
129
130 for (int i = 0; i < x.size(); i++) {
131 weights[i] += correction_factor * x[i];
132 }
133 weights[x.size()] += correction_factor;
134
135 return correction_factor;
136 }
int predict(const std::vector< double > &x, double *out=nullptr)
bool check_size_match(const std::vector< double > &x)
◆ fit() [2/2]
template<size_t N>
void machine_learning::adaline::fit |
( |
std::array< std::vector< double >, N > const & | X, |
|
|
std::array< int, N > const & | Y ) |
|
inline |
Update the weights of the model using supervised learning for an array of vectors.
- Parameters
-
[in] | X | array of feature vector |
[in] | y | known output value for each feature vector |
Definition at line 145 of file adaline_learning.cpp.
146 {
147 double avg_pred_error = 1.f;
148
149 int iter = 0;
151 iter++) {
152 avg_pred_error = 0.f;
153
154
155 for (
int i = 0; i <
N; i++) {
156 double err =
fit(X[i], Y[i]);
157 avg_pred_error += std::abs(err);
158 }
160
161
162
163 std::cout << "\tIter " << iter << ": Training weights: " << *this
164 << "\tAvg error: " << avg_pred_error << std::endl;
165 }
166
168 std::cout << "Converged after " << iter << " iterations."
169 << std::endl;
170 } else {
171 std::cout << "Did not converge after " << iter << " iterations."
172 << std::endl;
173 }
174 }
double fit(const std::vector< double > &x, const int &y)
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
◆ predict()
int machine_learning::adaline::predict |
( |
const std::vector< double > & | x, |
|
|
double * | out = nullptr ) |
|
inline |
predict the output of the model for given set of features
- Parameters
-
[in] | x | input vector |
[out] | out | optional argument to return neuron output before applying activation function (optional, nullptr to ignore) |
- Returns
- model prediction output
Definition at line 95 of file adaline_learning.cpp.
95 {
97 return 0;
98 }
99
101
102
103 y = std::inner_product(x.begin(), x.end(),
weights.begin(), y);
104
105 if (out != nullptr) {
106 *out = y;
107 }
108
110 }
◆ operator<<
std::ostream & operator<< |
( |
std::ostream & | out, |
|
|
const adaline & | ada ) |
|
friend |
Operator to print the weights of the model
Definition at line 76 of file adaline_learning.cpp.
76 {
77 out << "<";
78 for (
int i = 0; i < ada.
weights.size(); i++) {
80 if (i < ada.
weights.size() - 1) {
81 out << ", ";
82 }
83 }
84 out << ">";
85 return out;
86 }
◆ accuracy
const double machine_learning::adaline::accuracy |
|
private |
◆ eta
const double machine_learning::adaline::eta |
|
private |
◆ weights
std::vector<double> machine_learning::adaline::weights |
|
private |
The documentation for this class was generated from the following file: