algorithm - C - Perceptron school project - single data set, error not converging -
hello everyone, i'm stuck working on school project described such:
your program should read in file in.csv, , adjust weights of perceptron when given inputs trained with, provides corresponding outputs.
in reality, error may never 0 when training ann; goal make smart enough recognize instances. project start error of 5%, or 0.05. mean, example, ann correctly identified 95/100 of inputs, , 05/100 mistakenly classified. see if can modify parameters of ann training process better 5%, program should not continue trying modify weights after 500,000 iterations.
sample run: c:\percy in.csv 100 5 10
where executable percy.exe, input file in.csv, learning rate 100/1000=0.100, error 5/100, , max iterations in thousands, 10 corresponds 10,000.
the input file formatted per example below. first 16 characters in line represent sensor inputs, , last char either 0 or 1, , represents class input corresponds to.
0000000001000000,0
0000000001000001,1
0000000001000010,1
0000000001000011,1
0000000001000100,1
these few numbers portion of data set being used, rest of data set similar integers
so output supposed so:
iterationerror = 0.023437500, errcnt = 3, repnum=72
wt[00]: +0.00661 wt[01]: +0.00431 wt[02]: +0.00011 wt[03]: +0.00814
wt[04]: +0.00198 wt[05]: +0.00470 wt[06]: +0.00356 wt[07]: +0.00435
wt[08]: +0.00761 wt[09]: +0.52254 wt[10]: +0.00120 wt[11]: -0.01169
wt[12]: -0.00937 wt[13]: -0.00281 wt[14]: -0.00157 wt[15]: -0.00217
stop*****(with setting wt[10] 0.5232)
however, when run code (below) reading in .csv data set, program runs until hits 10k iterations , error doesn't change , i'm not sure may wrong loop or calculations. should note current code doesn't represent correct output yet.
any appreciated, thank you
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define learning_rate 0.1 #define max_iteration 10000 float randomfloat() { return (float)rand() / (float)rand_max; } int calculateoutput(float weights[], int x) { float sum = x * weights[0] + weights[1]; return (sum >= 0) ? 1 : -1; } int main(int argc, char *argv[]) { srand(time(null)); float weights[2], localerror, globalerror, max_error = 0.05; float x[208]; int outputs[208], patterncount, i, p, iteration, output; file *fp; if ((fp = fopen("in.csv", "r")) == null) { printf("cannot open file.\n"); exit(1); } = 0; while (fscanf(fp, "%f,%d", &x[i], &outputs[i]) != eof) { i++; } patterncount = i; weights[0] = randomfloat(); weights[1] = randomfloat(); iteration = 0; { printf(" iteration: %d ***********************************\n", iteration); iteration++; globalerror = 0; (p = 0; p < patterncount; p++) { output = calculateoutput(weights, x[p]); localerror = outputs[p] - output; weights[0] += learning_rate * localerror * x[p]; weights[1] += learning_rate * localerror; globalerror += (localerror*localerror); printf("%.1f \n", weights[0]); } globalerror = globalerror/((float)patterncount); printf(" iterationerror = %.5f\n", globalerror); } while ((globalerror > max_error) && (i < max_iteration)); return 0; }
Comments
Post a Comment