|
|
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 |
|
) |
| |
|
inline |
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}\)) |
57 std::cerr <<
"learning rate should be positive and nonzero"
◆ 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
179 <<
"Number of features in x does not match the feature "
180 "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
118 int prediction_error = y - p;
119 double correction_factor =
eta * prediction_error;
122 for (
int i = 0; i < x.
size(); i++) {
123 weights[i] += correction_factor * x[i];
127 return correction_factor;
◆ fit() [2/2]
template<int N>
| void machine_learning::adaline::fit |
( |
std::vector< double > const (&) |
X[N], |
|
|
const int * |
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 |
137 double avg_pred_error = 1.f;
140 for (iter = 0; (iter < MAX_ITER) && (avg_pred_error >
accuracy);
142 avg_pred_error = 0.f;
145 for (
int i = 0; i < N; i++) {
146 double err =
fit(X[i], y[i]);
147 avg_pred_error += std::abs(err);
153 std::cout <<
"\tIter " << iter <<
": Training weights: " << *
this
154 <<
"\tAvg error: " << avg_pred_error <<
std::endl;
159 std::cout <<
"Converged after " << iter <<
" iterations."
162 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
104 return activation(y);
◆ operator<<
Operator to print the weights of the model
The documentation for this class was generated from the following file: