/*
File name: widrow.c
Created by: Ljubomir Buturovic
Created: 08/10/2004
Purpose: linear discriminant learning using Widrow-Hoff algorithm.
*/
/*
Copyright 2004 Ljubomir J. Buturovic
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include "lau.h"
#include "lmat.h"
#include "dataset.h"
#include "widrow.h"
static char rcsid[] = "$Id: widrow.c,v 1.4 2004/10/21 01:55:28 ljubomir Exp $";
/*
Calculate linear discriminant classifier using Widrow-Hoff (LMS)
algorithm. The returned value is c by d+1 matrix (number of classes
by number of features plus 1). The last column contains biases.
The computations follow Algorithm 10 in Section 5.8.4, in
R. O. Duda, P. E. Hart and D. G. Stork, Pattern Classification,
Second Edition, John Wiley & Sons, Inc., 2001.
In case of error, return NULL and set 'errc'. The errors are EINVAL
if 'dset' is NULL, and memory allocation errors.
*/
float **widrow_learn(struct dataset *dset, int *errc, FILE *fdbg)
{
int i;
int j;
int k;
int d;
int idx;
int done;
int iclass;
int icum;
int iter;
float theta;
float eta;
float crit;
float term;
float *output;
float *diff;
float **lin = (float **) 0;
float **target;
if (dset)
{
d = dset->d;
target = fmx_alloc(dset->nv, dset->c);
if (target)
{
fmx_set(target, dset->nv, dset->c, 0.0);
iclass = 0;
icum = dset->nd[0];
for (i = 0; i < dset->nv; i++)
{
target[i][iclass] = 1.0;
if ((i == icum-1) && (iclass < dset->c-1))
{
iclass++;
icum += dset->nd[iclass];
}
}
}
output = malloc(dset->c*sizeof(float));
diff = malloc(dset->c*sizeof(float));
lin = fmx_alloc(dset->c, d+1);
if (lin)
{
fmx_set(lin, dset->c, d+1, 0.0);
k = 0;
done = 0;
theta = 0.000001;
iter = 0;
while (!done)
{
/*
For iris data, 1e-5 works very well. For vehicle data,
the program crashes, but 1e-7 works.
*/
eta = 1e-5;
idx = rand_int(0, dset->nv-1);
for (i = 0; i < dset->c; i++)
{
output[i] = fvec_dot(lin[i], dset->x[idx], d, (int *) 0);
output[i] += lin[i][d];
diff[i] = target[idx][i]-output[i];
}
crit = 0.0;
for (i = 0; i < dset->c; i++)
{
for (j = 0; j < d; j++)
{
term = eta*diff[i]*dset->x[idx][j];
lin[i][j] += term;
crit += term*term;
}
term = eta*diff[i];
lin[i][d] += term;
crit += term*term;
}
k = (k+1) % dset->nv;
iter++;
/*
if ((iter % 1000) == 0)
printf("iter: %d; crit: %g\n", iter, crit);
*/
/*
if (crit < theta)
done = 1;
*/
if (iter == 400000)
done = 1;
}
free(output);
free(diff);
}
}
else if (errc)
*errc = EINVAL;
return lin;
}
syntax highlighted by Code2HTML, v. 0.9.1