Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
Adaline learning algorithm
Collaboration diagram for Adaline learning algorithm:

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. More...
 
void delete_adaline (struct adaline *ada)
 delete dynamically allocated memory More...
 
int adaline_activation (double x)
 Heaviside activation function More...
 
char * adaline_get_weights_str (const struct adaline *ada)
 Operator to print the weights of the model. More...
 
int adaline_predict (struct adaline *ada, const double *x, double *out)
 predict the output of the model for given set of features More...
 
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. More...
 
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. More...
 

Detailed Description

Function Documentation

◆ adaline_activation()

int adaline_activation ( double  x)

Heaviside activation function

Parameters
xactivation function input
Returns
\(f(x)= \begin{cases}1 & \forall\; x > 0\\ -1 & \forall\; x \le0 \end{cases}\)
105 { return x > 0 ? 1 : -1; }

◆ adaline_fit()

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.

Parameters
[in]adaadaline model to train
[in]Xarray of feature vector
[in]yknown output value for each feature vector
[in]Nnumber of training samples
185 {
186  double avg_pred_error = 1.f;
187 
188  int iter;
189  for (iter = 0;
190  (iter < MAX_ADALINE_ITER) && (avg_pred_error > ADALINE_ACCURACY);
191  iter++)
192  {
193  avg_pred_error = 0.f;
194 
195  // perform fit for each sample
196  for (int i = 0; i < N; i++)
197  {
198  double err = adaline_fit_sample(ada, X[i], y[i]);
199  avg_pred_error += fabs(err);
200  }
201  avg_pred_error /= N;
202 
203  // Print updates every 200th iteration
204  // if (iter % 100 == 0)
205  printf("\tIter %3d: Training weights: %s\tAvg error: %.4f\n", iter,
206  adaline_get_weights_str(ada), avg_pred_error);
207  }
208 
209  if (iter < MAX_ADALINE_ITER)
210  printf("Converged after %d iterations.\n", iter);
211  else
212  printf("Did not converged after %d iterations.\n", iter);
213 }
Here is the call graph for this function:

◆ adaline_fit_sample()

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.

Parameters
[in]adaadaline model to fit
[in]xfeature vector
[in]yknown output value
Returns
correction factor
159 {
160  /* output of the model with current weights */
161  int p = adaline_predict(ada, x, NULL);
162  int prediction_error = y - p; // error in estimation
163  double correction_factor = ada->eta * prediction_error;
164 
165  /* update each weight, the last weight is the bias term */
166  for (int i = 0; i < ada->num_weights - 1; i++)
167  {
168  ada->weights[i] += correction_factor * x[i];
169  }
170  ada->weights[ada->num_weights - 1] += correction_factor; // update bias
171 
172  return correction_factor;
173 }
Here is the call graph for this function:

◆ adaline_get_weights_str()

char* adaline_get_weights_str ( const struct adaline ada)

Operator to print the weights of the model.

Parameters
adamodel for which the values to print
Returns
pointer to a NULL terminated string of formatted weights
113 {
114  static char out[100]; // static so the value is persistent
115 
116  sprintf(out, "<");
117  for (int i = 0; i < ada->num_weights; i++)
118  {
119  sprintf(out, "%s%.4g", out, ada->weights[i]);
120  if (i < ada->num_weights - 1)
121  sprintf(out, "%s, ", out);
122  }
123  sprintf(out, "%s>", out);
124  return out;
125 }

◆ adaline_predict()

int adaline_predict ( struct adaline ada,
const double *  x,
double *  out 
)

predict the output of the model for given set of features

Parameters
[in]adaadaline model to predict
[in]xinput vector
[out]outoptional argument to return neuron output before applying activation function (NULL to ignore)
Returns
model prediction output
137 {
138  double y = ada->weights[ada->num_weights - 1]; // assign bias value
139 
140  for (int i = 0; i < ada->num_weights - 1; i++) y += x[i] * ada->weights[i];
141 
142  if (out) // if out variable is not NULL
143  *out = y;
144 
145  // quantizer: apply ADALINE threshold function
146  return adaline_activation(y);
147 }
Here is the call graph for this function:

◆ delete_adaline()

void delete_adaline ( struct adaline ada)

delete dynamically allocated memory

Parameters
[in]adamodel from which the memory is to be freed.
90 {
91  if (ada == NULL)
92  return;
93 
94  free(ada->weights);
95 };

◆ new_adaline()

struct adaline new_adaline ( const int  num_features,
const double  eta 
)

Default constructor.

Parameters
[in]num_featuresnumber of features present
[in]etalearning rate (optional, default=0.1)
Returns
new adaline model
60 {
61  if (eta <= 0.f || eta >= 1.f)
62  {
63  fprintf(stderr, "learning rate should be > 0 and < 1\n");
64  exit(EXIT_FAILURE);
65  }
66 
67  // additional weight is for the constant bias term
68  int num_weights = num_features + 1;
69  struct adaline ada;
70  ada.eta = eta;
71  ada.num_weights = num_weights;
72  ada.weights = (double *)malloc(num_weights * sizeof(double));
73  if (!ada.weights)
74  {
75  perror("Unable to allocate error for weights!");
76  return ada;
77  }
78 
79  // initialize with random weights in the range [-50, 49]
80  for (int i = 0; i < num_weights; i++) ada.weights[i] = 1.f;
81  // ada.weights[i] = (double)(rand() % 100) - 50);
82 
83  return ada;
84 }
adaline::weights
double * weights
weights of the neural network
Definition: adaline_learning.c:46
adaline_activation
int adaline_activation(double x)
Heaviside activation function
Definition: adaline_learning.c:105
N
#define N
number of digits of the large number
Definition: sol1.c:109
ADALINE_ACCURACY
#define ADALINE_ACCURACY
convergence accuracy
Definition: adaline_learning.c:51
adaline_fit_sample
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.
Definition: adaline_learning.c:158
adaline::eta
double eta
learning rate of the algorithm
Definition: adaline_learning.c:45
MAX_ADALINE_ITER
#define MAX_ADALINE_ITER
Maximum number of iterations to learn.
Definition: adaline_learning.c:40
adaline_predict
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
adaline
structure to hold adaline model parameters
Definition: adaline_learning.c:44
adaline_get_weights_str
char * adaline_get_weights_str(const struct adaline *ada)
Operator to print the weights of the model.
Definition: adaline_learning.c:112
adaline::num_weights
int num_weights
number of weights of the neural network
Definition: adaline_learning.c:47