|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define INNODE 2 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>
|
|
|
|
|
#define HIDENODE 10 // <20><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>
|
|
|
|
|
#define OUTNODE 1 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧϰ<EFBFBD>ʣ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
double StudyRate = 1.6;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
double threshold = 1e-4;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
int mostTimes = 1e6;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
|
|
|
|
*/
|
|
|
|
|
int trainSize = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>С
|
|
|
|
|
*/
|
|
|
|
|
int testSize = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
typedef struct Sample{
|
|
|
|
|
double out[30][OUTNODE]; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
double in[30][INNODE]; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
}Sample;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
typedef struct Node{
|
|
|
|
|
double value; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
double bias; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>ƫƫ<C6AB><C6AB>ֵ
|
|
|
|
|
double bias_delta; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
double *weight; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>㴫<EFBFBD><E3B4AB><EFBFBD><EFBFBD>Ȩֵ
|
|
|
|
|
double *weight_delta; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>㴫<EFBFBD><E3B4AB><EFBFBD><EFBFBD>Ȩֵ<C8A8><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
}Node;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
Node inputLayer[INNODE];
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
Node hideLayer[HIDENODE];
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
Node outLayer[OUTNODE];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double Max(double a, double b){
|
|
|
|
|
return a > b ? a : b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sigmoid
|
|
|
|
|
* @param x <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
*/
|
|
|
|
|
double sigmoid(double x){
|
|
|
|
|
//<2F>벹ȫsigmod<6F><64><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
return 1 / (1 + exp(-x));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD>ȡѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param filename <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>
|
|
|
|
|
* @param filename <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return <EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
Sample * getTestData(const char * filename){
|
|
|
|
|
Sample* result = (Sample*)malloc(sizeof(Sample));
|
|
|
|
|
/*<2A><><EFBFBD>ڴ<EFBFBD><DAB4>з<EFBFBD><D0B7><EFBFBD><EFBFBD>㹻<EFBFBD>Ŀռ<C4BF><D5BC><EFBFBD><EFBFBD>洢һ<E6B4A2><D2BB>Sample<6C>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>洢<EFBFBD><E6B4A2>result<6C><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|
|
|
|
|
|
|
|
|
FILE * file = fopen(filename, "r");//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|
|
|
|
if(file != NULL){
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>count<6E><74><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8>ٶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int count = 0;
|
|
|
|
|
/*<2A><><EFBFBD><EFBFBD>whileѭ<65><D1AD><EFBFBD>Ӳ<EFBFBD><D3B2>Լ<EFBFBD><D4BC>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ļ<EFBFBD>ĩβ<C4A9><CEB2>
|
|
|
|
|
ÿ<EFBFBD>γɹ<EFBFBD><EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><EFBFBD><EFBFBD>count<EFBFBD><EFBFBD>*/
|
|
|
|
|
while (fscanf(file, "%lf %lf", &result->in[count][0], &result->in[count][1]) != EOF) {
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>count<6E><74>ֵ<EFBFBD>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD>ΪtestSize<7A><65>ȫ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>У<EFBFBD><D0A3>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
|
|
|
|
testSize = count;
|
|
|
|
|
printf("%s The file has been successfully read!\n", filename);
|
|
|
|
|
fclose(file);
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>result
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
fclose(file);
|
|
|
|
|
printf("%s Encountered an error while opening the file!\n\a", filename);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param data Ҫ<EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param size <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
|
|
|
|
*/
|
|
|
|
|
void printData(Sample * data, int size){
|
|
|
|
|
/*<2A>벹ȫ<EBB2B9><C8AB><EFBFBD><EFBFBD>*/
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
printf("%lf %lf %lf\n", data->in[i][0], data->in[i][1], data->out[i][0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
void init(){
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩֵ<C8A8><D6B5>ʼ<EFBFBD><CABC>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < HIDENODE; ++i) {
|
|
|
|
|
/*Ϊ<><CEAA><EFBFBD>ز<EFBFBD><D8B2>ڵ<EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> double <20><><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>ڴ洢<DAB4>ýڵ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FBFBD>*/
|
|
|
|
|
hideLayer[i].bias_delta = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD>Ȩֵ
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
*/
|
|
|
|
|
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() {
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>
|
|
|
|
|
init();
|
|
|
|
|
// <20><>ȡѵ<C8A1><D1B5><EFBFBD><EFBFBD>
|
|
|
|
|
Sample * trainSample = getTrainData("TrainData.txt");
|
|
|
|
|
// printData(trainSample, trainSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int trainTime = 0; trainTime < mostTimes; ++trainTime) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD>Ϣ
|
|
|
|
|
resetDelta();
|
|
|
|
|
|
|
|
|
|
// <20><>ǰѵ<C7B0><D1B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
double error_max = 0.0;
|
|
|
|
|
|
|
|
|
|
// <20><>ʼѵ<CABC><D1B5><EFBFBD><EFBFBD><EFBFBD>ۼ<EFBFBD>bp<62><70>
|
|
|
|
|
for (int currentTrainSample_Pos = 0; currentTrainSample_Pos < trainSize; ++currentTrainSample_Pos) {
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>
|
|
|
|
|
for (int inputLayer_Pos = 0; inputLayer_Pos < INNODE; ++inputLayer_Pos) {
|
|
|
|
|
inputLayer[inputLayer_Pos].value = trainSample->in[currentTrainSample_Pos][inputLayer_Pos];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** ----- <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> ----- */
|
|
|
|
|
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) {
|
|
|
|
|
/*<2A><><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2>ڵ<EFBFBD><DAB5><EFBFBD>value<75><65>Ȩֵ<C8A8>ij˻<C4B3><CBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵõ<D3B5>sum*/
|
|
|
|
|
sum += hideLayer[hideLayer_Pos].value * hideLayer[hideLayer_Pos].weight[outLayer_Pos];
|
|
|
|
|
}
|
|
|
|
|
/*<2A><><EFBFBD><EFBFBD>sum<75><6D>ʹsum<75><6D>ȥƫ<C8A5><C6AB>ֵ; */
|
|
|
|
|
sum -= hideLayer->bias;
|
|
|
|
|
/*<2A><><EFBFBD><EFBFBD>sigmod<6F><64><EFBFBD><EFBFBD><EFBFBD>Եõ<D4B5><C3B5><EFBFBD>sum<75><6D><EFBFBD>м<EFBFBD><D0BC><EFBFBD>Ѽ<EFBFBD><D1BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>value(outLayer[outLayer_Pos].value)<29><>*/
|
|
|
|
|
outLayer[outLayer_Pos].value = sigmoid(sum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** ----- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ----- */
|
|
|
|
|
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]);
|
|
|
|
|
// <20><>ʧ<EFBFBD><CAA7><EFBFBD><EFBFBD>
|
|
|
|
|
error += temp * temp / 2.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_max = Max(error_max, error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** ----- <20><><EFBFBD><EFBFBD> ----- */
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ﵽ<EFBFBD><EFB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
|
|
|
|
|
if(error_max < threshold){
|
|
|
|
|
printf("\a Training completed!Total training count:%d, maximum error is:%f\n", trainTime + 1, error_max);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB7><EFBFBD><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ѵ<><D1B5><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>Լ<EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
}
|