From e15f5b7df427aafdbf6270e7dd23f9e56a28652e Mon Sep 17 00:00:00 2001 From: p8rpsfin2 <2894799701@qq.com> Date: Thu, 16 Nov 2023 22:53:24 +0800 Subject: [PATCH] ADD file via upload --- BP神经网络易文昕.c | 424 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 424 insertions(+) create mode 100644 BP神经网络易文昕.c diff --git a/BP神经网络易文昕.c b/BP神经网络易文昕.c new file mode 100644 index 0000000..c06c611 --- /dev/null +++ b/BP神经网络易文昕.c @@ -0,0 +1,424 @@ +#include +#include +#include +#include + + +#define INNODE 2 // Ԫ +#define HIDENODE 8// زԪ +#define OUTNODE 1 // Ԫ + + +/** + * ѧϰʣ + */ +double StudyRate = 1.6; + +/** + * + */ +double threshold = 1e-4; + +/** + * + */ +int mostTimes = 1e6; + +/** + * ѵС + */ +int trainSize = 0; + +/** + * ԼС + */ +int testSize = 0; + +/** + * + */ +typedef struct Sample{ + double out[30][OUTNODE]; // + double in[30][INNODE]; // +}Sample; + + +/** + * Ԫ + */ +typedef struct Node{ + double value; // ǰԪֵ + double bias; // ǰԪƫƫֵ + double bias_delta; // ǰԪƫֵֵ + double *weight; // ǰԪһ㴫Ȩֵ + double *weight_delta; // ǰԪһ㴫Ȩֵֵ +}Node; + +/** + * + */ +Node inputLayer[INNODE]; +/** + * ز + */ +Node hideLayer[HIDENODE]; +/** + * + */ +Node outLayer[OUTNODE]; + + + +double Max(double a, double b){ + return a > b ? a : b; +} + +/** + * sigmoid + * @param x ֵ + * @return ֵ + */ +double sigmoid(double x){ + //벹ȫsigmodļ + double res; + res = 1 / (1 + exp(-x)); + return res; + +} + + +/** + * ȡѵ + * @param filename ļ + * @return ѵ + */ +Sample * getTrainData(const char * filename){ + Sample * result = (Sample*)malloc(sizeof (Sample)); + FILE * file = fopen(filename, "r"); + if(file != NULL){ + int count = 0; + while (fscanf(file, "%lf %lf %lf", &result->in[count][0], &result->in[count][1], &result->out[count][0]) != EOF){ + ++count; + } + trainSize = count; + printf("%s The file has been successfully read!\n", filename); + fclose(file); + return result; + } else{ + fclose(file); + printf("%s Encountered an error while opening the file!\n\a", filename); + return NULL; + } +} + +/** + * ȡԼ + * @param filename ļ + * @return Լ + */ +Sample * getTestData(const char * filename){ + /*ڴз㹻Ŀռ洢һSampleṹָڴָ洢result*/ + Sample * result = (Sample*)malloc(sizeof(Sample)); + FILE * file = fopen(filename, "r");//ļ + if(file != NULL){ + // ʼһcountڸٶȡ + int count = 0; + /*whileѭӲԼļжȡֱȡļĩβ + ÿγɹȡһݺ󣬵count*/ + while(fscanf(file,"%lf %lf %lf",&result->in[count][0],&result->in[count][1],&result->in[count][1]) != EOF){ + ++count; + } + testSize = count; + + //յcountֵ洢ΪtestSizeȫֱУԱʹ + + printf("%s The file has been successfully read!\n", filename); + fclose(file); + //result + + }else{ + fclose(file); + printf("%s Encountered an error while opening the file!\n\a", filename); + return NULL; + } +} + +/** + * ӡ + * @param data Ҫӡ + * @param size С + */ +void printData(Sample * data, int size){ + /*벹ȫ + + + */ + if(data == NULL) + { + printf("Sample is empty!"); + return; + } + else + { + for(int i = 0;i < size;i ++) + { + printf("%3d: Input1: %5lf, Input2: %5lf, Output: %5lf\n",i+1,data->in[i][0],data->in[i][1],data->out[i][0]); + } + } + + +} + +/** + * ʼ + */ +void init(){ + // ʱΪе + srand(time(NULL)); + + // ijʼ + for (int i = 0; i < INNODE; ++i) { + inputLayer[i].weight = (double *)malloc(sizeof (double ) * HIDENODE); + inputLayer[i].weight_delta = (double *) malloc(sizeof (double ) * HIDENODE); + inputLayer[i].bias = 0.0; + inputLayer[i].bias_delta = 0.0; + } + + // Ȩֵʼ + for (int i = 0; i < INNODE; ++i) { + for (int j = 0; j < HIDENODE; ++j) { + inputLayer[i].weight[j] = rand() % 10000 / (double )10000 * 2 - 1.0; + inputLayer[i].weight_delta[j] = 0.0; + } + } + + + // ʼز + for (int i = 0; i < HIDENODE; ++i) { + /*Ϊزڵ i һ double ͵飬ڴ洢ýڵһڵ㴫Ȩء + ʹmalloc ڶڴз㹻Ŀռ䣬Դ洢 OUTNODE double ͵Ȩֵ + */ + hideLayer[i].weight = (double*)malloc(sizeof(double)*OUTNODE); + /*Ϊزڵ i һڴ洢Ȩֵ顣齫ѵڴ洢Ȩصĸֵ + ʹmalloc ڶڴз㹻Ŀռ䣬Դ洢 OUTNODE double ͵Ȩֵ + */ + hideLayer[i].weight_delta = (double*)malloc(sizeof(double)*OUTNODE); + /*Ϊزڵ i ʼһƫֵ + ֵͨһ -1.0 1.0 ֮ڵýڵļֵ + */ + hideLayer[i].bias = rand() % 10000 / (double)10000 * 2 -1.0; + /*ʼزڵ i ƫֵֵʼֵΪ0.0*/ + hideLayer[i].bias_delta = 0.0; + } + + // ʼزȨֵ + for (int i = 0; i < HIDENODE; ++i) { + for (int j = 0; j < OUTNODE; ++j) { + hideLayer[i].weight[j] = rand() % 10000 / (double )10000 * 2 - 1.0; + hideLayer[i].weight_delta[j] = 0.0; + } + } + + for (int i = 0; i < OUTNODE; ++i) { + outLayer[i].bias = rand() % 10000 / (double )10000 * 2 - 1.0; + outLayer[i].bias_delta = 0.0; + } +} + +/** + * ֵ + */ +void resetDelta(){ + for (int i = 0; i < INNODE; ++i) { + for (int j = 0; j < HIDENODE; ++j) { + inputLayer[i].weight_delta[j] = 0.0; + } + } + + for (int i = 0; i < HIDENODE; ++i) { + hideLayer[i].bias_delta = 0.0; + for (int j = 0; j < OUTNODE; ++j) { + hideLayer[i].weight_delta[j] = 0.0; + } + } + + for (int i = 0; i < OUTNODE; ++i) { + outLayer[i].bias_delta = 0.0; + } +} + + +int main() { + // ʼ + init(); + // ȡѵ + Sample * trainSample = getTrainData("TrainData.txt"); +// printData(trainSample, trainSize); + + + for (int trainTime = 0; trainTime < mostTimes; ++trainTime) { + // ݶϢ + resetDelta(); + + // ǰѵ + double error_max = 0.0; + + // ʼѵۼbp + for (int currentTrainSample_Pos = 0; currentTrainSample_Pos < trainSize; ++currentTrainSample_Pos) { + + // Ա + for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) { + inputLayer[inputLayer_Pos].value = trainSample->in[currentTrainSample_Pos][inputLayer_Pos]; + } + + /** ----- ʼ򴫲 ----- */ + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + double sum = 0.0; + for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) { + sum += inputLayer[inputLayer_Pos].value * inputLayer[inputLayer_Pos].weight[hideLayer_Pos]; + } + + sum -= hideLayer[hideLayer_Pos].bias; + hideLayer[hideLayer_Pos].value = sigmoid(sum); + } + + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE ; ++outLayer_Pos) { + double sum = 0.0; + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + /*ÿһزڵvalueȨֵij˻ӵõsum + + */ + sum +=hideLayer[hideLayer_Pos].value * hideLayer[hideLayer_Pos].weight[outLayer_Pos]; + } + /*sumʹsumȥƫֵ; + + */ + sum -= outLayer[outLayer_Pos].bias; + /*sigmodԵõsumмѼĽֵӦڵvalue(outLayer[outLayer_Pos].value) + + */ + outLayer[outLayer_Pos].value = sigmoid(sum); + } + + + /** ----- ----- */ + double error = 0.0; + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + double temp = fabs(outLayer[outLayer_Pos].value - trainSample->out[currentTrainSample_Pos][outLayer_Pos]); + // ʧ + error += temp * temp / 2.0; + } + + error_max = Max(error_max, error); + + + /** ----- 򴫲 ----- */ + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + double bias_delta = -(trainSample->out[currentTrainSample_Pos][outLayer_Pos] - outLayer[outLayer_Pos].value) + * outLayer[outLayer_Pos].value * (1.0 - outLayer[outLayer_Pos].value); + outLayer[outLayer_Pos].bias_delta += bias_delta; + } + + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + double weight_delta = (trainSample->out[currentTrainSample_Pos][outLayer_Pos] - outLayer[outLayer_Pos].value) + * outLayer[outLayer_Pos].value * (1.0 - outLayer[outLayer_Pos].value) + * hideLayer[hideLayer_Pos].value; + hideLayer[hideLayer_Pos].weight_delta[outLayer_Pos] += weight_delta; + } + } + + // + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + double sum = 0.0; + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + sum += -(trainSample->out[currentTrainSample_Pos][outLayer_Pos] - outLayer[outLayer_Pos].value) + * outLayer[outLayer_Pos].value * (1.0 - outLayer[outLayer_Pos].value) + * hideLayer[hideLayer_Pos].weight[outLayer_Pos]; + } + hideLayer[hideLayer_Pos].bias_delta += sum * hideLayer[hideLayer_Pos].value * (1.0 - hideLayer[hideLayer_Pos].value); + } + + + for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) { + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + double sum = 0.0; + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + sum += (trainSample->out[currentTrainSample_Pos][outLayer_Pos] - outLayer[outLayer_Pos].value) + * outLayer[outLayer_Pos].value * (1.0 - outLayer[outLayer_Pos].value) + * hideLayer[hideLayer_Pos].weight[outLayer_Pos]; + } + inputLayer[inputLayer_Pos].weight_delta[hideLayer_Pos] += sum * hideLayer[hideLayer_Pos].value * (1.0 - hideLayer[hideLayer_Pos].value) + * inputLayer[inputLayer_Pos].value; + } + } + + } + + + // жǷﵽΧ + if(error_max < threshold){ + printf("\a Training completed!Total training count:%d, maximum error is:%f\n", trainTime + 1, error_max); + break; + } + + // ޷ܣʼ + + for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) { + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + inputLayer[inputLayer_Pos].weight[hideLayer_Pos] += StudyRate + * inputLayer[inputLayer_Pos].weight_delta[hideLayer_Pos] / + (double) trainSize; + } + } + + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + hideLayer[hideLayer_Pos].bias += StudyRate + * hideLayer[hideLayer_Pos].bias_delta / (double )trainSize; + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + hideLayer[hideLayer_Pos].weight[outLayer_Pos] += StudyRate + * hideLayer[hideLayer_Pos].weight_delta[outLayer_Pos] / (double )trainSize; + } + } + + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + outLayer[outLayer_Pos].bias += StudyRate + * outLayer[outLayer_Pos].bias_delta / (double )trainSize; + } + } + + // ѵɣȡԼ + Sample * testSample = getTestData("TestData.txt"); + printf("The predicted results are as follows:\n"); + for (int currentTestSample_Pos = 0; currentTestSample_Pos < testSize; ++currentTestSample_Pos) { + for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) { + inputLayer[inputLayer_Pos].value = testSample->in[currentTestSample_Pos][inputLayer_Pos]; + } + + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + double sum = 0.0; + for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) { + sum += inputLayer[inputLayer_Pos].value * inputLayer[inputLayer_Pos].weight[hideLayer_Pos]; + } + sum -= hideLayer[hideLayer_Pos].bias; + hideLayer[hideLayer_Pos].value = sigmoid(sum); + } + + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + double sum = 0.0; + for (int hideLayer_Pos = 0; hideLayer_Pos < HIDENODE; ++hideLayer_Pos) { + sum += hideLayer[hideLayer_Pos].value * hideLayer[hideLayer_Pos].weight[outLayer_Pos]; + } + sum -= outLayer[outLayer_Pos].bias; + outLayer[outLayer_Pos].value = sigmoid(sum); + } + + for (int outLayer_Pos = 0; outLayer_Pos < OUTNODE; ++outLayer_Pos) { + testSample->out[currentTestSample_Pos][outLayer_Pos] = outLayer[outLayer_Pos].value; + } + } + + printData(testSample, testSize); + + return 0; +}