void main() { int i, j, k, trialnr; /* input pars */ getpars(); /* input training set and test set */ getsets(); if (maxtrials>20) maxtrials=20; if (bias1==1) in_mod++; if (bias2==1) hid_mod++; hi_in_mod = in_mod+hid_mod; cell_mod=hi_in_mod; for (i=0;i<num_blocks;i++) cell_mod+=(2+block_size[i]); ges_mod = cell_mod+out_mod; if (ges_mod>max_units) { printf("Program terminated!\n"); printf("You have to set the constant max_units at begin\n"); printf("of the program file greater or equal %d and then\n",ges_mod); printf("compile the program again.\n"); exit(0); } srand(ran_sta); for (trialnr=0;trialnr<maxtrials;trialnr++) { outfile = outf[trialnr]; weightfile = weig[trialnr]; fp1 = fopen(outfile, "w"); fprintf(fp1,"Trial Nr.:%.1d\n",trialnr); fclose(fp1); fp2 = fopen(weightfile, "w"); fprintf(fp2,"Trial Nr.:%.1d\n",trialnr); fclose(fp2); initia(); examples=0; epoch=0; maxepoch=maxepoch_init; stop_learn=0; learn = 1; while (learn == 1) { /* executing the environment and setting the input */ execute_act(); /* forward pass */ forward_pass(); if (targ==1) /* only if target for this input */ { /* compute error */ for (k=cell_mod,j=0;k<ges_mod;k++,j++) { error[j]= target_a[j] - Yk_mod_new[k]; }; /* Training error */ comp_err(); } /* backward pass */ if (targ==1) /* only if target for this input */ { backward_pass(); } else { derivatives(); } /* set old activations */ for (i=0;i<ges_mod;i++) { Yk_mod_old[i] = Yk_mod_new[i]; } /* update weights */ if (weight_up==1) { weight_up=0; weight_update(); } /* stop if maxepoch reached */ if (epoch>maxepoch) learn=0; } weight_out(); test(); } exit(0); }
int main(){ int a,b; char c; int i; int j; double dif; int col = 0; double sum_sq_error = 0 ; int epoch=0; double pre_avg_sq_error = 0; double accuracy = 0; double max = 0; int num_correct= 0, tmp, k; //�ʱ�ȭ for(i = 0; i<NLayer; i++){ for(j = 0; j<MLayerSize; j++){ s[i][j] = 0; f[i][j] = 0; delta[i][j] = 0; } } initialization(); // ����ġ ������ �ʱ�ȭ FILE *fp, *fp1; fp = fopen("traindata.txt", "r");fp1 = fopen("testdata.txt", "r");//�Է½�ȣ �������� while(!feof(fp)){ fscanf(fp, "%d", &a); for(i = 0; i<m2; i++){ if(a == i) d[col][i] = 1; else d[col][i] = 0; } fscanf(fp, " %c ", &c); c = '0'; // $������ for(i = 0; i<N; i++){ fscanf(fp, "%d", &b); TrainData[col][i] = b; } col ++; } col = 0;a = 0;b = 0; while(!feof(fp1)) { fscanf(fp1, "%d", &a); for(i=0; i<m2; i++) { if(a==i) Td[col][i]=1; else Td[col][i]=0; } fscanf(fp1, " %c ", &c); c = '0'; // $������ for(i=0; i<N; i++) { fscanf(fp1, " %d", &b); TestData[col][i]=b; } col++; } //file input do{ pre_avg_sq_error = avg_sq_error; // printf("pre = %lf\n", pre_avg_sq_error); avg_sq_error=0, sum_sq_error=0; for(int t=0; t<=N_tr_examples-1; t++){ // �� ������ �Ʒð��� forward_compute(t); backward_compute(t); weight_update(t); } epoch++; for(int t = 0; t<=N_tr_examples-1; t++){ // �� ���� �� ��� ���� ��� // t���� �Ʒÿ����� �̿��Ͽ� �ý����� ����� ���� error�� ���� forward_compute(t); // t ��° �Ʒÿ��� �����Ͽ� ����� ���� for(i = 0; i<=M[NLayer-1]-1 ; i++){ // �������� ����� �̿��Ͽ� ������ ����. sum_sq_error+=pow((d[t][i]-f[NLayer-1][i]),2); } } avg_sq_error = sum_sq_error/(N_tr_examples*M[NLayer-1]); //��� �Ʒ� ���� ���Ͽ� ��� square ������ ����� printf("%d epoch error rate = %lf\n",epoch,avg_sq_error); /* Fail �κ�, �Ӱ�ġ ���Ϸ� �������� �ʹ� ���� ����� ������ ������ �Ͼ���ϴ�. dif = pre_avg_sq_error - avg_sq_error; if(dif < 0) dif = -dif; printf("dif = %lf\n", dif); */ }while(threshold < avg_sq_error) ; //&& (threshold < dif)); //avg_sq_error �� �Ӱ�ġ �����̸� ����, //�Ǵ� ���� epoch ������ avg_sq_error �� �̹� epoch �� avg_sq_error �� ���̰� �Ӱ�ġ �����̸� ���� for(int t = 0; t<=N_te_examples-1; t++){ // TestData[t], d_te[t] �� input,d�� �ִ´�. t_forward_compute(t); max=0.; for(j=0; j<NLayer; j++) { if(max < f[NLayer-1][j]) { max = f[NLayer-1][j]; tmp = j; } } for(k=0; k<NLayer; k++) { if(Td[t][k] == 1 && tmp == k) num_correct++; } } accuracy = (double)num_correct/(double)N_te_examples; printf("Accuracy = %lf", accuracy); fclose(fp); fclose(fp1); }