# include <conio.h>
# include <stdlib.h>
# include <time.h>
# include <math.h>
# include <stdio.h>
# include <string.h>
/*********************************/
# define inpoints 2
# define outpoints 1
# define defaultpoints 8
# define datagroup 500
# define testdatagroup 1000
# define ALFA 0.25
# define BETA 0.1
char * datafile = "data.dat";
/*********************************/
void make_wv( w, v, dlta_v, dlta_w) /*随机赋初值*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1];
float dlta_v [defaultpoints + 1], dlta_w [defaultpoints] [inpoints + 1];
{
int i, j;
randomize();
for (i = 0; i < defaultpoints; i++ ) {
for (j = 0; j <= inpoints; j++ ) {
w[i][j] = + ((float) rand()/RAND_MAX - (float) rand()/RAND_MAX);/**/
}
}
for (i = 0; i <= defaultpoints; i++ ) {
v[i] = + ((float) rand()/RAND_MAX - (float) rand()/RAND_MAX);
}
for (i = 0; i < defaultpoints; i++ ) {
for (j = 0; j <= inpoints; j++ ) {
dlta_w [i] [j] = 0.0;
}
}
for (i = 0; i <= defaultpoints; i++ ) {
dlta_v [i] = 0.0;
}
}
void make_x(fp, x, d, group) /*产生训练样本或测试样本*/
FILE *fp;
int group;
float x[datagroup][inpoints + 1],d[datagroup];
{
int i, j;
randomize();
for (i = 0; i < group; i++ ) {
for (j = 0; j < inpoints; j++ ) {
x[i][j] = (float) rand()/RAND_MAX - (float) rand()/RAND_MAX;
}
x[i][inpoints] = 1.0;
if (x[i][0]*x[i][1] < 1e-6) d[i]=0;
else d[i]=1;
}
for (i = 0; i < group; i++ ) {
for (j = 0; j < inpoints; j++ ) {
fprintf(fp,"%f ", x[i][j]);
}
fprintf(fp,"%f ", d[i]);
fprintf(fp,"\n");
}
}
void out_sub1 (w, v ,x ,out, y, k) /*正向传播*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1], x [datagroup] [inpoints + 1];
float out[defaultpoints + 1], * y;
int k;
{
int i,j;
float (* wp) [inpoints + 1],(* xp) [inpoints +1];
float sum1,sum2=0;
wp = w; xp = x; * y = 0;
for (i=0; i < defaultpoints; i++) {
sum1 = 0;
for (j = 0; j <= inpoints; j++ ) {
sum1 += (* (* (wp+i)+j)) * (* (* (xp+k)+j));
}
out[i] = 1.0/(1.0 + exp(-sum1));
}
out[defaultpoints]=1.0;
for (i=0; i <= defaultpoints; i++) {
sum2 = sum2 + v [i] * out[i];
}
*y = 1.0/(1.0 + exp(-sum2));
}
void out_sub2(v, x, o, d, y, k, dlta_v, dlta_w)
float v [defaultpoints], x [datagroup] [inpoints + 1],* d;
float * y, o [datagroup] [defaultpoints + 1];
int k;
float dlta_v [defaultpoints + 1], dlta_w [defaultpoints] [inpoints + 1]; /*反向传播*/
{
int i,j;
for(i = 0; i < defaultpoints; i++){
for(j = 0; j <= inpoints; j++){
dlta_w [i] [j] += (d [k] - y [k]) * y[k] * (1-y[k])* v [i]* o [k] [i]
* (1 - o [k] [i]) * x [k] [j];
/* printf(" dlta_w [%d] [%d]= %f\n",i,j,dlta_w [i] [j]);*/
}
}
for(i = 0; i <= defaultpoints; i++){
dlta_v [i] += (d [k] - y [k]) * y[k] * (1-y[k]) * o [k] [i];
/* printf(" dlta_v [%d]= %f\n",i,dlta_v [i]);*/
}
}
void myw (w, v, dlta_w, dlta_v, x, d, o, y, n, fp) /*写权值*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1];
float dlta_w [defaultpoints] [inpoints + 1], dlta_v [defaultpoints + 1];
float x [datagroup] [inpoints + 1], d [datagroup];
float o [datagroup] [defaultpoints + 1], y [datagroup];
int n;
FILE * fp;
{
int i,j;
fprintf(fp, "\n\n");
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fprintf(fp,"%f ", w[j][i]);
}
fprintf(fp,"\n");
}
for(j=0; j <= defaultpoints; j++){
fprintf(fp,"%f ", v[j]);
}
fprintf(fp,"\n");
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fprintf(fp,"%f ", dlta_w[j][i]);
}
fprintf(fp,"\n");
}
for(j=0; j <= defaultpoints; j++){
fprintf(fp,"%f ", dlta_v[j]);
}
fprintf(fp,"\n");
for(i=0; i < datagroup; i++){
for(j=0;j <= inpoints; j++){
fprintf(fp,"%8.6f ",x [i] [j]);
}
fprintf(fp,"%10.8f %10.8f",d[i],y[i]);
fprintf(fp,"\n");
}
for(i=0;i < datagroup; i++){
for(j=0;j<=defaultpoints;j++){
fprintf(fp,"%f ",o[i][j]);
}
}
fprintf(fp,"\n%d",n);
}
void myr (w, v, dlta_w, dlta_v, x, d, o, y, n, fp) /*读权值*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1];
float dlta_w [defaultpoints] [inpoints + 1], dlta_v [defaultpoints + 1];
float x [datagroup] [inpoints + 1], d [datagroup];
float o [datagroup] [defaultpoints + 1], y [datagroup];
int *n;
FILE * fp;
{
int i,j;
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fscanf(fp,"%f", &w[j][i]);
}
}
for(j=0; j <= defaultpoints; j++){
fscanf(fp,"%f", &v[j]);
}
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fscanf(fp,"%f", &dlta_w[j][i]);
}
}
for(j=0; j <= defaultpoints; j++){
fscanf(fp,"%f", &dlta_v[j]);
}
for(i=0; i < datagroup; i++){
for(j=0;j <= inpoints; j++){
fscanf(fp,"%8.6f",&x [i] [j]);
}
fscanf(fp,"%10.8f%10.8f",&d[i],&y[i]);
}
for(i=0;i < datagroup; i++){
for(j=0; j <= defaultpoints; j++){
fscanf(fp,"%f",&o[i][j]);
}
}
fscanf(fp,"\n%d",n);
}
void main()
{
int h,i,j,k,n,ch,count=0;
float w [defaultpoints] [inpoints + 1], dlta_w [defaultpoints] [inpoints + 1];
float x [datagroup] [inpoints + 1],xc[inpoints + 1],tstx [testdatagroup] [inpoints + 1];
float o [datagroup] [defaultpoints + 1], outc[defaultpoints + 1] ,y [datagroup];
float v [defaultpoints + 1], dlta_v [defaultpoints + 1];
float d [datagroup],tstd [testdatagroup];
float e,earr,rate;
float yc,sigma;
FILE *fp, *fpq, *fpt;
printf("C:读数据文件QR.DAT并进行数据拟合运算用于测试\n");
printf("其它键:开始执行程序\n");
ch = toupper(getche());
if (ch=='C'){
if((fpq=fopen("qr.dat","rt")) == NULL){ /*读qr.dat*/
printf("open file QR.DAT error");
exit(0);
}
if((fpt=fopen("testdata.dat","wt")) == NULL){
printf("open file testdata.DAT error");
exit(0);
}
make_x(fpt,tstx,tstd,testdatagroup); /*产生测试样本并写进testdata.dat*/
myr(w, v, dlta_w, dlta_v, x, d, o, y, &n, fpq);
fscanf(fpq, "%f", &e);
fclose(fpq);
/*测试并计算和输出正确率*/
for(h=0; h < testdatagroup; h++){
out_sub1 (w, v ,tstx ,outc, &yc, h);
fprintf(fpt,"\n%f ",yc);
if((tstd[h] >=0.5)&&(yc >=0.5)||((tstd[h] < 0.5)&&(yc <0.5)))
count+=1;
}
rate = ((float)count/(float)testdatagroup)*100;
printf("\ncorrect rate=%f%%\n",rate);
fprintf(fpt,"\n %f%%",rate);
fclose(fpt);
exit(0);
}
else{
make_wv(w, v, dlta_v, dlta_w);
printf("\n------w------\n"); /*屏幕输出初置*/
for (j=0; j < defaultpoints; j++){
for (i=0; i <= inpoints; i++){
printf("%f",w [j] [i]);
}
printf("\n");
}
printf("\n------v------\n");
for (j=0; j <= defaultpoints; j++){
printf("%f",v [j]);
}
printf("\n-----dlta_w-----\n");
for (j=0; j < defaultpoints; j++){
for (i=0; i <= inpoints; i++){
printf("%f",dlta_w [j] [i]);
}
printf("\n");
}
printf("\n------dlta_v------\n");
for (j=0; j <= defaultpoints; j++){
printf("%f",dlta_v [j]);
}
printf("\n");
if((fp=fopen(datafile,"wt")) == NULL){
printf("open file %s error",datafile);
exit(0);
}
/* printf("\n------1------\n");*/
make_x(fp,x,d,datagroup); /*产生训练样本并写入data.dat*/
fclose(fp);
n=1;
}
/*训练*/
do{
/* printf("------do-1------\n");*/
for (i =
alvarocfc
- 粉丝: 131
- 资源: 1万+
最新资源
- VmwareHardenedLoader.zip
- Labview通过FINS tcp协议与欧姆龙PLC通讯,支持CIO区,W区,D区,布尔量,整数,浮点数,字符串读写操作,软件无加密
- 英特尔2021-2024年网络连接性和IPU路线图
- Intouch2020R2SP1与西门子1500PLC通讯配置手册
- 电池组散热分析 ansys 流体 fluent
- 陀螺仪选型陀螺仪陀螺仪选型型陀螺仪选型
- 快速排序算法Python实现:详解分治法原理与高效排序步骤
- STM32F401,使用ST-link时候,不能识别,显示ST-LINK USB communication error
- Avue.js是基于现有的element-plus库进行的二次封装,简化一些繁琐的操作,核心理念为数据驱动视图,主要的组件库针对table表格和form表单场景,同时衍生出更多企业常用的组件,达到高复
- COMSOL 准 BIC控制石墨烯临界耦合光吸收 COMSOL 光学仿真,石墨烯,光吸收,费米能级可调下图是仿真文件截图,所见即所得
- Intel-633246-eASIC-PB-006-N5X-Product-Brief .pdf
- 家庭用具检测21-YOLO(v5至v11)、COCO、Paligemma、TFRecord、VOC数据集合集.rar
- 51单片机仿真摇号抽奖机源程序12864液晶显示仿真+程序
- Pear Admin 是 一 款 开 箱 即 用 的 前 端 开 发 模 板,提供便捷快速的开发方式,延续 Admin 的设计规范
- ECSHOP模板堂最新2017仿E宠物模板 整合ECTouch微分销商城
- 完结26章Java主流分布式解决方案多场景设计与实战
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈