暗影零号 暗影零号
人会在痛苦中成长,当人承受了超越常人的痛苦之后,没错………
关注数: 25 粉丝数: 429 发帖数: 14,435 关注贴吧数: 60
BP程序求查错、、 我写了一个BP神经网络,但是在训练的时候发现训练起来不是那么回事,很混乱,基本不能成功。把有关部分源码放上来大家帮我看看哪里写的不对,谢谢! 我用的是objective-C,基本和C意思一样,希望不造成阅读嶂碍。 // // NeuronNetWork.h // net work // // Created by —————— on 12-1-19. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> @interface NeuronNetWork : NSObject { @public double KBetweenNeurons[15][25][15][25];//记录第a层第b个神经元与第c层第d个神经元的权值。 double LearnSpeed;//学习速率 double Momentum;//动量项 double KCBetweenNeuronsLast[15][25][15][25]; //上次学习时权值的变化量 int NumberOfStage;//层的数量 int NumberOfNeuron[100];//各层神经元的数量 double ResultOfNeuron[100][100];//神经元最后一次运算时神经元的输出 int FunctionUsed;//使用的阈值函数 double Threshold;//神经元阈值 int NumberOfData;//训练样本数 double DataInput[100][10];//记录输入样本,第a个样本的第b个输入 double DataOutput[100][10];//记录输出样本 double Input[100];//记录输入 double OutPut[100];//记录输出 double KA;//阈值函数中的一个参数 double KSD;//宽松度-当误差平方除以二的平均值小于宽松度时训练结束 double grads[10][20];//各神经元最后一次运算后的局部梯度,用来反馈调节时使用 double InsertOfNeuron[100][100];//最后一次运算时神经元的输入 BOOL iflearning;//判断是否在学习 } -(void)learn;//开始学习 -(void)ChangeK;//改变神经元之间的权值 -(void)clear;//初始化 -(id)init;//也是初始化 -(double)F1:(double)x;//阈值函数1 -(double)F2:(double)x;//阈值函数2 -(double)F3:(double)x;//阈值函数3 -(double)F4:(double)x;//阈值函数4 -(double)F1D:(double)x;//阈值函数1导函数 -(double)F2D:(double)x;//阈值函数2导函数 -(double)F3D:(double)x;//阈值函数3导函数 -(double)F4D:(double)x;//阈值函数4导函数 -(double)FD:(double)x;//调用阈值函数导函数,自动判断应该调用哪个 -(void)LoadLearnDataInput:(int)learn;//从第learn个样本中读取输入到神经网络的输入区域 -(void)RunForStage:(int)stage;//单层运转神经网络,用于被运转函数调用 -(void)run;//运转神经网络 -(double)GetDelta:(int)Learn;//得到误差平方除以二的平均值 -(void)CaculateGrads:(int)learn;//计算据部梯度 -(void)CZXL;//重新更新所有权值 @end // // NeuronNetWork.m // net work // // Created by —————— on 12-1-19. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "NeuronNetWork.h" @implementation NeuronNetWork
求BP程序查错 我写了一个BP神经网络,但是在训练的时候发现训练起来不是那么回事,很混乱,基本不能成功。把有关部分源码放上来大家帮我看看哪里写的不对,谢谢! 我用的是objective-C,基本和C意思一样,希望不造成阅读嶂碍。 // // NeuronNetWork.h // net work // // Created by —————— on 12-1-19. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> @interface NeuronNetWork : NSObject { @public double KBetweenNeurons[15][25][15][25];//记录第a层第b个神经元与第c层第d个神经元的权值。 double LearnSpeed;//学习速率 double Momentum;//动量项 double KCBetweenNeuronsLast[15][25][15][25]; //上次学习时权值的变化量 int NumberOfStage;//层的数量 int NumberOfNeuron[100];//各层神经元的数量 double ResultOfNeuron[100][100];//神经元最后一次运算时神经元的输出 int FunctionUsed;//使用的阈值函数 double Threshold;//神经元阈值 int NumberOfData;//训练样本数 double DataInput[100][10];//记录输入样本,第a个样本的第b个输入 double DataOutput[100][10];//记录输出样本 double Input[100];//记录输入 double OutPut[100];//记录输出 double KA;//阈值函数中的一个参数 double KSD;//宽松度-当误差平方除以二的平均值小于宽松度时训练结束 double grads[10][20];//各神经元最后一次运算后的局部梯度,用来反馈调节时使用 double InsertOfNeuron[100][100];//最后一次运算时神经元的输入 BOOL iflearning;//判断是否在学习 } -(void)learn;//开始学习 -(void)ChangeK;//改变神经元之间的权值 -(void)clear;//初始化 -(id)init;//也是初始化 -(double)F1:(double)x;//阈值函数1 -(double)F2:(double)x;//阈值函数2 -(double)F3:(double)x;//阈值函数3 -(double)F4:(double)x;//阈值函数4 -(double)F1D:(double)x;//阈值函数1导函数 -(double)F2D:(double)x;//阈值函数2导函数 -(double)F3D:(double)x;//阈值函数3导函数 -(double)F4D:(double)x;//阈值函数4导函数 -(double)FD:(double)x;//调用阈值函数导函数,自动判断应该调用哪个 -(void)LoadLearnDataInput:(int)learn;//从第learn个样本中读取输入到神经网络的输入区域 -(void)RunForStage:(int)stage;//单层运转神经网络,用于被运转函数调用 -(void)run;//运转神经网络 -(double)GetDelta:(int)Learn;//得到误差平方除以二的平均值 -(void)CaculateGrads:(int)learn;//计算据部梯度 -(void)CZXL;//重新更新所有权值 @end // // NeuronNetWork.m // net work // // Created by —————— on 12-1-19. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "NeuronNetWork.h" @implementation NeuronNetWork
问一个关于BP神经网络的问题= =不知到算不算一个问题 我写了一个BP神经网络,但是在训练的时候发现训练起来不是那么回事,很混乱,基本不能成功。把有关部分源码放上来大家帮我看看哪里写的不对,谢谢! 我用的是objective-C,基本和C意思一样,希望不造成阅读嶂碍。 // // NeuronNetWork.h // net work // // Created by —————— on 12-1-19. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> @interface NeuronNetWork : NSObject { @public double KBetweenNeurons[15][25][15][25];//记录第a层第b个神经元与第c层第d个神经元的权值。 double LearnSpeed;//学习速率 double Momentum;//动量项 double KCBetweenNeuronsLast[15][25][15][25]; //上次学习时权值的变化量 int NumberOfStage;//层的数量 int NumberOfNeuron[100];//各层神经元的数量 double ResultOfNeuron[100][100];//神经元最后一次运算时神经元的输出 int FunctionUsed;//使用的阈值函数 double Threshold;//神经元阈值 int NumberOfData;//训练样本数 double DataInput[100][10];//记录输入样本,第a个样本的第b个输入 double DataOutput[100][10];//记录输出样本 double Input[100];//记录输入 double OutPut[100];//记录输出 double KA;//阈值函数中的一个参数 double KSD;//宽松度-当误差平方除以二的平均值小于宽松度时训练结束 double grads[10][20];//各神经元最后一次运算后的局部梯度,用来反馈调节时使用 double InsertOfNeuron[100][100];//最后一次运算时神经元的输入 BOOL iflearning;//判断是否在学习 } -(void)learn;//开始学习 -(void)ChangeK;//改变神经元之间的权值 -(void)clear;//初始化 -(id)init;//也是初始化 -(double)F1:(double)x;//阈值函数1 -(double)F2:(double)x;//阈值函数2 -(double)F3:(double)x;//阈值函数3 -(double)F4:(double)x;//阈值函数4 -(double)F1D:(double)x;//阈值函数1导函数 -(double)F2D:(double)x;//阈值函数2导函数 -(double)F3D:(double)x;//阈值函数3导函数 -(double)F4D:(double)x;//阈值函数4导函数 -(double)FD:(double)x;//调用阈值函数导函数,自动判断应该调用哪个 -(void)LoadLearnDataInput:(int)learn;//从第learn个样本中读取输入到神经网络的输入区域 -(void)RunForStage:(int)stage;//单层运转神经网络,用于被运转函数调用 -(void)run;//运转神经网络 -(double)GetDelta:(int)Learn;//得到误差平方除以二的平均值 -(void)CaculateGrads:(int)learn;//计算据部梯度 -(void)CZXL;//重新更新所有权值 @end // // NeuronNetWork.m // net work // // Created by —————— on 12-1-19. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "NeuronNetWork.h" @implementation NeuronNetWork
(DP进阶)质数取石子 题目描述 DD 和 MM 正在玩取石子游戏。他们的游戏规则是这样的:桌上有若干石子,DD 先取,轮流取,每次必须取质数个。如果某一时刻某一方无法从桌上的石子中取质数个,比如说剩下 0 个或 1 个石子,那么他/她就输了。 DD 和 MM 都很聪明,不管哪方存在一个可以必胜的最优策略,他/她都会按照最优策略保证胜利。于是,DD 想知道,对于给定的桌面上的石子数,他究竟能不能取得胜利呢? 当 DD 确定会取得胜利时,他会说:“不管 MM 选择怎样的取石子策略,我都能保证至多 X 步以后就能取得胜利。”那么,最小的满足要求的 X 是多少呢?注意,不管是 DD 取一次石子还是 MM 取一次石子都应该被计算为“一步”。 输入格式 第一行有一个整数 N,表示这个输入文件中包含 N 个测试数据。 第二行开始,每行有一个测试数据,其中仅包含一个整数,表示桌面上的石子数。 输出格式 你需要对于每个输入文件中的 N 个测试数据输出相应的 N 行。 如果对于该种情形是 DD 一定取得胜利,那么输出最小的 X。否则该行输出 -1。 样例输入3 8 9 16 样例输出1 -1 3 样例说明 当桌上有 8 个石子时,先取的 DD 只需要取走 7 个石子剩下 1 个就可以在一步之后保证胜利,输出 1。 当桌上有 9 个石子时。若 DD 取走 2 个,MM 会取走 7 个,剩下 0 个,DD 输。若 DD 取走 3 个,MM 会取走 5 个,剩下 1 个,DD 输。DD 取走 5 个或者 7 个的情况同理可知。所以当桌上有 9 个石子时,不管 DD 怎么取,MM 都可以让 DD 输,输出 -1。 当桌上有 16 个石子时,DD 可以保证在 3 步以内取得胜利。可以证明,为了在 3 步内取得胜利,DD 第一步必须取 7 个石子。剩下 9 个石子之后,不管第二步 MM 怎么取,DD 取了第三步以后可以保证胜利,所以输出 3。 数据范围 输入文件中的数据数 N<=10。 每次桌上初始的石子数都不超过 20000。
aaaaaaa #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #define doorreducek 0.001; //build the menmber of neuron typedef struct neurons; typedef struct nerves; typedef struct excitingsave; int numofneurons=0; struct neurons { neurons *next; nerves *firstnerve; nerves *lastnerve; long int lastexcitetime; double excitereducek; int maxnervenum;// the max number of its' nerve double stddoor;// the standard door of this neuron double door;// the door of this neuron double state;// the state which control the neurons double kto;// when be excited,this neurons kto * connected neurons kfrom will be add to state of next double kfrom; // int output; } *firstneuron,*lastneuron; struct nerves { neurons *neuron; nerves *nextnerve; }; struct excitingsave { neurons *a; excitingsave *next; } *firstexneuron,*lastexneuron; int exctingneuronsnum=0; //to build a neurons link which save the neurons void buildneuronslink() { firstneuron = NULL; lastneuron = NULL; return ; } //a function used to add a new neuron in the link of neurons void createneuron(double stddoor,double kto,double kfrom,double reduce,int output) { neurons *newn; newn = (neurons *)malloc(sizeof(neurons)); newn->maxnervenum = 0; newn->excitereducek = reduce; newn->stddoor = stddoor; newn->door = stddoor; newn->state = 0; newn->kto = kto; newn->kfrom = kfrom; newn->next = NULL; newn->firstnerve = NULL; newn->lastnerve = NULL; newn->output = output; //printf("-%f-%f-\n",kto,newn->kto); if(firstneuron==NULL) { firstneuron = newn; lastneuron = newn; } else { lastneuron->next = newn; lastneuron = newn; } numofneurons++; } //connect a to b neurons void connectneuron(neurons *a,neurons *b) { nerves *newn; newn = (nerves *)malloc(sizeof(nerves)); newn->neuron = b; newn->nextnerve = NULL; if(a->firstnerve==NULL) { a->firstnerve = newn; a->lastnerve = newn; } else { a->lastnerve->nextnerve = newn; a->lastnerve = newn; } a->maxnervenum++; } //excite neuron a void exciteneuron(neurons *a) { nerves *p; neurons *to; FILE *f = fopen("excitingneurons.ai","a+"); FILE *f2 = fopen("output.ai","a+"); long int timenow = time(NULL); for(p=a->firstnerve;p!=NULL;p=p->nextnerve) { to = p->neuron; to->state += (to->kfrom) * (a->kto); if(to->state>=to->door) { printf("because %d,%d is excited\n",a,to); to->state = 0; to->door = to-> stddoor +(to->door - to->stddoor)*(1-(((double)(timenow - to->lastexcitetime)) * to->excitereducek)); to->door += (to->kfrom*a->kto)*doorreducek; to->lastexcitetime = timenow; fprintf(f,"%d\n",to); if(to->output!=0){fprintf(f2,"%d\n",to->output);} } } fclose(f); fclose(f2); } void deleteneuron(neurons *a) { } void cleanentxt() { FILE *f2 = fopen("excitingneurons.ai","w"); fclose(f2); } void cleanentxt2() { FILE *f = fopen("output.ai","w"); fclose(f); } void add0fortxt() { FILE *f = fopen("excitingneurons.ai","a+"); fprintf(f,"0\n"); fclose(f); return; } int main() { long int timer,j,key=1; FILE *f1; FILE *f0 = fopen("excitingneurons.ai","a+"); neurons *p; excitingsave *exs=NULL; buildneuronslink(); cleanentxt(); cleanentxt2(); createneuron(50,50,50,1,0); createneuron(60,50,50,1,0); createneuron(70,50,50,1,10); connectneuron(firstneuron,firstneuron->next); connectneuron(firstneuron->next,lastneuron); exciteneuron(firstneuron); for(;;)//the most impotant part of the progranmming { p = NULL; add0fortxt(); timer = time(NULL); key=1; firstexneuron = NULL; lastexneuron = NULL; f1 = fopen("excitingneurons.ai","r"); while(key==1) { fscanf(f1,"%d",&p); if(p==0) { key=0; } else { exs = (excitingsave *)malloc(sizeof(excitingsave)); exs->a = p; exs->next = NULL; if(firstexneuron==NULL) { firstexneuron=exs; lastexneuron=exs; } else { lastexneuron->next = exs; lastexneuron = exs; } } } fclose(f1); cleanentxt(); for(exs=firstexneuron;exs!=NULL;exs=exs->next) { exciteneuron(exs->a); } firstexneuron=NULL; lastexneuron=NULL; } fclose(f0); fclose(f1); return 0; }
首页 1 2 3 4 下一页