📄 newnn2.cpp
字号:
double ** outputVector;
public:
SourceData(int nOE,int nOI,int nOO);
bool Print();
double GetInputVector(int orderOfExample,int idInLayer);
double GetOutputVector(int orderOfExample,int idInLayer);
};
SourceData::SourceData(int nOE,int nOIN,int nOON){
int i=0;
numberOfExample=nOE;
numberOfInputNeural=nOIN;
numberOfOutputNeural=nOON;
inputVector=(double **)malloc(nOE*sizeof(double *));
outputVector=(double **)malloc(nOE*sizeof(double *));
for(i=0;i<nOE;i++){
inputVector[i]=(double *)malloc(nOIN*sizeof(double));
outputVector[i]=(double *)malloc(nOON*sizeof(double));
}
cout<<"the number of example is "<<numberOfExample<<endl;
for(i=0;i<numberOfExample;i++){
cout<<"please input the "<<i<<" data:"<<endl;
for(int j=0;j<numberOfInputNeural;j++){
cout<<"please input the "<<j<<"th input signal:";
cin>>inputVector[i][j];
}
for(int k=0;k<numberOfOutputNeural;k++){
cout<<"please input the "<<k<<"th output signal:";
cin>>outputVector[i][k];
}
}
}
bool SourceData::Print(){
cout<<"the number of example is "<<numberOfExample<<endl;
for(int i=0;i<numberOfExample;i++){
cout<<"input signal of the "<<i<<"th data:";
for(int j=0;j<numberOfInputNeural;j++){
cout<<" "<<inputVector[i][j];
}
cout<<endl;
cout<<"output signal of the "<<i<<"th data:";
for(int k=0;k<numberOfOutputNeural;k++){
cout<<" "<<outputVector[i][k];
}
cout<<endl;
}
return true;
}
double SourceData::GetInputVector(int orderOfExample,int idInLayer){
if(orderOfExample>=numberOfExample||idInLayer>=numberOfInputNeural) return false;
return inputVector[orderOfExample][idInLayer];
}
double SourceData::GetOutputVector(int orderOfExample,int idInLayer){
if(orderOfExample>=numberOfExample||idInLayer>=numberOfOutputNeural) return false;
return outputVector[orderOfExample][idInLayer];
}
// 基于线性同余发生器的伪均匀随即数发生器
// unsigned long z
unsigned long mod1(double dd1, unsigned long dd2)
{
unsigned long temp, temp1;
temp1 = (unsigned long)(dd1/dd2);
temp = (unsigned long)(dd1-temp1*dd2);
return(temp);
}
double drand()
{
/* Z(i) = (aZ(i-1)+C) mod m; 0<=Z(i)<=m-1 */
/* U(i) = Z(i)/m 0<=U(i)<=1 */
/* a = 65539.0; c = 1732515.0; m = 2147483638 */
double x;
x = 65539.0 * z + 17325151.0;
z = mod1(x, 2147483638);
return(z/2147483638.0);
}
//
void main(){
int i=0,j=0,k=0;
int p=0,q=0;
int temp;
double dtemp,ddtemp;
Media ** mediaList=0;
Neural *** neuralList=0;
int numberOfIterations=0;
int numberOfNeuralsInInputLayer=0;
int numberOfHiddenlayer=0;
int * numberOfNeuralsInHiddenLayer=0;
int numberOfNeuralsInOutputLayer=0;
double learingRate=0.05;
cout<<"please input the number of iteration: ";
cin>>numberOfIterations;
cout<<"please input the number of neurals in the input layer: ";
cin>>numberOfNeuralsInInputLayer;
cout<<"please input the number of hidden layer: ";
cin>>numberOfHiddenlayer;
numberOfNeuralsInHiddenLayer=(int *)malloc(numberOfHiddenlayer*sizeof(int));
mediaList=(Media **)malloc((numberOfHiddenlayer+1)*sizeof(Media *));
for(i=0;i<numberOfHiddenlayer;i++){
cout<<"please input the number of neurals in the "<<i<<"th hidden layer: ";
cin>>numberOfNeuralsInHiddenLayer[i];
if(i==0){
mediaList[i]=new Media(numberOfNeuralsInInputLayer,numberOfNeuralsInHiddenLayer[i]);
}else {
mediaList[i]=new Media(numberOfNeuralsInHiddenLayer[i-1],numberOfNeuralsInHiddenLayer[i]);
}
//cout<<i<<" "<<mediaList[i]->GetNumberOfPriorNeural()<<" "<<mediaList[i]->GetNumberOfNextNeural()<<endl;
}
cout<<"please input the number of neurals in the output layer: ";
cin>>numberOfNeuralsInOutputLayer;
mediaList[numberOfHiddenlayer]=new Media(numberOfNeuralsInHiddenLayer[numberOfHiddenlayer-1],numberOfNeuralsInOutputLayer);
//cout<<numberOfHiddenlayer<<" "<<mediaList[numberOfHiddenlayer]->GetNumberOfPriorNeural()<<" "<<mediaList[numberOfHiddenlayer]->GetNumberOfNextNeural()<<endl;
neuralList=(Neural ***)malloc((1+numberOfHiddenlayer+1)*sizeof(Neural **));
for(i=0;i<1+numberOfHiddenlayer+1;i++){
if(i==0){
neuralList[i]=(Neural **)malloc(numberOfNeuralsInInputLayer*sizeof(Neural *));
for(int j=0;j<numberOfNeuralsInInputLayer;j++){
neuralList[i][j]=new Neural(inputLayer,j,0,mediaList[i]);
mediaList[i]->Push(neuralList[i][j],j,fPrior);
// cout<<"cursor:"<<i<<" "<<mediaList[i]->GetCursorOfPrior()<<" "<<mediaList[i]->GetCursorOfNext()<<endl;
}
//cout<<endl;
}else if(i==numberOfHiddenlayer+1){
neuralList[i]=(Neural **)malloc(numberOfNeuralsInOutputLayer*sizeof(Neural *));
for(int j=0;j<numberOfNeuralsInOutputLayer;j++){
neuralList[i][j]=new Neural(outputLayer,j,mediaList[i-1],0);
mediaList[i-1]->Push(neuralList[i][j],j,fNext);
// cout<<"cursor:"<<i-1<<" "<<mediaList[i-1]->GetCursorOfPrior()<<" "<<mediaList[i-1]->GetCursorOfNext()<<endl;
}
//cout<<endl;
}else if(i>0&&i<numberOfHiddenlayer+1){
neuralList[i]=(Neural **)malloc(numberOfNeuralsInHiddenLayer[i-1]*sizeof(Neural *));
for(int j=0;j<numberOfNeuralsInHiddenLayer[i-1];j++){
neuralList[i][j]=new Neural(hiddenLayer,j,mediaList[i-1],mediaList[i]);
mediaList[i-1]->Push(neuralList[i][j],j,fNext);
mediaList[i]->Push(neuralList[i][j],j,fPrior);
// cout<<"cursor:"<<i-1<<" "<<mediaList[i-1]->GetCursorOfPrior()<<" "<<mediaList[i-1]->GetCursorOfNext()<<endl;
// cout<<"cursor:"<<i<<" "<<mediaList[i]->GetCursorOfPrior()<<" "<<mediaList[i]->GetCursorOfNext()<<endl;
}
//cout<<endl;
//if(i==numberOfHiddenlayer) cout<<"cursor:"<<mediaList[i]->GetCursorOfPrior()<<endl;
}
}
for(i=0;i<1+numberOfHiddenlayer;i++){
cout<<"i="<<i<<endl;
mediaList[i]->InitWeight();
mediaList[i]->WeightPrint();
// cout<<"cursorofprior: "<<mediaList[i]->GetCursorOfPrior()<<endl;
// cout<<"cursorofnext: "<<mediaList[i]->GetCursorOfNext()<<endl;
}
int numberOfTrainningExample=0;
cout<<"please input the number of trainning examples: ";
cin>>numberOfTrainningExample;
SourceData trainningData(numberOfTrainningExample,numberOfNeuralsInInputLayer,numberOfNeuralsInOutputLayer);
trainningData.Print();
int numberOfTestExample=0;
cout<<"please input the number of the test example: ";
cin>>numberOfTestExample;
SourceData testData(numberOfTestExample,numberOfNeuralsInInputLayer,numberOfNeuralsInOutputLayer);
testData.Print();
for(i=0;i<numberOfIterations;i++){
ddtemp=0;
for(p=0;p<numberOfTrainningExample;p++){
for(j=0;j<numberOfNeuralsInInputLayer;j++){
if(neuralList[0][j]->SetInputNeuralValues(trainningData.GetInputVector(p,j))==false) return;
// cout<<" first value:"<<j<<" "<<neuralList[0][j]->GetValue()<<endl;
}
for(j=0;j<numberOfHiddenlayer;j++){
for(k=0;k<numberOfNeuralsInHiddenLayer[j];k++){
// cout<<"value:"<<k<<"th "<<neuralList[j+1][k]->GetValue()<<endl;
neuralList[j+1][k]->ForwardCompute();
// cout<<"value:"<<k<<"th "<<neuralList[j+1][k]->GetValue()<<endl;
}
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
neuralList[numberOfHiddenlayer+1][j]->ForwardCompute();
// cout<<"value:"<<j<<"th "<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
// cout<<"outdata="<<sData.GetOutputVector(p,j);
// cout<<neuralList[numberOfHiddenlayer+1][j]->GetValue();
neuralList[numberOfHiddenlayer+1][j]->computeNewGradient(trainningData.GetOutputVector(p,j));
// cout<<"gradient:"<<j<<" "<<neuralList[numberOfHiddenlayer+1][j]->GetGradient()<<endl;
//////
if(neuralList[numberOfHiddenlayer+1][j]->WeightAdjust()==false) return;
// mediaList[numberOfHiddenlayer]->WeightPrint();
}
for(j=numberOfHiddenlayer-1;j>=0;j--){
for(k=0;k<numberOfNeuralsInHiddenLayer[j];k++){
// cout<<neuralList[j+1][k]->GetValue();
neuralList[j+1][k]->computeNewGradient();
// cout<<"gradient:"<<k<<" "<<neuralList[j+1][k]->GetGradient()<<endl;
if(neuralList[j+1][k]->WeightAdjust()==false) return;
// mediaList[j]->WeightPrint();
}
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
// cout<<"should"<<sData.GetOutputVector(p,j)<<endl;
// cout<<"really"<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
dtemp=(trainningData.GetOutputVector(p,j)-neuralList[numberOfHiddenlayer+1][j]->GetValue());
ddtemp+=dtemp*dtemp;
// cout<<"value:"<<j<<"th "<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
}
}
for(j=0;j<numberOfHiddenlayer+1;j++) mediaList[j]->UpdatePerIteration();
ddtemp=ddtemp/numberOfTrainningExample;
if(i*10%numberOfIterations==0||(ddtemp<=1.0e-6&&ddtemp>1.0e-7)){
cout<<i<<" e="<<ddtemp<<endl<<endl;
}
}
for(p=0;p<numberOfTrainningExample;p++){
for(j=0;j<numberOfNeuralsInInputLayer;j++){
if(neuralList[0][j]->SetInputNeuralValues(trainningData.GetInputVector(p,j))==false) return;
// cout<<" first value:"<<j<<" "<<neuralList[0][j]->GetValue()<<endl;
}
for(j=0;j<numberOfHiddenlayer;j++){
for(k=0;k<numberOfNeuralsInHiddenLayer[j];k++){
// cout<<"value:"<<k<<"th "<<neuralList[j+1][k]->GetValue()<<endl;
neuralList[j+1][k]->ForwardCompute();
// cout<<"value:"<<k<<"th "<<neuralList[j+1][k]->GetValue()<<endl;
}
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
neuralList[numberOfHiddenlayer+1][j]->ForwardCompute();
// cout<<"value:"<<j<<"th "<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
cout<<"should"<<trainningData.GetOutputVector(p,j)<<endl;
cout<<"really"<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
dtemp=(trainningData.GetOutputVector(p,j)-neuralList[numberOfHiddenlayer+1][j]->GetValue());
ddtemp+=dtemp*dtemp;
cout<<p<<"value:"<<j<<"th "<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
}
}
for(p=0;p<numberOfTestExample;p++){
for(j=0;j<numberOfNeuralsInInputLayer;j++){
if(neuralList[0][j]->SetInputNeuralValues(testData.GetInputVector(p,j))==false) return;
}
for(j=0;j<numberOfHiddenlayer;j++){
for(k=0;k<numberOfNeuralsInHiddenLayer[j];k++){
neuralList[j+1][k]->ForwardCompute();
}
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
neuralList[numberOfHiddenlayer+1][j]->ForwardCompute();
}
for(j=0;j<numberOfNeuralsInOutputLayer;j++){
cout<<"should"<<testData.GetOutputVector(p,j)<<endl;
cout<<"really"<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
dtemp=(testData.GetOutputVector(p,j)-neuralList[numberOfHiddenlayer+1][j]->GetValue());
ddtemp+=dtemp*dtemp;
cout<<p<<"value:"<<j<<"th "<<neuralList[numberOfHiddenlayer+1][j]->GetValue()<<endl;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -