int main() { size_t i, j; size_t nencode = 3, ninput = pow(2, nencode); //Set the number of vectors size_t iter_times = 50000; // Complex network requires more iteration times double rate = 0.5, momentum = 0.5; // Learning parameters; size_t hidden[1] = { nencode }; char *ch = "sigmoid"; char *chs[1] = {ch}; double **input = alloc_2d(ninput, ninput), **output = alloc_2d(ninput, ninput); for (i = 0; i < ninput; ++i) { for (j = 0; j < ninput; ++j) { if (i == j) input[i][j] = output[i][j] = 1; else input[i][j] = output[i][j] = 0; } } /* Set the configuration of neural network */ struct nn_config config = { .dim_h = hidden, .ahfunc = chs, .n_hidden = 1, .dim_i = ninput, .dim_o = ninput, .aofunc = ch }; struct neural_network* nn = create_nn(&config); train(nn, input, output, ninput, rate, momentum, iter_times); double *res = alloc_1d(ninput); for (i = 0; i < ninput; ++i) { for (j = 0; j < ninput; ++j) printf("%d ", (int)input[i][j]); printf("=> "); predict_hidden(nn, input[i], res, 0); for (j = 0; j < nencode; ++j) { printf("%.3f ", res[j]); // Actually value // printf("%d ", (int)(res[j]+0.5)); // 0-1 value } predict(nn, input[i], res); printf("=> "); for (j = 0; j < ninput; ++j) printf("%.3f ", res[j]); printf("\n"); } free_2d(input, ninput); free_2d(output, ninput); free_1d(res); destroy_nn(nn); return 0; }
int main() { int i, j, m=4, n=5; int **p = alloc_2d(m, n); for (i=0; i<m; i++) for (j=0; j<n; j++) p[i][j] = i+j; for (i=0; i<m; i++) { for (j=0; j<n; j++) printf("(%d,%d)=%d ", i, j, p[i][j]); printf("\n"); } free(p); return 0; }
/*Builds the CSF filter banks for each channel and contrast level.*/ static void build_csf_tables(csf_filter_bank _filters[5][8]){ csf_ctx csf_ctxs[5]={ { (csf_func)csfl, &Y_CTX, FREQUENCY_MAX, 3.74695975597140362 }, { (csf_func)csfc, &CR_CTX, FREQUENCY_MAX, 0 }, { (csf_func)csfc, &CR_CTX, 0.5*FREQUENCY_MAX, 0 }, { (csf_func)csfc, &CB_CTX, FREQUENCY_MAX, 0 }, { (csf_func)csfc, &CB_CTX, 0.5*FREQUENCY_MAX, 0 } }; double **scratch; int chi; int ci; /*Allocate the scratch space needed for our least-squares problem.*/ scratch=(double **)alloc_2d(NFREQS*NPHASES*8,NTAPS+1,sizeof(double)); for(chi=0;chi<5;chi++){ for(ci=0;ci<8;ci++){ csf_ctxs[chi].contrast=pow(CONTRASTS[ci]*CONTRAST_SCALE,1.0/CONTRAST_EXP); /*Find the normalization constant for this contrast level.*/ csf_ctxs[chi].scale=csf_ctxs[chi].contrast+ 1/csf_ctxs[chi].c0csf(csf_ctxs[chi].c0csf_ctx,csf_ctxs[chi].peakf); find_csf_filters(_filters[chi][ci],(csf_func)csf,csf_ctxs+chi,scratch); } } }