c语言可以实现深度定制吗,C语言实现深度学习

# include

# include

# include

# define INPUTNO 2/*Cell num of input*/

# define ALPHA 1/*learning rate*/

# define MAXNO 100/*maximum of training data*/

# define BIGNUM 100/*initialize the error*/

# define LIMIT 0.001/*limit of the random_error*/

# define SEED 63553/*random seed*/

# define _CRT_SECURE_NO_WARNINGS

void initwo(double wo[INPUTNO+1]);/*randomly initialize the weighten of input*/

int getdata(double e[][INPUTNO+1]); /*input the learning data*/

double forward(double wo[INPUTNO+1],double e[INPUTNO+1]);/*compute in turn*/

void olearn(double wo[INPUTNO+1],double e[INPUTNO+1], double o);/*output the weighten*/

void printweight(double wo[INPUTNO+1]);/*output the result*/

double s(double u); /*sigmoid function*/

double drand(void);/*generate number from -1 to 1*/

int main() {

double wo[INPUTNO + 1];/*w of output*/

double e[MAXNO][INPUTNO + 1];/*learning data*/

double o;/*output*/

double err = BIGNUM;/*evaluate the error*/

int i, j;/*for cycle*/

int n_of_e;/*size of learning data*/

int count = 0;

srand(SEED);/*initialize the random seed*/

/*initialize and print the weight*/

initwo(wo);

printweight(wo);

/*input the learning data*/

n_of_e = getdata(e);

while (err > LIMIT) {

err = 0.0;

for (j = 0; j < n_of_e; j++) {

/*compute in turn*/

o = forward(wo, e[j]);/*positively forward*/

olearn(wo,e[j],o);/*neg forward*/

err+=(o-e[j][INPUTNO])*(o-e[j][INPUTNO]);/*compute the error which need to be minimize*/

}

++count;

}

printweight(wo);

for(i=0;i

printf("%d\n",i);

for(j=0;j

printf("%lf",e[i][j]);

o=forward(wo,e[i]);

printf("%lf\n",o);

}

}

return 0;

}

/*

The training data is divided according to the number of neuron units, and function will return the

number of training data in the spilit dataset

*/

double s(double u){

return 1.0/(1.0+exp(-u));

}

int getdata(double e[][INPUTNO+1]){

int n_of_e=0;

int j;

while((scanf("%lf",&e[n_of_e][j])!=EOF)){

++j;

if(j>INPUTNO){

j=0;

n_of_e+=1;

if(n_of_e>=MAXNO){

fprintf(stderr,"The data input has been reached the superior limit\n");

break;

}

}

}

return n_of_e;

}

/*

*here might make mistakes, pay attention plz.

*/

void initwo(double wo[INPUTNO+1]){

int i;

for(i=0;i

wo[i]=drand();

}

}

/*forward function*/

double forward(double wo[INPUTNO+1],double e[INPUTNO+1]){

int i;

double o;

o=0;

for(i=0;i

o+=e[i]*wo[i];/*consider wo as both weights and bias*/

o-=wo[i];/*consider wo as both weights and bias*/

}

/*s represent the sigmoid function*/

return s(o);

}

void olearn(double wo[INPUTNO+1],double e[INPUTNO+1], double o){

int i;

double d;

d=(e[INPUTNO]-o)*o*(1-o);

for(i=0;i

wo[i]+=ALPHA*e[i]*d;/*function from deriving the sigmoid function*/

}

wo[i]+=ALPHA*(-1.0)*d;

}

void printweight(double wo[INPUTNO+1]){

int i;

for(i=0;i

printf("%lf\n",wo[i]);

printf("\n");

}

}

double drand(void){

double rndno;

rndno=rand()/(RAND_MAX/2);

rndno-=1;

return rndno;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值