Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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<int N>
void fit (std::vector< double > const (&X)[N], const int *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::ostreamoperator<< (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 
)
inline

Default constructor

Parameters
[in]num_featuresnumber of features present
[in]etalearning rate (optional, default=0.1)
[in]convergenceaccuracy (optional, default= \(1\times10^{-5}\))
55  : eta(eta), accuracy(accuracy) {
56  if (eta <= 0) {
57  std::cerr << "learning rate should be positive and nonzero"
58  << std::endl;
59  std::exit(EXIT_FAILURE);
60  }
61 
63  num_features +
64  1); // additional weight is for the constant bias term
65 
66  // initialize with random weights in the range [-50, 49]
67  for (int i = 0; i < weights.size(); i++) weights[i] = 1.f;
68  // weights[i] = (static_cast<double>(std::rand() % 100) - 50);
69  }
Here is the call graph for this function:

Member Function Documentation

◆ 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]xfecture vector to check
Returns
true size matches
false size does not match
176  {
177  if (x.size() != (weights.size() - 1)) {
178  std::cerr << __func__ << ": "
179  << "Number of features in x does not match the feature "
180  "dimension in model!"
181  << std::endl;
182  return false;
183  }
184  return true;
185  }
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]xfeature vector
[in]yknown output value
Returns
correction factor
112  {
113  if (!check_size_match(x))
114  return 0;
115 
116  /* output of the model with current weights */
117  int p = predict(x);
118  int prediction_error = y - p; // error in estimation
119  double correction_factor = eta * prediction_error;
120 
121  /* update each weight, the last weight is the bias term */
122  for (int i = 0; i < x.size(); i++) {
123  weights[i] += correction_factor * x[i];
124  }
125  weights[x.size()] += correction_factor; // update bias
126 
127  return correction_factor;
128  }
Here is the call graph for this function:

◆ 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]Xarray of feature vector
[in]yknown output value for each feature vector
136  {
137  double avg_pred_error = 1.f;
138 
139  int iter;
140  for (iter = 0; (iter < MAX_ITER) && (avg_pred_error > accuracy);
141  iter++) {
142  avg_pred_error = 0.f;
143 
144  // perform fit for each sample
145  for (int i = 0; i < N; i++) {
146  double err = fit(X[i], y[i]);
147  avg_pred_error += std::abs(err);
148  }
149  avg_pred_error /= N;
150 
151  // Print updates every 200th iteration
152  // if (iter % 100 == 0)
153  std::cout << "\tIter " << iter << ": Training weights: " << *this
154  << "\tAvg error: " << avg_pred_error << std::endl;
155  }
156 
157  if (iter < MAX_ITER)
158 
159  std::cout << "Converged after " << iter << " iterations."
160  << std::endl;
161  else
162  std::cout << "Did not converge after " << iter << " iterations."
163  << std::endl;
164  }

◆ 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]xinput vector
[out]outoptional argument to return neuron output before applying activation function (optional, nullptr to ignore)
Returns
model prediction output
92  {
93  if (!check_size_match(x))
94  return 0;
95 
96  double y = weights.back(); // assign bias value
97 
98  // for (int i = 0; i < x.size(); i++) y += x[i] * weights[i];
99  y = std::inner_product(x.begin(), x.end(), weights.begin(), y);
100 
101  if (out != nullptr) // if out variable is provided
102  *out = y;
103 
104  return activation(y); // quantizer: apply ADALINE threshold function
105  }
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

74  {
75  out << "<";
76  for (int i = 0; i < ada.weights.size(); i++) {
77  out << ada.weights[i];
78  if (i < ada.weights.size() - 1)
79  out << ", ";
80  }
81  out << ">";
82  return out;
83  }

The documentation for this class was generated from the following file:
std::inner_product
T inner_product(T... args)
f
uint64_t f[MAX]
Definition: fibonacci_fast.cpp:27
std::vector< double >
std::vector::size
T size(T... args)
machine_learning::adaline::fit
double fit(const std::vector< double > &x, const int &y)
Definition: adaline_learning.cpp:112
std::vector::back
T back(T... args)
machine_learning::adaline::predict
int predict(const std::vector< double > &x, double *out=nullptr)
Definition: adaline_learning.cpp:92
machine_learning::adaline::check_size_match
bool check_size_match(const std::vector< double > &x)
Definition: adaline_learning.cpp:176
std::cerr
machine_learning::adaline::eta
const double eta
learning rate of the algorithm
Definition: adaline_learning.cpp:187
machine_learning::adaline::weights
std::vector< double > weights
weights of the neural network
Definition: adaline_learning.cpp:189
std::endl
T endl(T... args)
std::vector::begin
T begin(T... args)
std::vector::end
T end(T... args)
machine_learning::adaline::accuracy
const double accuracy
model fit convergence accuracy
Definition: adaline_learning.cpp:188
std::exit
T exit(T... args)