📄 lms_1.c
字号:
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#define SampleNumber 500 //the number of the train sample
#define Mode 20 //the number of the mode net
#define PI 3.1415926
#define MU 0.001 //learning rate
main()
{
double input[SampleNumber];//the input og the network
double d_hopeoutput[SampleNumber]; //hope output of the network
double output[SampleNumber]; // the real output of the network
double weight[Mode]; // the weight of the weight
void inital(double input[SampleNumber],double d_hopeoutput[SampleNumber],double weight[Mode]); // inital the input,hopeoutput,and weight
void train(double input[SampleNumber],double d_hopeoutput[SampleNumber],double output[SampleNumber],double weight[Mode]);
inital(input,d_hopeoutput,weight);
train(input,d_hopeoutput,output,weight);
}
void inital(double input[SampleNumber],double d_hopeoutput[SampleNumber],double weight[Mode])
{
double gauss(double mean,double sigma,int *s);
int i;
double mean=0;
double sigma=1.0;
int seed=13579;
FILE *FP=NULL;
FILE *FW=NULL;
for (i=0;i<Mode;i++)
weight[i]=0.0;
FP=fopen("hopeoutput.txt","w");
FW=fopen("input.txt","w");
if(FP==NULL) {
printf("error to open the output file,press any key to exit\n");
getch();
exit(0);
}
for(i=0;i<SampleNumber;i++)
{
//input[i]=sqrt(2.0)*sin(2*PI*i/20.0);
d_hopeoutput[i]=sqrt(2.0)*sin(2*PI*i/20.0);
input[i]=d_hopeoutput[i]+gauss(mean,sigma,&seed);
fprintf(FP,"%lf\n",d_hopeoutput[i]);
fprintf(FW,"%lf\n",input[i]);
}
close(FP);
fclose(FW);
//for(i=0;i<SampleNumber;i++)
}
double gauss(double mean,double sigma,int *s)
{
double uniform(double a,double b,int *seed);
int i;
double x,y;
x=0;
for(i=0;i<12;i++)
x+=uniform(0.0,1.0,s);
x=x-6.0;
y=mean+x*sigma;
return(y);
}
double uniform(double a,double b,int *seed)
{
double t;
*seed=2045*(*seed)+1;
*seed=*seed-(*seed/1048576)*1048576;
t=(*seed)/1048576.0;
t=a+(b-a)*t;
return(t);
}
void train(double input[SampleNumber],double d_hopeoutput[SampleNumber],double output[SampleNumber],double weight[Mode])
{
int count=0;
int i=0;
double flag=0,TotalError=0;
FILE *FP=0;
double lms(double input[SampleNumber],double d_hopeoutput[SampleNumber],double output[SampleNumber],double weight[Mode]);
FP=fopen("error.txt","w");
if(FP==NULL) {
printf("error to open the error file,press any key to exit\n");
getch();
exit(0);
}
while(count<50)
{
TotalError=lms(input,d_hopeoutput,output,weight);
fprintf(FP,"%lf\n",TotalError);
count++;
}
fclose(FP);
printf("%d\n",count);
FP=fopen("weight.txt","w");
if(FP==NULL) {
printf("error to open the weight file,press any key to exit\n");
getch();
exit(0);
}
for(i=0;i<Mode;i++)
{
fprintf(FP,"%lf\n",weight[i]);
}
fclose(FP);
FP=fopen("output.txt","w");
if(FP==NULL) {
printf("error to open the output file,press any key to exit\n");
getch();
exit(0);
}
for(i=0;i<SampleNumber;i++)
{
fprintf(FP,"%lf\n",output[i]);
}
fclose(FP);
}
double lms(double input[SampleNumber],double d_hopeoutput[SampleNumber],double output[SampleNumber],double weight[Mode])
{
int i,k;
double error=0;
double TotalError=0;
FILE *FP=NULL;
FP=fopen("err.txt","w++");
for(k=0;k<Mode;k++)
{
output[k]=0;
for(i=0;i<=k;i++)
{
output[k]+=input[k-i]*weight[i];
}
error=d_hopeoutput[k]-output[k];
fprintf(FP,"%lf\n",error);
for(i=0;i<=k;i++)
{
weight[i]+=2.0*MU*error*input[k-i];
}
TotalError+=error;
}
for (k=Mode;k<SampleNumber;k++)
{
output[k]=0;
for(i=0;i<Mode;i++)
{
output[k]+=input[k-i]*weight[i];
}
error=d_hopeoutput[k]-output[k];
for(i=0;i<Mode;i++)
{
weight[i]+=2.0*MU*error*input[k-i];
}
fprintf(FP,"%lf\n",error);
TotalError+=error;
}
fclose(FP);
return(TotalError);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -