Last active
August 29, 2015 14:17
-
-
Save maekawatoshiki/e7504923af563918a748 to your computer and use it in GitHub Desktop.
NeuralNetworks_Weather
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 <cstdio> | |
#include <cstdlib> | |
#include <cmath> | |
#include <ctime> | |
#include <iostream> | |
#include <unistd.h> | |
using namespace std; | |
#define NUM_LEARN 80000000 | |
#define NUM_SAMPLE 310 | |
#define NUM_INPUT 3 | |
#define NUM_HIDDEN 8 | |
#define NUM_OUTPUT 1 | |
#define RTMAX 50 | |
double sigmoid(double x) | |
{ | |
return 1.0 / (1.0+exp(-x)); | |
} | |
struct NN | |
{ | |
double x[NUM_INPUT]; | |
double y[NUM_OUTPUT]; | |
}; | |
unsigned int w = 123426572; | |
unsigned int xor128() | |
{ | |
static unsigned int x = 123456789, y = 362436069, z = 521288629; | |
unsigned int t = (x ^ (x << 11) ); | |
x=y; y=z; z=w; | |
return ( w=(w^(w>>19)) ^ ( t ^ ( t >>8)) ); | |
} | |
NN txy[NUM_SAMPLE] = { 0 }; | |
double x[NUM_INPUT+1],h[NUM_HIDDEN+1],y[NUM_OUTPUT]; | |
double w1[NUM_INPUT+1][NUM_HIDDEN],w2[NUM_HIDDEN+1][NUM_OUTPUT]; | |
double h_back[NUM_HIDDEN+1],y_back[NUM_OUTPUT]; | |
int main(int argc, char *argv[]) | |
{ | |
//if(argc < 2) return -1; | |
FILE *fp = fopen("data.csv", "r"); | |
int learn, sample, i = 0, j, k = 0; | |
double net_input, starttm, endtm; | |
double emax; | |
double EPSILON = 0.006; | |
cin >> emax; | |
srand((unsigned)time(NULL)); | |
// goto Break; | |
fscanf(fp, "%lf %lf %lf %lf", | |
&txy[0].x[0], &txy[0].x[1], &txy[0].x[2], | |
&txy[0].y[0] ); | |
txy[0].x[0] = (fabs(txy[0].x[0]) ) / RTMAX; | |
txy[0].x[1] = (fabs(txy[0].x[1]) ) / RTMAX; | |
txy[0].x[2] = (fabs(txy[0].x[2]) ) / RTMAX; | |
txy[0].y[0] = (fabs(txy[0].y[0]) ) / RTMAX; | |
for(i = 1; i < NUM_SAMPLE-1; i++ ) | |
{ | |
txy[i].x[0] = txy[i-1].x[1]; | |
txy[i].x[1] = txy[i-1].x[2]; | |
txy[i].x[2] = txy[i-1].y[0]; | |
fscanf(fp, "%lf ", &txy[i].y[0] ); | |
txy[i].y[0] = (fabs(txy[i].y[0]) ) / RTMAX; | |
cout << txy[i].x[0] * RTMAX << endl; | |
} | |
getchar(); getchar(); | |
for(i = 0; i < NUM_INPUT+1; i++) | |
for(j = 0;j < NUM_HIDDEN; j++) | |
{ | |
w1[i][j]= (double) ( (double) xor128() / 0xFFFFFFFF - 0.5) / 10.0; | |
cout << xor128() << endl; | |
} | |
for(i = 0; i < NUM_HIDDEN+1; i++) | |
for(j = 0;j < NUM_OUTPUT; j++) | |
w2[i][j]= (double) ( (double) xor128() / 0xFFFFFFFF - 0.5) / 10.0; | |
starttm = clock(); | |
for(learn = 0; learn < NUM_LEARN; learn++) | |
{ | |
double e = 0.0; | |
for(sample = 0; sample < NUM_SAMPLE; sample++) | |
{ | |
for(i = 0;i < NUM_INPUT; i++) | |
x[i] = txy[sample].x[i]; | |
x[NUM_INPUT] = (double)1.0; | |
for( j = 0; j < NUM_HIDDEN;j++) | |
{ | |
net_input = 0; | |
for( i = 0; i < NUM_INPUT+1; i++ ) | |
net_input = net_input + w1[i][j] * x[i]; | |
h[j] = tanh(net_input); | |
} | |
h[NUM_HIDDEN] = (double)1.0; | |
for (j = 0; j < NUM_OUTPUT;j++) | |
{ | |
net_input = 0; | |
for(i = 0; i < NUM_HIDDEN + 1; i++) | |
net_input = net_input + w2[i][j] * h[i]; | |
y[j] = sigmoid(net_input); | |
e += 0.5 * (txy[sample].y[j] - y[j]) * (txy[sample].y[j] - y[j]); | |
if(learn % 10000 == 0) | |
{ | |
printf("> %lf %lf error=%lf Epsilon=%lf\n", y[j] * RTMAX, fabs(txy[sample].y[j] - y[j]), | |
e, EPSILON); | |
} | |
} | |
for(i = 0; i < NUM_HIDDEN;i++) | |
{ | |
net_input = 0; | |
for(j = 0; j < NUM_OUTPUT; j++) | |
{ | |
y_back[j] = ( y[j] - txy[sample].y[j] ) * ( 1.0 - y[j] ) * y[j]; | |
net_input = net_input + w2[i][j] * y_back[j]; | |
} | |
h_back[i] = net_input * ((double)1.0 - h[i]) * h[i]; | |
} | |
for(i = 0; i < NUM_INPUT+1; i++) | |
for(j = 0; j < NUM_HIDDEN; j++) | |
w1[i][j] = w1[i][j] - EPSILON * x[i] * h_back[j]; | |
for(i = 0; i < NUM_HIDDEN+1; i++) | |
for(j = 0; j < NUM_OUTPUT; j++) | |
w2[i][j] = w2[i][j] - EPSILON * h[i] * y_back[j]; | |
} | |
// if(learn % 10000 == 0) | |
/*if(e - 0.01 < emax) | |
EPSILON = e - emax + 0.001;*/ | |
if(e <= emax) | |
{ | |
cout << "error = " << e << endl; | |
break; | |
} | |
} | |
Break: | |
fclose(fp); | |
fseek(fp, 0L, SEEK_SET); | |
fp = fopen("w_io.wd", "wb"); | |
if(!fp) cout << "err" << endl; | |
fwrite(w1, sizeof(double) , (NUM_INPUT+1) * NUM_HIDDEN, fp); | |
fwrite(w2, sizeof(double) , (NUM_HIDDEN+1) * NUM_OUTPUT, fp); | |
fclose(fp); | |
endtm = clock(); | |
cout << "End of learning : " << (endtm-starttm) / CLOCKS_PER_SEC / 60 << "minutes" << endl; | |
for(int w = 0; w < 10; w++) | |
{ | |
for(i = 0;i < NUM_INPUT; i++) | |
{ | |
cin >> x[i]; | |
x[i] /= RTMAX; | |
} | |
for(k=0; k<30; k++) | |
{ | |
h[NUM_HIDDEN] = x[NUM_INPUT] = (double)1.0; | |
for( j = 0; j < NUM_HIDDEN;j++) | |
{ | |
net_input = 0; | |
for(i = 0; i < NUM_INPUT+1; i++) | |
{ | |
net_input = net_input + w1[i][j] * x[i]; | |
} | |
h[j] = tanh(net_input); | |
} | |
for (j = 0; j < NUM_OUTPUT;j++) | |
{ | |
net_input = 0; | |
for(i = 0;i < NUM_HIDDEN+1; i++) | |
net_input += w2[i][j] * h[i]; | |
y[j] = sigmoid(net_input); | |
printf("> %lf\n", y[j] * RTMAX); | |
} | |
for (j = 0; j < NUM_INPUT-1; j++) | |
{ | |
x[j] = x[j+1]; | |
} | |
x[NUM_INPUT-1] = y[0]; | |
} | |
} | |
return 0; | |
} | |
/* | |
0.3.2 | |
0.3.4 | |
0.3.7 | |
0.3.8 | |
0.3.5 | |
0.3.8 | |
0.3.6 | |
0.3.9 | |
0.3.8 | |
0.3.4 | |
0.3.7 | |
0.3.3 | |
0.3.2 | |
0.2.9 | |
0.2.8 | |
0.2.9 | |
0.2.6 | |
0.2.8 | |
0.2.5 | |
0.2.7 | |
0.1.9 | |
0.2.4 | |
0.2.4 | |
0.2.2 | |
0.2.3 | |
0.2.1 | |
0.2.1 | |
0.2.1 | |
0.2.3 | |
0.2.3 | |
0.2.3 | |
0.2.4 | |
0.2.1 | |
0.2.5 | |
0.2.7 | |
0.2.9 | |
0.3.2 | |
0.3.0 | |
0.3.0 | |
0.3.0 | |
0.3.1 | |
0.3.0 | |
0.3.1 | |
0.3.1 | |
0.3.0 | |
0.3.1 | |
0.3.2 | |
0.3.3 | |
0.3.2 | |
0.3.4 | |
0.3.5 | |
0.3.6 | |
0.4.0 | |
0.4.4 | |
0.5.1 | |
0.5.4 | |
0.6.0 | |
0.5.8 | |
0.6.6 | |
0.7.6 | |
0.8.0 | |
0.8.0 | |
0.7.7 | |
0.7.9 | |
0.19.2 | |
0.9.5 | |
0.18.1 | |
0.18.7 | |
0.8.7 | |
0.9.7 | |
0.19.7 | |
0.110. | |
0.110. | |
0.10. | |
0.11. | |
0.10. | |
0.10. | |
0.110. | |
0.11. | |
0.12. | |
0.11. | |
0.11. | |
0.11. | |
0.11. | |
0.10. | |
0.10. | |
0.97 | |
0.96 | |
0.95 | |
0.95 | |
0.96 | |
0.91 | |
0.87 | |
0.85 | |
0.84 | |
0.83 | |
0.82 | |
0.81 | |
0.78 | |
0.77 | |
0.75 | |
0.72 | |
0.70 | |
0.73 | |
0.77 | |
0.79 | |
0.77 | |
0.76 | |
0.76 | |
0.77 | |
0.73 | |
0.70 | |
0.68 | |
0.70 | |
0.73 | |
0.70 | |
0.68 | |
0.72 | |
0.69 | |
0.70 | |
0.72 | |
0.68 | |
0.67 | |
0.69 | |
0.72 | |
0.65 | |
0.67 | |
0.70 | |
0.68 | |
0.66 | |
0.64 | |
0.66 | |
0.65 | |
0.64 | |
0.63 | |
0.61 | |
0.65 | |
0.63 | |
0.56 | |
0.58 | |
0.55 | |
0.51 | |
0.53 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment