当前位置:首页 » 编程语言 » bp神经网络c语言

bp神经网络c语言

发布时间: 2022-04-25 12:06:37

Ⅰ 求BP神经网络算法的C++源代码

// AnnBP.cpp: implementation of the CAnnBP class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "AnnBP.h"
#include "math.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAnnBP::CAnnBP()
{
eta1=0.3;
momentum1=0.3;

}

CAnnBP::~CAnnBP()
{

}

double CAnnBP::drnd()
{
return ((double) rand() / (double) BIGRND);
}

/*** 返回-1.0到1.0之间的双精度随机数 ***/
double CAnnBP::dpn1()
{
return (double) (rand())/(32767/2)-1;
}

/*** 作用函数,目前是S型函数 ***/
double CAnnBP::squash(double x)
{
return (1.0 / (1.0 + exp(-x)));
}

/*** 申请1维双精度实数数组 ***/
double* CAnnBP::alloc_1d_dbl(int n)
{
double *new1;

new1 = (double *) malloc ((unsigned) (n * sizeof (double)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_1D_DBL: Couldn't allocate array of doubles\n");
return (NULL);
}
return (new1);
}

/*** 申请2维双精度实数数组 ***/
double** CAnnBP::alloc_2d_dbl(int m, int n)
{
int i;
double **new1;

new1 = (double **) malloc ((unsigned) (m * sizeof (double *)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_2D_DBL: Couldn't allocate array of dbl ptrs\n");
return (NULL);
}

for (i = 0; i < m; i++) {
new1[i] = alloc_1d_dbl(n);
}

return (new1);
}

/*** 随机初始化权值 ***/
void CAnnBP::bpnn_randomize_weights(double **w, int m, int n)
{
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = dpn1();
}
}

}

/*** 0初始化权值 ***/
void CAnnBP::bpnn_zero_weights(double **w, int m, int n)
{
int i, j;

for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = 0.0;
}
}

}

/*** 设置随机数种子 ***/
void CAnnBP::bpnn_initialize(int seed)
{
CString msg,s;
msg="Random number generator seed:";
s.Format("%d",seed);
AfxMessageBox(msg+s);
srand(seed);
}

/*** 创建BP网络 ***/
BPNN* CAnnBP::bpnn_internal_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = (BPNN *) malloc (sizeof (BPNN));
if (newnet == NULL) {
printf("BPNN_CREATE: Couldn't allocate neural network\n");
return (NULL);
}

newnet->input_n = n_in;
newnet->hidden_n = n_hidden;
newnet->output_n = n_out;
newnet->input_units = alloc_1d_dbl(n_in + 1);
newnet->hidden_units = alloc_1d_dbl(n_hidden + 1);
newnet->output_units = alloc_1d_dbl(n_out + 1);

newnet->hidden_delta = alloc_1d_dbl(n_hidden + 1);
newnet->output_delta = alloc_1d_dbl(n_out + 1);
newnet->target = alloc_1d_dbl(n_out + 1);

newnet->input_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

newnet->input_prev_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_prev_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

return (newnet);

}

/* 释放BP网络所占地内存空间 */
void CAnnBP::bpnn_free(BPNN *net)
{
int n1, n2, i;

n1 = net->input_n;
n2 = net->hidden_n;

free((char *) net->input_units);
free((char *) net->hidden_units);
free((char *) net->output_units);

free((char *) net->hidden_delta);
free((char *) net->output_delta);
free((char *) net->target);

for (i = 0; i <= n1; i++) {
free((char *) net->input_weights[i]);
free((char *) net->input_prev_weights[i]);
}
free((char *) net->input_weights);
free((char *) net->input_prev_weights);

for (i = 0; i <= n2; i++) {
free((char *) net->hidden_weights[i]);
free((char *) net->hidden_prev_weights[i]);
}
free((char *) net->hidden_weights);
free((char *) net->hidden_prev_weights);

free((char *) net);
}

/*** 创建一个BP网络,并初始化权值***/
BPNN* CAnnBP::bpnn_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = bpnn_internal_create(n_in, n_hidden, n_out);

#ifdef INITZERO
bpnn_zero_weights(newnet->input_weights, n_in, n_hidden);
#else
bpnn_randomize_weights(newnet->input_weights, n_in, n_hidden);
#endif
bpnn_randomize_weights(newnet->hidden_weights, n_hidden, n_out);
bpnn_zero_weights(newnet->input_prev_weights, n_in, n_hidden);
bpnn_zero_weights(newnet->hidden_prev_weights, n_hidden, n_out);

return (newnet);

}

void CAnnBP::bpnn_layerforward(double *l1, double *l2, double **conn, int n1, int n2)
{
double sum;
int j, k;

/*** 设置阈值 ***/
l1[0] = 1.0;

/*** 对于第二层的每个神经元 ***/
for (j = 1; j <= n2; j++) {

/*** 计算输入的加权总和 ***/
sum = 0.0;
for (k = 0; k <= n1; k++) {
sum += conn[k][j] * l1[k];
}
l2[j] = squash(sum);
}
}

/* 输出误差 */
void CAnnBP::bpnn_output_error(double *delta, double *target, double *output, int nj, double *err)
{
int j;
double o, t, errsum;

errsum = 0.0;
for (j = 1; j <= nj; j++) {
o = output[j];
t = target[j];
delta[j] = o * (1.0 - o) * (t - o);
errsum += ABS(delta[j]);
}
*err = errsum;

}

/* 隐含层误差 */
void CAnnBP::bpnn_hidden_error(double *delta_h, int nh, double *delta_o, int no, double **who, double *hidden, double *err)
{
int j, k;
double h, sum, errsum;

errsum = 0.0;
for (j = 1; j <= nh; j++) {
h = hidden[j];
sum = 0.0;
for (k = 1; k <= no; k++) {
sum += delta_o[k] * who[j][k];
}
delta_h[j] = h * (1.0 - h) * sum;
errsum += ABS(delta_h[j]);
}
*err = errsum;
}

/* 调整权值 */
void CAnnBP::bpnn_adjust_weights(double *delta, int ndelta, double *ly, int nly, double **w, double **oldw, double eta, double momentum)
{
double new_dw;
int k, j;

ly[0] = 1.0;
for (j = 1; j <= ndelta; j++) {
for (k = 0; k <= nly; k++) {
new_dw = ((eta * delta[j] * ly[k]) + (momentum * oldw[k][j]));
w[k][j] += new_dw;
oldw[k][j] = new_dw;
}
}

}

/* 进行前向运算 */
void CAnnBP::bpnn_feedforward(BPNN *net)
{
int in, hid, out;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** Feed forward input activations. ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

}

/* 训练BP网络 */
void CAnnBP::bpnn_train(BPNN *net, double eta, double momentum, double *eo, double *eh)
{
int in, hid, out;
double out_err, hid_err;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** 前向输入激活 ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

/*** 计算隐含层和输出层误差 ***/
bpnn_output_error(net->output_delta, net->target, net->output_units,
out, &out_err);
bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out,
net->hidden_weights, net->hidden_units, &hid_err);
*eo = out_err;
*eh = hid_err;

/*** 调整输入层和隐含层权值 ***/
bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid,
net->hidden_weights, net->hidden_prev_weights, eta, momentum);
bpnn_adjust_weights(net->hidden_delta, hid, net->input_units, in,
net->input_weights, net->input_prev_weights, eta, momentum);
}

/* 保存BP网络 */
void CAnnBP::bpnn_save(BPNN *net, char *filename)
{
CFile file;
char *mem;
int n1, n2, n3, i, j, memcnt;
double dvalue, **w;
n1 = net->input_n; n2 = net->hidden_n; n3 = net->output_n;
printf("Saving %dx%dx%d network to '%s'\n", n1, n2, n3, filename);
try
{
file.Open(filename,CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

file.Write(&n1,sizeof(int));
file.Write(&n2,sizeof(int));
file.Write(&n3,sizeof(int));

memcnt = 0;
w = net->input_weights;
mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double)));
// mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
dvalue = w[i][j];
//fast(&mem[memcnt], &dvalue, sizeof(double));
fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);

}
}

file.Write(mem,sizeof(double)*(n1+1)*(n2+1));
free(mem);

memcnt = 0;
w = net->hidden_weights;
mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double)));
// mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));
for (i = 0; i <= n2; i++) {
for (j = 0; j <= n3; j++) {
dvalue = w[i][j];
fast(&mem[memcnt], &dvalue, sizeof(double));
// fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);
}
}

file.Write(mem, (n2+1) * (n3+1) * sizeof(double));
// free(mem);

file.Close();
return;
}

/* 从文件中读取BP网络 */
BPNN* CAnnBP::bpnn_read(char *filename)
{
char *mem;
BPNN *new1;
int n1, n2, n3, i, j, memcnt;
CFile file;

try
{
file.Open(filename,CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

// printf("Reading '%s'\n", filename);// fflush(stdout);

file.Read(&n1, sizeof(int));
file.Read(&n2, sizeof(int));
file.Read(&n3, sizeof(int));

new1 = bpnn_internal_create(n1, n2, n3);

// printf("'%s' contains a %dx%dx%d network\n", filename, n1, n2, n3);
// printf("Reading input weights..."); // fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));

file.Read(mem, ((n1+1)*(n2+1))*sizeof(double));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
//fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);
}
}
free(mem);

// printf("Done\nReading hidden weights..."); //fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));

file.Read(mem, (n2+1) * (n3+1) * sizeof(double));
for (i = 0; i <= n2; i++) {

for (j = 0; j <= n3; j++) {
//fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);

}
}
free(mem);
file.Close();

printf("Done\n"); //fflush(stdout);

bpnn_zero_weights(new1->input_prev_weights, n1, n2);
bpnn_zero_weights(new1->hidden_prev_weights, n2, n3);

return (new1);
}

void CAnnBP::CreateBP(int n_in, int n_hidden, int n_out)
{
net=bpnn_create(n_in,n_hidden,n_out);
}

void CAnnBP::FreeBP()
{
bpnn_free(net);

}

void CAnnBP::Train(double *input_unit,int input_num, double *target,int target_num, double *eo, double *eh)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}

for(int j=1;j<=target_num;j++)
{
net->target[j]=target[j-1];
}
bpnn_train(net,eta1,momentum1,eo,eh);

}

void CAnnBP::Identify(double *input_unit,int input_num,double *target,int target_num)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}
bpnn_feedforward(net);
for(int j=1;j<=target_num;j++)
{
target[j-1]=net->output_units[j];
}
}

void CAnnBP::Save(char *filename)
{
bpnn_save(net,filename);

}

void CAnnBP::Read(char *filename)
{
net=bpnn_read(filename);
}

void CAnnBP::SetBParm(double eta, double momentum)
{
eta1=eta;
momentum1=momentum;

}

void CAnnBP::Initialize(int seed)
{
bpnn_initialize(seed);

}

Ⅱ 前馈神经网络(BP算法)和后馈神经网络(优化) 要求c语言实现!

这个太难了撒,只会用C编什么运算之类的,无能为力,惭愧啊

Ⅲ 最近在学习人工神经网络算法,哪位能发一个含有c语言原程序的BP算法实例哪其它语言的也可以

附件是BP神经网络的C语言实现。


BP(Back Propagation)神经网络是86年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一。BP网络能学习和存贮大量的输入-输出模式映射关系,而无需事前揭示描述这种映射关系的数学方程。它的学习规则是使用最速下降法,通过反向传播来不断调整网络的权值和阈值,使网络的误差平方和最小。BP神经网络模型拓扑结构包括输入层(input)、隐层(hidden layer)和输出层(output layer)。

Ⅳ 你好,请问你有用C语言写的用BP神经网络去逼近一个函数的程序吗

#include<stdio.h>
void xu(int n);
void main()
{
int n;
do
{
printf("Input n:\n");
scanf("%d",&n);
}while(n>99999);
xu(n);
}

void xu(int n)
{
int a,i=0,count=0;
int b[10];
while(n!=0)
{
a=n%10;
n=n/10;
count++;
b[i]=a;
i++;
printf("%d",a);
}
printf("\n");
printf("该数为%d位数\n",count);
for(i=count-1;i>=0;i--)
printf("%d\n",b[i]);
printf("下面是奇偶性判断:\n");
if(b[count-1]/2==0)
printf("1\n");
else
printf("0\n");
}
我用的vc6.0可以处理汉字,如果你用的tc的话自己把汉字换成其他字母描述,否则就是乱码!希望满意,不晓得满足你的最简不
希望对你有帮助

Ⅳ 如何用C语言编程实现多层前向BP神经网络,用来解

https://zhuanlan.hu.com/p/26089367
看看吧

Ⅵ BP神经网络的预测(回归)过程C语言程序

神经网络本质也是一种数据回归模型。我们举个简单的例子
y=ax^2+bx+c,
有三个系数要回归,如果只给你1~2组数据,你觉得能回归好吗?
考虑到样本本身有误差,为了防止过拟合(或过学习),一般要求神经网络的训练样本数是连接权系数(包括阈值)的2~3倍。你用了太多的连接权系数(包括阈值),即使效果不错,风险仍然很高。

Ⅶ BP神经网络代码用什么实现

BP算法,只是一种算法,用任何语言都能实现。
Matlab有神经网络工具箱,提供已经封装好的:网络建立函数newff、训练函数train,省去了自己编写代码的麻烦,你可以考虑。

BP(Back Propagation)神经网络是1986年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一。BP网络能学习和存贮大量的输入-输出模式映射关系,而无需事前揭示描述这种映射关系的数学方程。它的学习规则是使用最速下降法,通过反向传播来不断调整网络的权值和阈值,使网络的误差平方和最小。BP神经网络模型拓扑结构包括输入层(input)、隐层(hidden layer)和输出层(output layer)。

Ⅷ 什么是BP神经网络

BP算法的基本思想是:学习过程由信号正向传播与误差的反向回传两个部分组成;正向传播时,输入样本从输入层传入,经各隐层依次逐层处理,传向输出层,若输出层输出与期望不符,则将误差作为调整信号逐层反向回传,对神经元之间的连接权矩阵做出处理,使误差减小。经反复学习,最终使误差减小到可接受的范围。具体步骤如下:
1、从训练集中取出某一样本,把信息输入网络中。
2、通过各节点间的连接情况正向逐层处理后,得到神经网络的实际输出。
3、计算网络实际输出与期望输出的误差。
4、将误差逐层反向回传至之前各层,并按一定原则将误差信号加载到连接权值上,使整个神经网络的连接权值向误差减小的方向转化。
5、対训练集中每一个输入—输出样本对重复以上步骤,直到整个训练样本集的误差减小到符合要求为止。

Ⅸ 用C语言解决BP神经网络算法

囧,用C语言干嘛,matlab里面带有神经网络函数库的……

55555,我的论文也是用神经网络仿真……

我的隐藏层个数都没确定……输入节点数也没有定……

matlab神经网络函数库的程序,在ilovematlab论坛上有。

热点内容
java日期之间的天数 发布:2025-07-16 08:21:38 浏览:960
我的世界挖钻石赚钱的服务器 发布:2025-07-16 08:15:18 浏览:722
python进程获取 发布:2025-07-16 08:15:10 浏览:406
安卓系统优盘打不开怎么回事 发布:2025-07-16 08:14:34 浏览:762
猫耳fm上传 发布:2025-07-16 08:13:42 浏览:470
php培训学费多少 发布:2025-07-16 07:56:15 浏览:521
输入url或服务器地址 发布:2025-07-16 07:47:46 浏览:295
iphone便捷访问有什么用 发布:2025-07-16 07:46:59 浏览:843
16进制的字符串java 发布:2025-07-16 07:39:54 浏览:439
一加手机安卓11怎么分屏 发布:2025-07-16 07:32:35 浏览:87