Created
February 27, 2016 04:37
-
-
Save e10dokup/c6772a09e35cfa4b3187 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <math.h> | |
#include <time.h> | |
#include <stdlib.h> | |
int c1_size = 0; | |
int c2_size = 0; | |
int c_test_size = 0; | |
int dim = 2; | |
int dim_2nd = 2; | |
int dim_out = 2; | |
float mu = 1.0; | |
float w[dim_out][dim_2nd][dim] = {{{0,0},{0,0}},{{0,0},{0,0}}}; | |
//重み係数の初期化を行う | |
void initweight() { | |
float mean = 0; | |
int i,j; | |
srand((unsigned)time(NULL)); | |
for(i=0; i<dim_2nd; i++) { | |
for(j=0; j<dim; j++) { | |
w[0][i][j] = 0.1f * (float)rand() / (float)RAND_MAX; | |
mean += w[0][i][j]; | |
} | |
} | |
mean = mean / (dim*dim_2nd); | |
for(i=0; i<dim_2nd; i++) { | |
for(j=0; j<dim; j++) { | |
w[0][i][j] = w[0][i][j] - mean; | |
} | |
} | |
mean = 0; | |
srand((unsigned)time(NULL)); | |
for(i=0; i<dim_out; i++) { | |
for(j=0; j<dim_2nd; j++) { | |
w[1][i][j] = 0.1f * (float)rand() / (float)RAND_MAX; | |
mean += w[1][i][j]; | |
} | |
} | |
mean = mean / (dim_2nd*dim_out); | |
for(i=0; i<dim_2nd; i++) { | |
for(j=0; j<dim; j++) { | |
w[1][i][j] = w[1][i][j] - mean; | |
} | |
} | |
} | |
int main(void) { | |
float *c1; | |
float *c2; | |
float *c_test; | |
float *c1_even; | |
float *c1_odd; | |
float *c2_even; | |
float *c2_odd; | |
float *c_test_even; | |
float *c_test_odd; | |
int i,j,k; | |
FILE *fp_c1; | |
char fn_c1[128] = "class1.dat"; | |
FILE *fp_c2; | |
char fn_c2[128] = "class2.dat"; | |
FILE *fp_c_test; | |
char fn_c_test[128] = "test.dat"; | |
if((fp_c1 = fopen(fn_c1, "rb+")) == NULL) { | |
printf("FILE OPEN ERROR\n"); | |
exit(EXIT_FAILURE); | |
} | |
fseek(fp_c1, 0, SEEK_END); | |
c1_size = ftell(fp_c1) / sizeof(float); | |
fseek(fp_c1, 0, SEEK_SET); | |
if((fp_c2 = fopen(fn_c2, "rb+")) == NULL) { | |
printf("FILE OPEN ERROR\n"); | |
exit(EXIT_FAILURE); | |
} | |
fseek(fp_c2, 0, SEEK_END); | |
c2_size = ftell(fp_c2) / sizeof(float); | |
fseek(fp_c2, 0, SEEK_SET); | |
if((fp_c_test = fopen(fn_c_test, "rb+")) == NULL) { | |
printf("FILE OPEN ERROR\n"); | |
exit(EXIT_FAILURE); | |
} | |
fseek(fp_c_test, 0, SEEK_END); | |
c_test_size = ftell(fp_c_test) / sizeof(float); | |
fseek(fp_c_test, 0, SEEK_SET); | |
c1 = (float *)malloc((size_t)(c1_size * sizeof(float))); | |
c2 = (float *)malloc((size_t)(c2_size * sizeof(float))); | |
c_test = (float *)malloc((size_t)(c_test_size * sizeof(float))); | |
//サンプルを偶数奇数のインデックスで分割 | |
c1_odd = (float *)malloc((size_t)(c1_size / 2 * sizeof(float))); | |
c1_even = (float *)malloc((size_t)(c1_size / 2 * sizeof(float))); | |
c2_odd = (float *)malloc((size_t)(c2_size / 2 * sizeof(float))); | |
c2_even = (float *)malloc((size_t)(c2_size / 2 * sizeof(float))); | |
c_test_odd = (float *)malloc((size_t)(c_test_size / 2 * sizeof(float))); | |
c_test_even = (float *)malloc((size_t)(c_test_size / 2 * sizeof(float))); | |
for(i=0; i<c1_size; i++) { | |
if(i%2 == 0) { | |
c1_even[i/2] = c1[i]; | |
} else { | |
c1_odd[i/2] = c1[i]; | |
} | |
} | |
for(i=0; i<c2_size; i++) { | |
if(i%2 == 0) { | |
c2_even[i/2] = c2[i]; | |
} else { | |
c2_odd[i/2] = c2[i]; | |
} | |
} | |
for(i=0; i<c_test_size; i++) { | |
if(i%2 == 0) { | |
c_test_even[i/2] = c_test[i]; | |
} else { | |
c_test_odd[i/2] = c_test[i]; | |
} | |
} | |
//入力と教師信号を作成する | |
float x1[c1_size/2 + c2_size/2]; | |
float x2[c1_size/2 + c2_size/2]; | |
float d[c1_size/2 + c2_size/2][dim_out]; | |
int N=0; | |
for(i=0; i<c1_size_2; i++){ | |
x1[i] = c1_even[i]; | |
x2[i] = c1_odd[i]; | |
d[i][0] = 1; | |
d[i][1] = 0; | |
N++; | |
} | |
for(i=c1_size/2; i<c1_size/2+c2_size/2; i++){ | |
x1[i] = c2_even[i-c2_size/2]; | |
x2[i] = c2_odd[i-c2_size/2]; | |
d[i][0] = 0; | |
d[i][1] = 1; | |
N++; | |
} | |
initweight(); | |
fclose(fp_c1); | |
fclose(fp_c2); | |
fclose(fp_c_test); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment