谁有最简单的BP神经网络的可以的代码啊?
大家谁有可运行的简单的神经网络代码啊,网上现在能搜索道的全都无法运行,C/C++都可以,就要那种输入样本,然后就能出一个结果的,谢谢大家乐...
大家谁有可运行的简单的神经网络代码啊,网上现在能搜索道的全都无法运行,C/C++都可以,就要那种输入样本,然后就能出一个结果的,谢谢大家乐
展开
展开全部
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <ctime>
//Rumelhart,1985,only one hidden layer
const double lr = 0.1;
const double goal = 1e-1;
const size_t SampleNum = 4;
const size_t HidNeuron = 10;
const size_t Input = 2;
const size_t Output = 1;
double Weight1[Input][HidNeuron] = {0};
double Bias1[HidNeuron] = {0};
double Weight2[HidNeuron][Output] = {0};
double Bias2[Output] = {0};
double InputValue[SampleNum][Input] ={0};
double OutputValue[SampleNum][Output] ={0};
double Error[SampleNum][Output] = {0};
double MSE = 1;
void InitWeight()
{
size_t i,j;
for( i=0; i<Input; i++)
{
for(j=0; j<HidNeuron; j++)
{
Weight1[i][j] = (rand()/(double)RAND_MAX)*2 - 1;
}
}
for( i=0; i<HidNeuron; i++)
Bias1[i] = (rand()/(double)RAND_MAX)*2 - 1;
for( i=0; i<HidNeuron; i++)
{
for(j=0; j<Output; j++)
{
Weight2[i][j] = (rand()/(double)RAND_MAX)*2 - 1;
}
}
for( i=0; i<Output; i++)
Bias2[i] = (rand()/(double)RAND_MAX)*2 - 1;
}
double LogSigmoid(double x)
{
return 1/(1+exp(-x));
}
double DetLogSigmoid(double x)
{
return LogSigmoid(x) - pow(LogSigmoid(x),2.0);
}
void Computing()
{
size_t i,j,k;
double sum=0;
double hidOutput[SampleNum][HidNeuron] = {0};
double finOutput[SampleNum][Output] = {0};
double detOutput[Output] = {0};
double detHid[HidNeuron] = {0};
double simHidError[HidNeuron] ={0};
for(i=0; i<SampleNum; i++)
{
//ForwardComputing
for(j=0; j<HidNeuron; j++)
{
sum =0;
for(k=0; k<Input+1; k++)
{
if(k == Input)
sum += (-1)*Bias1[j];
else
sum += InputValue[i][k]*Weight1[k][j];
}
hidOutput[i][j] = LogSigmoid(sum);
}
for(j=0; j<Output; j++)
{
sum =0;
for(k=0; k<HidNeuron+1; k++)
{
if(k == HidNeuron)
sum += (-1)*Bias2[j];
else
sum += hidOutput[i][k]*Weight2[k][j];
}
finOutput[i][j] = LogSigmoid(sum);
Error[i][j] = OutputValue[i][j] - finOutput[i][j];
}
//BackwardComputing
for(j=0; j<Output; j++)
{
detOutput[j] = finOutput[i][j]*(1-finOutput[i][j])*Error[i][j];
for(k=0; k<HidNeuron; k++)
{
Weight2[k][j] += (lr*detOutput[j]*hidOutput[i][k]);
}
Bias2[j] += (lr*detOutput[j]*(-1));
}
for(j=0; j<HidNeuron; j++)
{
simHidError[j] = 0;
for(k=0; k<Output; k++)
{
simHidError[j] += detOutput[k]*Weight2[j][k];
}
}
for(j=0; j<HidNeuron; j++)
{
detHid[j] = hidOutput[i][j]*(1-hidOutput[i][j])*simHidError[j];
for(k=0; k<Input; k++)
{
Weight1[k][j] += (lr*detHid[j]*InputValue[i][k]);
}
Bias1[j] += (lr*detHid[j]*(-1));
}
}
MSE=0;
for(i =0; i<Output; i++)
{
for(j=0; j<SampleNum; j++)
{
MSE += (Error[j][i]*Error[j][i]);
}
}
MSE = sqrt(MSE/SampleNum);
}
int main()
{
srand(unsigned(time(NULL)));
static clock_t BeforeRunTime = clock();
InitWeight();
InputValue[0][0] = 0;
InputValue[0][1] = 0;
OutputValue[0][0] = 0;
InputValue[1][0] = 1;
InputValue[1][1] = 1;
OutputValue[1][0] = 0;
InputValue[2][0] = 0;
InputValue[2][1] = 1;
OutputValue[2][0] = 1;
InputValue[3][0] = 1;
InputValue[3][1] = 0;
OutputValue[3][0] = 1;
size_t cEpoch = 0;
while(MSE>goal)
{
cEpoch++;
Computing();
printf("MSE: %.6f\n",MSE);
}
printf("Epochs %d\n",cEpoch);
clock_t UsedTime = clock()-BeforeRunTime;
printf("UsedTime %dms\n",UsedTime);
getchar();
return 0;
}
#include <cstdlib>
#include <cstdio>
#include <ctime>
//Rumelhart,1985,only one hidden layer
const double lr = 0.1;
const double goal = 1e-1;
const size_t SampleNum = 4;
const size_t HidNeuron = 10;
const size_t Input = 2;
const size_t Output = 1;
double Weight1[Input][HidNeuron] = {0};
double Bias1[HidNeuron] = {0};
double Weight2[HidNeuron][Output] = {0};
double Bias2[Output] = {0};
double InputValue[SampleNum][Input] ={0};
double OutputValue[SampleNum][Output] ={0};
double Error[SampleNum][Output] = {0};
double MSE = 1;
void InitWeight()
{
size_t i,j;
for( i=0; i<Input; i++)
{
for(j=0; j<HidNeuron; j++)
{
Weight1[i][j] = (rand()/(double)RAND_MAX)*2 - 1;
}
}
for( i=0; i<HidNeuron; i++)
Bias1[i] = (rand()/(double)RAND_MAX)*2 - 1;
for( i=0; i<HidNeuron; i++)
{
for(j=0; j<Output; j++)
{
Weight2[i][j] = (rand()/(double)RAND_MAX)*2 - 1;
}
}
for( i=0; i<Output; i++)
Bias2[i] = (rand()/(double)RAND_MAX)*2 - 1;
}
double LogSigmoid(double x)
{
return 1/(1+exp(-x));
}
double DetLogSigmoid(double x)
{
return LogSigmoid(x) - pow(LogSigmoid(x),2.0);
}
void Computing()
{
size_t i,j,k;
double sum=0;
double hidOutput[SampleNum][HidNeuron] = {0};
double finOutput[SampleNum][Output] = {0};
double detOutput[Output] = {0};
double detHid[HidNeuron] = {0};
double simHidError[HidNeuron] ={0};
for(i=0; i<SampleNum; i++)
{
//ForwardComputing
for(j=0; j<HidNeuron; j++)
{
sum =0;
for(k=0; k<Input+1; k++)
{
if(k == Input)
sum += (-1)*Bias1[j];
else
sum += InputValue[i][k]*Weight1[k][j];
}
hidOutput[i][j] = LogSigmoid(sum);
}
for(j=0; j<Output; j++)
{
sum =0;
for(k=0; k<HidNeuron+1; k++)
{
if(k == HidNeuron)
sum += (-1)*Bias2[j];
else
sum += hidOutput[i][k]*Weight2[k][j];
}
finOutput[i][j] = LogSigmoid(sum);
Error[i][j] = OutputValue[i][j] - finOutput[i][j];
}
//BackwardComputing
for(j=0; j<Output; j++)
{
detOutput[j] = finOutput[i][j]*(1-finOutput[i][j])*Error[i][j];
for(k=0; k<HidNeuron; k++)
{
Weight2[k][j] += (lr*detOutput[j]*hidOutput[i][k]);
}
Bias2[j] += (lr*detOutput[j]*(-1));
}
for(j=0; j<HidNeuron; j++)
{
simHidError[j] = 0;
for(k=0; k<Output; k++)
{
simHidError[j] += detOutput[k]*Weight2[j][k];
}
}
for(j=0; j<HidNeuron; j++)
{
detHid[j] = hidOutput[i][j]*(1-hidOutput[i][j])*simHidError[j];
for(k=0; k<Input; k++)
{
Weight1[k][j] += (lr*detHid[j]*InputValue[i][k]);
}
Bias1[j] += (lr*detHid[j]*(-1));
}
}
MSE=0;
for(i =0; i<Output; i++)
{
for(j=0; j<SampleNum; j++)
{
MSE += (Error[j][i]*Error[j][i]);
}
}
MSE = sqrt(MSE/SampleNum);
}
int main()
{
srand(unsigned(time(NULL)));
static clock_t BeforeRunTime = clock();
InitWeight();
InputValue[0][0] = 0;
InputValue[0][1] = 0;
OutputValue[0][0] = 0;
InputValue[1][0] = 1;
InputValue[1][1] = 1;
OutputValue[1][0] = 0;
InputValue[2][0] = 0;
InputValue[2][1] = 1;
OutputValue[2][0] = 1;
InputValue[3][0] = 1;
InputValue[3][1] = 0;
OutputValue[3][0] = 1;
size_t cEpoch = 0;
while(MSE>goal)
{
cEpoch++;
Computing();
printf("MSE: %.6f\n",MSE);
}
printf("Epochs %d\n",cEpoch);
clock_t UsedTime = clock()-BeforeRunTime;
printf("UsedTime %dms\n",UsedTime);
getchar();
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询