|
楼主 |
发表于 2007-7-6 02:46
|
显示全部楼层
- /******************************************************************************
- T R A I N I N G T H E N E T
- ******************************************************************************/
- REAL Neighborhood(NET* Net, INT i)
- {
- INT iRow, iCol, jRow, jCol;
- REAL Distance;
- iRow = i / COLS; jRow = Net->Winner / COLS;
- iCol = i % COLS; jCol = Net->Winner % COLS;
- Distance = sqrt(sqr(iRow-jRow) + sqr(iCol-jCol));
- return exp(-sqr(Distance) / (2*sqr(Net->Sigma)));
- }
- void TrainKohonen(NET* Net, REAL* Input)
- {
- INT i,j;
- REAL Out, Weight, Lambda, StepSize;
- for (i=0; i<Net->KohonenLayer->Units; i++) {
- for (j=0; j<Net->InputLayer->Units; j++) {
- Out = Input[j];
- Weight = Net->KohonenLayer->Weight[i][j];
- Lambda = Neighborhood(Net, i);
- Net->KohonenLayer->Weight[i][j] += Net->Alpha * Lambda * (Out - Weight);
- }
- StepSize = Net->KohonenLayer->StepSize[i];
- Net->KohonenLayer->StepSize[i] += Net->Alpha__ * Lambda * -StepSize;
- }
- }
- void TrainOutput(NET* Net, REAL* Output)
- {
- INT i,j;
- REAL Out, Weight, Lambda;
- for (i=0; i<Net->OutputLayer->Units; i++) {
- for (j=0; j<Net->KohonenLayer->Units; j++) {
- Out = Output[i];
- Weight = Net->OutputLayer->Weight[i][j];
- Lambda = Neighborhood(Net, j);
- Net->OutputLayer->Weight[i][j] += Net->Alpha_ * Lambda * (Out - Weight);
- }
- }
- }
- void TrainUnits(NET* Net, REAL* Input, REAL* Output)
- {
- TrainKohonen(Net, Input);
- TrainOutput(Net, Output);
- }
- void TrainNet(NET* Net)
- {
- INT n,t;
- POLE Pole;
- REAL wOld, wNew, ScoreOld, ScoreNew, dScore, dScoreMean, StepSize;
- REAL Input[N];
- REAL Output[M];
- REAL Target[M];
- n = 0;
- while (n<TRAIN_STEPS) {
- t = 0;
- InitializePole(&Pole);
- fprintf(f, " Time Angle Force\n");
- fprintf(f, "%4.1fs %5.1f? %5.1fN\n", t * T, Pole.w, Pole.F);
- wOld = Pole.w;
- ScoreOld = ScoreOfPole(&Pole);
- SimulatePole(&Pole);
- wNew = Pole.w;
- ScoreNew = ScoreOfPole(&Pole);
- while (PoleStillBalanced(&Pole) AND (t<BALANCED)) {
- n++;
- t++;
- Net->Alpha = 0.5 * pow(0.01, (REAL) n / TRAIN_STEPS);
- Net->Alpha_ = 0.5 * pow(0.01, (REAL) n / TRAIN_STEPS);
- Net->Alpha__ = 0.005;
- Net->Gamma = 0.05;
- Net->Sigma = 6.0 * pow(0.2, (REAL) n / TRAIN_STEPS);
- Input[0] = wOld;
- Input[1] = wNew;
- SetInput(Net, Input);
- PropagateNet(Net);
- GetOutput(Net, Output);
- Pole.F = Output[0];
- StepSize = Net->KohonenLayer->StepSize[Net->Winner];
- Pole.F += StepSize * RandomNormalREAL(0, 10);
- fprintf(f, "%4.1fs %5.1f? %5.1fN\n", t * T, Pole.w, Pole.F);
- wOld = Pole.w;
- ScoreOld = ScoreOfPole(&Pole);
- SimulatePole(&Pole);
- wNew = Pole.w;
- ScoreNew = ScoreOfPole(&Pole);
- dScore = ScoreNew - ScoreOld;
- dScoreMean = Net->KohonenLayer->dScoreMean[Net->Winner];
- if (dScore > dScoreMean) {
- Target[0] = Pole.F;
- TrainUnits(Net, Input, Target);
- }
- Net->KohonenLayer->dScoreMean[Net->Winner] += Net->Gamma * (dScore - dScoreMean);
- }
- if (PoleStillBalanced(&Pole))
- fprintf(f, "Pole still balanced after %0.1fs ...\n\n", t * T);
- else
- fprintf(f, "Pole fallen after %0.1fs ...\n\n", (t+1) * T);
- }
- }
- /******************************************************************************
- M A I N
- ******************************************************************************/
- void main()
- {
- NET Net;
- InitializeRandoms();
- GenerateNetwork(&Net);
- RandomWeights(&Net);
- InitializeApplication(&Net);
- TrainNet(&Net);
- WriteNet(&Net);
- FinalizeApplication(&Net);
- }
复制代码 |
|