Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
adaline_learning.c File Reference

Adaptive Linear Neuron (ADALINE) implementation More...

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for adaline_learning.c:

Data Structures

struct  adaline
 structure to hold adaline model parameters More...
 

Macros

#define MAX_ADALINE_ITER   500
 Maximum number of iterations to learn.
 
#define ADALINE_ACCURACY   1e-5
 convergence accuracy \(=1\times10^{-5}\)
 

Functions

struct adaline new_adaline (const int num_features, const double eta)
 Default constructor.
 
void delete_adaline (struct adaline *ada)
 delete dynamically allocated memory
 
int adaline_activation (double x)
 Heaviside activation function
 
char * adaline_get_weights_str (const struct adaline *ada)
 Operator to print the weights of the model.
 
int adaline_predict (struct adaline *ada, const double *x, double *out)
 predict the output of the model for given set of features
 
double adaline_fit_sample (struct adaline *ada, const double *x, const int y)
 Update the weights of the model using supervised learning for one feature vector.
 
void adaline_fit (struct adaline *ada, double **X, const int *y, const int N)
 Update the weights of the model using supervised learning for an array of vectors.
 
void test1 (double eta)
 test function to predict points in a 2D coordinate system above the line \(x=y\) as +1 and others as -1.
 
void test2 (double eta)
 test function to predict points in a 2D coordinate system above the line \(x+3y=-1\) as +1 and others as -1.
 
void test3 (double eta)
 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.
 
int main (int argc, char **argv)
 Main function.
 

Detailed Description

Adaptive Linear Neuron (ADALINE) implementation

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.

Author
Krishna Vedala

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Main function.

399{
400 srand(time(NULL)); // initialize random number generator
401
402 double eta = 0.1; // default value of eta
403 if (argc == 2) // read eta value from commandline argument if present
404 eta = strtof(argv[1], NULL);
405
406 test1(eta);
407
408 printf("Press ENTER to continue...\n");
409 getchar();
410
411 test2(eta);
412
413 printf("Press ENTER to continue...\n");
414 getchar();
415
416 test3(eta);
417
418 return 0;
419}
void test2()
Definition k_means_clustering.c:356
void test1()
Test that creates a random set of points distributed in four clusters in 2D space and trains an SOM t...
Definition kohonen_som_topology.c:406
void test3()
Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM ...
Definition kohonen_som_topology.c:609
Here is the call graph for this function:

◆ test1()

void test1 ( double  eta)

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)
226{
227 struct adaline ada = new_adaline(2, eta); // 2 features
228
229 const int N = 10; // number of sample points
230 const double saved_X[10][2] = {{0, 1}, {1, -2}, {2, 3}, {3, -1},
231 {4, 1}, {6, -5}, {-7, -3}, {-8, 5},
232 {-9, 2}, {-10, -15}};
233
234 double **X = (double **)malloc(N * sizeof(double *));
235 const int Y[10] = {1, -1, 1, -1, -1,
236 -1, 1, 1, 1, -1}; // corresponding y-values
237 for (int i = 0; i < N; i++)
238 {
239 X[i] = (double *)saved_X[i];
240 }
241
242 printf("------- Test 1 -------\n");
243 printf("Model before fit: %s\n", adaline_get_weights_str(&ada));
244
245 adaline_fit(&ada, X, Y, N);
246 printf("Model after fit: %s\n", adaline_get_weights_str(&ada));
247
248 double test_x[] = {5, -3};
249 int pred = adaline_predict(&ada, test_x, NULL);
250 printf("Predict for x=(5,-3): % d\n", pred);
251 assert(pred == -1);
252 printf(" ...passed\n");
253
254 double test_x2[] = {5, 8};
255 pred = adaline_predict(&ada, test_x2, NULL);
256 printf("Predict for x=(5, 8): % d\n", pred);
257 assert(pred == 1);
258 printf(" ...passed\n");
259
260 // for (int i = 0; i < N; i++)
261 // free(X[i]);
262 free(X);
263 delete_adaline(&ada);
264}
void delete_adaline(struct adaline *ada)
delete dynamically allocated memory
Definition adaline_learning.c:89
void adaline_fit(struct adaline *ada, double **X, const int *y, const int N)
Update the weights of the model using supervised learning for an array of vectors.
Definition adaline_learning.c:184
int adaline_predict(struct adaline *ada, const double *x, double *out)
predict the output of the model for given set of features
Definition adaline_learning.c:136
struct adaline new_adaline(const int num_features, const double eta)
Default constructor.
Definition adaline_learning.c:59
char * adaline_get_weights_str(const struct adaline *ada)
Operator to print the weights of the model.
Definition adaline_learning.c:112
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
structure to hold adaline model parameters
Definition adaline_learning.c:44
double eta
learning rate of the algorithm
Definition adaline_learning.c:45
Here is the call graph for this function:

◆ test2()

void test2 ( double  eta)

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)
274{
275 struct adaline ada = new_adaline(2, eta); // 2 features
276
277 const int N = 50; // number of sample points
278
279 double **X = (double **)malloc(N * sizeof(double *));
280 int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values
281 for (int i = 0; i < N; i++) X[i] = (double *)malloc(2 * sizeof(double));
282
283 // generate sample points in the interval
284 // [-range2/100 , (range2-1)/100]
285 int range = 500; // sample points full-range
286 int range2 = range >> 1; // sample points half-range
287 for (int i = 0; i < N; i++)
288 {
289 double x0 = ((rand() % range) - range2) / 100.f;
290 double x1 = ((rand() % range) - range2) / 100.f;
291 X[i][0] = x0;
292 X[i][1] = x1;
293 Y[i] = (x0 + 3. * x1) > -1 ? 1 : -1;
294 }
295
296 printf("------- Test 2 -------\n");
297 printf("Model before fit: %s\n", adaline_get_weights_str(&ada));
298
299 adaline_fit(&ada, X, Y, N);
300 printf("Model after fit: %s\n", adaline_get_weights_str(&ada));
301
302 int N_test_cases = 5;
303 double test_x[2];
304 for (int i = 0; i < N_test_cases; i++)
305 {
306 double x0 = ((rand() % range) - range2) / 100.f;
307 double x1 = ((rand() % range) - range2) / 100.f;
308
309 test_x[0] = x0;
310 test_x[1] = x1;
311 int pred = adaline_predict(&ada, test_x, NULL);
312 printf("Predict for x=(% 3.2f,% 3.2f): % d\n", x0, x1, pred);
313
314 int expected_val = (x0 + 3. * x1) > -1 ? 1 : -1;
315 assert(pred == expected_val);
316 printf(" ...passed\n");
317 }
318
319 for (int i = 0; i < N; i++) free(X[i]);
320 free(X);
321 free(Y);
322 delete_adaline(&ada);
323}
Definition prime_factoriziation.c:25
Here is the call graph for this function:

◆ test3()

void test3 ( double  eta)

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)
337{
338 struct adaline ada = new_adaline(6, eta); // 2 features
339
340 const int N = 50; // number of sample points
341
342 double **X = (double **)malloc(N * sizeof(double *));
343 int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values
344 for (int i = 0; i < N; i++) X[i] = (double *)malloc(6 * sizeof(double));
345
346 // generate sample points in the interval
347 // [-range2/100 , (range2-1)/100]
348 int range = 200; // sample points full-range
349 int range2 = range >> 1; // sample points half-range
350 for (int i = 0; i < N; i++)
351 {
352 double x0 = ((rand() % range) - range2) / 100.f;
353 double x1 = ((rand() % range) - range2) / 100.f;
354 double x2 = ((rand() % range) - range2) / 100.f;
355 X[i][0] = x0;
356 X[i][1] = x1;
357 X[i][2] = x2;
358 X[i][3] = x0 * x0;
359 X[i][4] = x1 * x1;
360 X[i][5] = x2 * x2;
361 Y[i] = (x0 * x0 + x1 * x1 + x2 * x2) <= 1 ? 1 : -1;
362 }
363
364 printf("------- Test 3 -------\n");
365 printf("Model before fit: %s\n", adaline_get_weights_str(&ada));
366
367 adaline_fit(&ada, X, Y, N);
368 printf("Model after fit: %s\n", adaline_get_weights_str(&ada));
369
370 int N_test_cases = 5;
371 double test_x[6];
372 for (int i = 0; i < N_test_cases; i++)
373 {
374 double x0 = ((rand() % range) - range2) / 100.f;
375 double x1 = ((rand() % range) - range2) / 100.f;
376 double x2 = ((rand() % range) - range2) / 100.f;
377 test_x[0] = x0;
378 test_x[1] = x1;
379 test_x[2] = x2;
380 test_x[3] = x0 * x0;
381 test_x[4] = x1 * x1;
382 test_x[5] = x2 * x2;
383 int pred = adaline_predict(&ada, test_x, NULL);
384 printf("Predict for x=(% 3.2f,% 3.2f): % d\n", x0, x1, pred);
385
386 int expected_val = (x0 * x0 + x1 * x1 + x2 * x2) <= 1 ? 1 : -1;
387 assert(pred == expected_val);
388 printf(" ...passed\n");
389 }
390
391 for (int i = 0; i < N; i++) free(X[i]);
392 free(X);
393 free(Y);
394 delete_adaline(&ada);
395}
Here is the call graph for this function: