/* lab070420a.c Simple Neuron 2007.4.19 A.Date % gcc lab070420a.c -lm */ #include #include /* drand48 */ #include /* log */ #define MAX_SAMPLES 1000 #define N_DIM 3 /* 以下のパラメータ値を変えて学習の様子がどう変化するか観測してみる */ #define RAND_SEED 1234567 /* 乱数の種 */ #define SLEEP 50000 /* 表示スピード */ int n_trials = 100; /* 学習回数 */ double eta = 0.1; /* 学習係数 */ double lambda=1.0; /* シグモイド関数のパラメータ */ char data_file1[] = "apples.dat"; /* 入力データ.課題C,Dではファイル名を変える */ char data_file2[] = "oranges.dat"; int n_data, n_data1, n_data2; double sigmoid(double x){ return 1.0/(1.0 + exp(-lambda*x)); } int main () { FILE *gp, *fp; int a,i,j,l,t, n; int seed = RAND_SEED; int n_data; int teacher; double y1, y2; double dw[N_DIM]; double w[N_DIM]; double data[MAX_SAMPLES][N_DIM]; double u,z,e,e2; double x_start = -1.0, x_end=5.0; double y_start = -1.0, y_end=5.0; srand48(seed); /* load data */ fp = fopen(data_file1, "r"); if (fp == NULL){ fprintf(stderr, "Can't open data file: %s\n",data_file1); exit(-1); } i=0; while( (n=fscanf(fp, "%lf %lf", &data[i][1], &data[i][2])) != EOF ){ if( n == 2 ){ data[i][0] = 1.0; i++; } else { fprintf(stderr, "Something wrong in data1.\n"); exit(-1); } } n_data1 = i; fclose(fp); fp = fopen(data_file2, "r"); if (fp == NULL){ fprintf(stderr, "Can't open data file: %s\n",data_file2); exit(-1); } while( (n=fscanf(fp, "%lf %lf", &data[i][1], &data[i][2])) != EOF ){ if( n == 2 ){ data[i][0] = 1.0; i++; } else { fprintf(stderr, "Something wrong in data2.\n"); exit(-1); } } n_data = i; n_data2 = n_data - n_data1; fclose(fp); /* gnuplot へのコマンド. あまり気にしなくてよい.*/ gp = popen("gnuplot -geometry 640x480","w"); fprintf(gp, "set xlabel \"x1\"\n"); fprintf(gp, "set ylabel \"x2\"\n"); fprintf(gp, "set xrange[%.2lf:%.2lf]\n",x_start,x_end); fprintf(gp, "set yrange[%.2lf:%.2lf]\n",y_start,y_end); fprintf(gp, "set size square\n"); // fprintf(gp, "set terminal postscript\n"); /* 結合係数の初期値として乱数を代入 */ for (i=0; i