1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include "functions.h"
using namespace std;
float *ArraySum(float *x1,float *x2,int l)
{
float *y = new float[l];
for (int i = 0;i < l;i++)
y[i] = x1[i] + x2[i];
return y;
}
float ArrayProd(float *x1,float *x2,int l)
{
float s =0;
for (int i = 0;i < l;i++)
s = s + x1[i]*x2[i];
return s;
}
float *ArrayMultiply(float *x,float a,int l)
{
float *m = new float[l];
for (int i = 0;i < l;i++)
{
m[i] = x[i]*a;
}
return m;
}
float sgn(float x)
{
if (x < 0)
return (-1);
else if (x > 0)
return (1);
else
return(0);
}
float **percept(float **x,float *d,int SampleSize,int FactorDim,int MaxEpochs,float MaxError)
{
float e;
float E = 100;
int epochs = 0;
float *w = new float[FactorDim];
for (int i = 0;i < FactorDim;i++)
w[i] = 0;
while (epochs < MaxEpochs && E > MaxError)
{
epochs++;
E = 0;
for (int i = 0;i < SampleSize;i++)
{
e = d[i] - sgn(ArrayProd(x[i],w,FactorDim));
E = E + 0.5*e*e;
w = ArraySum(ArrayMultiply(x[i],e,FactorDim),w,FactorDim);
}
E = E/SampleSize;
}
float **r = new float*[2];
r[0] = new float[FactorDim];
r[1] = new float[2];
r[0] = w;
r[1][0] = E;
r[1][1] = epochs;
return(r);
}
|