Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
adaline_learning.cpp File Reference

Adaptive Linear Neuron (ADALINE) implementation More...

#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <numeric>
#include <vector>
Include dependency graph for adaline_learning.cpp:

Classes

class  machine_learning::adaline
 

Namespaces

 machine_learning
 Machine learning algorithms.
 

Macros

#define MAX_ITER   500
 

Functions

void test1 (double eta=0.01)
 
void test2 (double eta=0.01)
 
void test3 (double eta=0.01)
 
int main (int argc, char **argv)
 

Detailed Description

Adaptive Linear Neuron (ADALINE) implementation

Author
Krishna Vedala

source ADALINE is one of the first and simplest single layer artificial neural network. The algorithm essentially implements a linear function

\[ f\left(x_0,x_1,x_2,\ldots\right) = \sum_j x_jw_j+\theta \]

where \(x_j\) are the input features of a sample, \(w_j\) are the coefficients of the linear function and \(\theta\) is a constant. If we know the \(w_j\), then for any given set of features, \(y\) can be computed. Computing the \(w_j\) is a supervised learning algorithm wherein a set of features and their corresponding outputs are given and weights are computed using stochastic gradient descent method.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Main function

331  {
332  std::srand(std::time(nullptr)); // initialize random number generator
333 
334  double eta = 0.1; // default value of eta
335  if (argc == 2) // read eta value from commandline argument if present
336  eta = strtof(argv[1], nullptr);
337 
338  test1(eta);
339 
340  std::cout << "Press ENTER to continue..." << std::endl;
341  std::cin.get();
342 
343  test2(eta);
344 
345  std::cout << "Press ENTER to continue..." << std::endl;
346  std::cin.get();
347 
348  test3(eta);
349 
350  return 0;
351 }
Here is the call graph for this function:

◆ test1()

void test1 ( double  eta = 0.01)

test function to predict points in a 2D coordinate system above the line \(x=y\) as +1 and others as -1. Note that each point is defined by 2 values or 2 features.

Parameters
[in]etalearning rate (optional, default=0.01)
202  {
203  adaline ada(2, eta); // 2 features
204 
205  const int N = 10; // number of sample points
206 
207  std::vector<double> X[N] = {{0, 1}, {1, -2}, {2, 3}, {3, -1},
208  {4, 1}, {6, -5}, {-7, -3}, {-8, 5},
209  {-9, 2}, {-10, -15}};
210  int y[] = {1, -1, 1, -1, -1, -1, 1, 1, 1, -1}; // corresponding y-values
211 
212  std::cout << "------- Test 1 -------" << std::endl;
213  std::cout << "Model before fit: " << ada << std::endl;
214 
215  ada.fit(X, y);
216  std::cout << "Model after fit: " << ada << std::endl;
217 
218  int predict = ada.predict({5, -3});
219  std::cout << "Predict for x=(5,-3): " << predict;
220  assert(predict == -1);
221  std::cout << " ...passed" << std::endl;
222 
223  predict = ada.predict({5, 8});
224  std::cout << "Predict for x=(5,8): " << predict;
225  assert(predict == 1);
226  std::cout << " ...passed" << std::endl;
227 }
Here is the call graph for this function:

◆ test2()

void test2 ( double  eta = 0.01)

test function to predict points in a 2D coordinate system above the line \(x+3y=-1\) as +1 and others as -1. Note that each point is defined by 2 values or 2 features. The function will create random sample points for training and test purposes.

Parameters
[in]etalearning rate (optional, default=0.01)
236  {
237  adaline ada(2, eta); // 2 features
238 
239  const int N = 50; // number of sample points
240 
241  std::vector<double> X[N];
242  int Y[N]; // corresponding y-values
243 
244  // generate sample points in the interval
245  // [-range2/100 , (range2-1)/100]
246  int range = 500; // sample points full-range
247  int range2 = range >> 1; // sample points half-range
248  for (int i = 0; i < N; i++) {
249  double x0 = ((std::rand() % range) - range2) / 100.f;
250  double x1 = ((std::rand() % range) - range2) / 100.f;
251  X[i] = {x0, x1};
252  Y[i] = (x0 + 3. * x1) > -1 ? 1 : -1;
253  }
254 
255  std::cout << "------- Test 2 -------" << std::endl;
256  std::cout << "Model before fit: " << ada << std::endl;
257 
258  ada.fit(X, Y);
259  std::cout << "Model after fit: " << ada << std::endl;
260 
261  int N_test_cases = 5;
262  for (int i = 0; i < N_test_cases; i++) {
263  double x0 = ((std::rand() % range) - range2) / 100.f;
264  double x1 = ((std::rand() % range) - range2) / 100.f;
265 
266  int predict = ada.predict({x0, x1});
267 
268  std::cout << "Predict for x=(" << x0 << "," << x1 << "): " << predict;
269 
270  int expected_val = (x0 + 3. * x1) > -1 ? 1 : -1;
271  assert(predict == expected_val);
272  std::cout << " ...passed" << std::endl;
273  }
274 }
Here is the call graph for this function:

◆ test3()

void test3 ( double  eta = 0.01)

test function to predict points in a 3D coordinate system lying within the sphere of radius 1 and centre at origin as +1 and others as -1. Note that each point is defined by 3 values but we use 6 features. The function will create random sample points for training and test purposes. The sphere centred at origin and radius 1 is defined as: \(x^2+y^2+z^2=r^2=1\) and if the \(r^2<1\), point lies within the sphere else, outside.

Parameters
[in]etalearning rate (optional, default=0.01)
287  {
288  adaline ada(6, eta); // 2 features
289 
290  const int N = 100; // number of sample points
291 
292  std::vector<double> X[N];
293  int Y[N]; // corresponding y-values
294 
295  // generate sample points in the interval
296  // [-range2/100 , (range2-1)/100]
297  int range = 200; // sample points full-range
298  int range2 = range >> 1; // sample points half-range
299  for (int i = 0; i < N; i++) {
300  double x0 = ((std::rand() % range) - range2) / 100.f;
301  double x1 = ((std::rand() % range) - range2) / 100.f;
302  double x2 = ((std::rand() % range) - range2) / 100.f;
303  X[i] = {x0, x1, x2, x0 * x0, x1 * x1, x2 * x2};
304  Y[i] = ((x0 * x0) + (x1 * x1) + (x2 * x2)) <= 1.f ? 1 : -1;
305  }
306 
307  std::cout << "------- Test 3 -------" << std::endl;
308  std::cout << "Model before fit: " << ada << std::endl;
309 
310  ada.fit(X, Y);
311  std::cout << "Model after fit: " << ada << std::endl;
312 
313  int N_test_cases = 5;
314  for (int i = 0; i < N_test_cases; i++) {
315  double x0 = ((std::rand() % range) - range2) / 100.f;
316  double x1 = ((std::rand() % range) - range2) / 100.f;
317  double x2 = ((std::rand() % range) - range2) / 100.f;
318 
319  int predict = ada.predict({x0, x1, x2, x0 * x0, x1 * x1, x2 * x2});
320 
321  std::cout << "Predict for x=(" << x0 << "," << x1 << "," << x2
322  << "): " << predict;
323 
324  int expected_val = ((x0 * x0) + (x1 * x1) + (x2 * x2)) <= 1.f ? 1 : -1;
325  assert(predict == expected_val);
326  std::cout << " ...passed" << std::endl;
327  }
328 }
Here is the call graph for this function:
std::srand
T srand(T... args)
std::vector< double >
test1
void test1(double eta=0.01)
Definition: adaline_learning.cpp:202
std::cout
std::rand
T rand(T... args)
std::endl
T endl(T... args)
std::strtof
T strtof(T... args)
std::time
T time(T... args)
std::cin
test2
void test2(double eta=0.01)
Definition: adaline_learning.cpp:236
test3
void test3(double eta=0.01)
Definition: adaline_learning.cpp:287
machine_learning::adaline
Definition: adaline_learning.cpp:42