|
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"
69 for (
double &weight :
weights) weight = 1.f;
◆ 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
199 <<
"Number of features in x does not match the feature "
200 "dimension in model!"
◆ 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
126 int prediction_error = y - p;
127 double correction_factor =
eta * prediction_error;
130 for (
int i = 0; i < x.
size(); i++) {
131 weights[i] += correction_factor * x[i];
135 return correction_factor;
◆ 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 |
147 double avg_pred_error = 1.f;
152 avg_pred_error = 0.f;
155 for (
int i = 0; i < N; i++) {
156 double err =
fit(X[i], Y[i]);
157 avg_pred_error += std::abs(err);
163 std::cout <<
"\tIter " << iter <<
": Training weights: " << *
this
164 <<
"\tAvg error: " << avg_pred_error <<
std::endl;
168 std::cout <<
"Converged after " << iter <<
" iterations."
171 std::cout <<
"Did not converge after " << iter <<
" iterations."
◆ 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
105 if (out !=
nullptr) {
◆ operator<<
Operator to print the weights of the model
The documentation for this class was generated from the following file: