Last active
May 18, 2017 06:59
-
-
Save ryoppippi/e2949a3eda88dbff3869e0977ec18213 to your computer and use it in GitHub Desktop.
授業で作ったNeural Networkでxの二乗を学習するやつ #cpp #NN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| #define FOR(i,a,b) for (auto i=(a);i<(b);i++) | |
| #define REP(i,n) for (auto i=0;i<(n);i++) | |
| #define EPS 1e-15 | |
| #define I 1 | |
| #define H 4 | |
| #define O 1 | |
| #define N 500 | |
| #define alpha 0.35 | |
| double x[N][I], y[N][O]; | |
| double w[H][I+1], wt[O][H+1]; | |
| using namespace std; | |
| double xrand(){ | |
| return (double) rand()/RAND_MAX; | |
| } | |
| void datainit(){ | |
| srand(1); | |
| REP(i, N){ | |
| x[i][0]=(double) xrand(); | |
| /* y = x * x + NOISE */ | |
| y[i][0]=x[i][0]*x[i][0]+EPS*xrand(); | |
| } | |
| } | |
| void netinit(){ | |
| /* initialize weights at random */ | |
| REP(i, I+1){ | |
| REP(j, H) w[j][i]=xrand(); | |
| } | |
| REP(i, H+1){ | |
| REP(j, O) wt[j][i]=xrand(); | |
| } | |
| /* learning rate */ | |
| } | |
| double f(double x){ | |
| double t, ti; | |
| if(fabs(x) > 20.0) return (x>0 ? 1.0:-1.0); | |
| else{ | |
| t = exp(x); | |
| ti = 1/exp(x); | |
| return (t - ti)/(t+ti); | |
| } | |
| } | |
| double fp(double x){ | |
| return 1 - pow(f(x),2); | |
| } | |
| double g(double x){ | |
| return x; | |
| } | |
| double gp(double x){ | |
| return 1; | |
| } | |
| double ynet(double x[]){ | |
| double s[H],st[H],h[H],t,y[O]; | |
| REP(j, H){ | |
| t = w[j][0]*1; // add bias | |
| FOR(i,1, I+1) t+=w[j][i]*x[i-1]; | |
| s[j] = t; //sum | |
| h[j] = f(s[j]); | |
| } | |
| REP(j, O){ | |
| t= wt[j][0]; | |
| FOR(i, 1, H+1)t +=wt[j][i]*h[i-1]; | |
| st[j] = t; | |
| y[j] = g(st[j]); | |
| } | |
| return y[0]; | |
| } | |
| void netstep(double x[], double yd[]){ | |
| double z, s[H], st[O], h[H], t, y[O]; | |
| REP(j, H){ | |
| t = w[j][0]; | |
| FOR(i,1, I+1){ | |
| t+=w[j][i]*x[i-1]; | |
| } | |
| s[j] = t; | |
| h[j] = f(s[j]); | |
| } //hidden layer | |
| REP(j, O){ | |
| t = wt[j][0]; | |
| FOR(i,1, H+1){ | |
| t+=wt[j][i]*h[i-1]; | |
| } | |
| st[j] = t; | |
| y[j] = g(st[j]); | |
| } //output | |
| /* update weight */ | |
| REP(j, O){ | |
| wt[j][0] += alpha*(yd[j]-y[j])*gp(st[j]); | |
| FOR(i,1, H+1){ | |
| wt[j][i]+=alpha*(yd[j]-y[j])*gp(st[j])*h[i-1]; | |
| } | |
| } | |
| REP(i, I+1){ | |
| REP(j, H){ | |
| z = 0; | |
| REP(k, O)z+=gp(st[k])*wt[k][j+1]*(yd[k]-y[k]); | |
| z=z*fp(s[j]); | |
| if(i==0)w[j][i]+=alpha*z; | |
| else w[j][i] +=alpha*z*x[i-1]; | |
| } | |
| } | |
| } | |
| int main(int argc, char const *argv[]) { | |
| srand((unsigned)time(NULL)); | |
| int j=0, jstep=400; | |
| double xt[1]; | |
| datainit(); | |
| netinit(); | |
| while(j<jstep){ | |
| REP(i, N){ | |
| netstep(x[i],y[i]); | |
| } | |
| j++; | |
| } | |
| /* check the network */ | |
| REP(i, 10+1){ | |
| xt[0]=i*0.1; | |
| printf("x=%7.5f y=%7.5f ynet=%+7.5f\n",xt[0],xt[0]*xt[0],ynet(xt)); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment