machine_learning::adaline Class Reference
Collaboration diagram for machine_learning::adaline:
[legend]

## Public Member Functions

| 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)

## Private Member Functions

bool | check_size_match (const std::vector< double > &x)

## Private Attributes

const double | eta
| learning rate of the algorithm
const double | accuracy
| model fit convergence accuracy
std::vector< double > | weights
| weights of the neural network

## Friends

std::ostream & | operator<< (std::ostream &out, const adaline &ada)

## Constructor & Destructor Documentation

## 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}\))

: eta(eta), accuracy(accuracy) {
if (eta <= 0) {
std::cerr << "learning rate should be positive and nonzero"
<< std::endl;
std::exit(EXIT_FAILURE);
}
weights = std::vector<double>(
num_features +
1); // additional weight is for the constant bias term
// initialize with random weights in the range [-50, 49]
for (double &weight : weights) weight = 1.f;
// weights[i] = (static_cast<double>(std::rand() % 100) - 50);
}
std::cerr
machine_learning::adaline::eta
const double eta
learning rate of the algorithm
Definition: adaline_learning.cpp:207
machine_learning::adaline::weights
std::vector< double > weights
weights of the neural network
Definition: adaline_learning.cpp:209
machine_learning::adaline::accuracy
const double accuracy
model fit convergence accuracy
Definition: adaline_learning.cpp:208
std::endl
T endl(T... args)
std::exit
T exit(T... args)
std::vector< double >
Here is the call graph for this function:

## Member Function Documentation

## 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

{ 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

{
if (x.size() != (weights.size() - 1)) {
std::cerr << __func__ << ": "
<< "Number of features in x does not match the feature "
"dimension in model!"
<< std::endl;
return false;
}
return true;
}
std::vector::size
T size(T... args)
Here is the call graph for this function:

## 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

{
if (!check_size_match(x)) {
return 0;
}
/* output of the model with current weights */
int p = predict(x);
int prediction_error = y - p; // error in estimation
double correction_factor = eta * prediction_error;
/* update each weight, the last weight is the bias term */
for (int i = 0; i < x.size(); i++) {
weights[i] += correction_factor * x[i];
}
weights[x.size()] += correction_factor; // update bias
return correction_factor;
}
machine_learning::adaline::predict
int predict(const std::vector< double > &x, double *out=nullptr)
Definition: adaline_learning.cpp:95
machine_learning::adaline::check_size_match
bool check_size_match(const std::vector< double > &x)
Definition: adaline_learning.cpp:196
Here is the call graph for this function:

## 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

{
double avg_pred_error = 1.f;
int iter = 0;
for (iter = 0; (iter < MAX_ITER) && (avg_pred_error > accuracy);
iter++) {
avg_pred_error = 0.f;
// perform fit for each sample
for (int i = 0; i < N; i++) {
double err = fit(X[i], Y[i]);
avg_pred_error += std::abs(err);
}
avg_pred_error /= N;
// Print updates every 200th iteration
// if (iter % 100 == 0)
std::cout << "\tIter " << iter << ": Training weights: " << *this
<< "\tAvg error: " << avg_pred_error << std::endl;
}
if (iter < MAX_ITER) {
std::cout << "Converged after " << iter << " iterations."
<< std::endl;
} else {
std::cout << "Did not converge after " << iter << " iterations."
<< std::endl;
}
}
machine_learning::adaline::fit
double fit(const std::vector< double > &x, const int &y)
Definition: adaline_learning.cpp:119
MAX_ITER
constexpr int MAX_ITER
Definition: adaline_learning.cpp:40
Here is the call graph for this function:

## 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

{
if (!check_size_match(x)) {
return 0;
}
double y = weights.back(); // assign bias value
// for (int i = 0; i < x.size(); i++) y += x[i] * weights[i];
y = std::inner_product(x.begin(), x.end(), weights.begin(), y);
if (out != nullptr) { // if out variable is provided
*out = y;
}
return activation(y); // quantizer: apply ADALINE threshold function
}
std::vector::back
T back(T... args)
std::vector::begin
T begin(T... args)
machine_learning::adaline::activation
int activation(double x)
Definition: adaline_learning.cpp:186
std::vector::end
T end(T... args)
std::inner_product
T inner_product(T... args)
Here is the call graph for this function:

## Friends And Related Function Documentation

## operator<<

std::ostream& operator<< | ( | std::ostream & | out,
|  | const adaline & | ada
| ) |  |

friend

Operator to print the weights of the model

{
out << "<";
for (int i = 0; i < ada.weights.size(); i++) {
out << ada.weights[i];
if (i < ada.weights.size() - 1) {
out << ", ";
}
}
out << ">";
return out;
}

The documentation for this class was generated from the following file:

- machine_learning/adaline_learning.cpp