|
const double | eta |
| learning rate of the algorithm
|
|
const double | accuracy |
| model fit convergence accuracy
|
|
std::vector< double > | weights |
| weights of the neural network
|
|
◆ 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}\)) |
59 std::cerr <<
"learning rate should be positive and nonzero"
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
Definition adaline_learning.cpp:207
std::vector< double > weights
weights of the neural network
Definition adaline_learning.cpp:209
const double accuracy
model fit convergence accuracy
Definition adaline_learning.cpp:208
◆ 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
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
196 {
199 << "Number of features in x does not match the feature "
200 "dimension in model!"
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
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 }
134
135 return correction_factor;
136 }
int predict(const std::vector< double > &x, double *out=nullptr)
Definition adaline_learning.cpp:95
bool check_size_match(const std::vector< double > &x)
Definition adaline_learning.cpp:196
◆ fit() [2/2]
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 |
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."
170 } else {
171 std::cout <<
"Did not converge after " << iter <<
" iterations."
173 }
174 }
double fit(const std::vector< double > &x, const int &y)
Definition adaline_learning.cpp:119
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
Definition sparse_table.cpp:47
constexpr int MAX_ITER
Definition adaline_learning.cpp:40
◆ 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
95 {
97 return 0;
98 }
99
101
102
104
105 if (out != nullptr) {
106 *out = y;
107 }
108
110 }
int activation(double x)
Definition adaline_learning.cpp:186
T inner_product(T... args)
◆ operator<<
Operator to print the weights of the model
76 {
77 out << "<";
81 out << ", ";
82 }
83 }
84 out << ">";
85 return out;
86 }
The documentation for this class was generated from the following file: