/** Applies the currently learned function to the filtered and preprocessed input vector. \param S State of the liquid (= filtered and preprocessed response of the neural microcircuit). \param X Target pointer where to save the result. \return -1 if an error occured, 1 for success. */ int linear_classification::apply(const double* S, double* X) { if (S == 0) { // Error TheCsimError.add("linear_classification::apply: Input is a NULL pointer!\n"); return -1; } if (X == 0) { // Error TheCsimError.add("linear_classification::apply: Target is a NULL pointer!\n"); return -1; } if (nClasses == 2) { // binary classification double res = weighted_sum(S, regression_coefficients[0]); *X = (double) (res >= 0); return 1; } else { // multi-class classification double max_v = 0; int max_ind = -1; double res; // Calculate class with highest weighted sum for (int i=0; i<nClasses; i++) { csimPrintf("%d: ", i); res = weighted_sum(S, regression_coefficients[i]); if ((res > max_v) || (max_ind < 0)) { max_v = res; max_ind = i; } } *X = (double) max_ind; return 1; } }
void test_neuron(std::map<std::string,std::string> & args){ std::ofstream ofile; std::ifstream ifile; std::string filename; neuron * p = new heavy_neuron(3); neuron & cell = *p; std::cout<<std::endl<<"Creating a neuron with 3 inputs..."<<std::endl; cell.print(); std::cout<<std::endl<<"receiving input 1.2 ..."<<std::endl; cell.receive(1.2); std::cout << cell << std::endl; std::cout<<std::endl<<"receiving inputs 2.3 and 3.4 ..."<<std::endl; cell << 2.3 << 3.4; std::cout << cell << std::endl; std::cout<<std::endl<<"running neural evaluation ..."<<std::endl; cell.evaluate(); std::cout << cell << std::endl; std::cout<<std::endl<<"receiving multiples inputs from std::vector (0.1, 0.2, 0.3) ..."<<std::endl; std::vector<double> data; data.push_back(0.1); data.push_back(0.2); data.push_back(0.3); cell << data; std::cout << cell << std::endl; std::cout<<std::endl<<"Running neural evaluation ..."<<std::endl; cell.evaluate(); std::cout << cell << std::endl; std::cout<<std::endl<<"changing weights ..."<<std::endl; cell.setWeight(0, 0.4); cell.setWeight(1, 0.1); cell.setWeight(2, 0.2); cell.setWeight(3, 0.3); std::cout << cell << std::endl; std::cout<<std::endl<<"Comparison of neuron's outputs to expected output ..."<<std::endl; double sum; cell << 1.1 << 1.2 << 1.3; std::cout << cell << std::endl; cell.evaluate(); sum = weighted_sum(1.1, 0.1, 1.2,0.2, 1.3,0.3, 0); std::cout<<"inputs 1.1, 1.2, 1.3 --> "<<cell.output()<<" expected weighted sum: "<<sum<<" expected output: "<<activation::sigmoid(sum-0.4)<<std::endl; std::cout<<std::endl<<"testing observers methods ..."<<std::endl; std::cout<<"size(): "<<cell.size()<<std::endl; std::cout<<"input_index(): "<<cell.input_index()<<std::endl; std::cout<<"output(): "<<cell.output()<<std::endl; std::cout<<"binary_output(): "<<cell.binary_output()<<std::endl; std::cout<<"is_active(): "<<cell.is_active()<<std::endl; std::cout<<"getWeight() 1: "<<cell.getWeight(1)<<" | 2: "<<cell.getWeight(2)<<" | 3: "<<cell.getWeight(3)<<std::endl; std::cout<<std::endl<<"Saving to file save_neuron.neu..."<<std::endl; filename = "res/save_neuron.neu"; ofile.open( filename.c_str() ); if( ofile.is_open() ){ cell.save(ofile); ofile.close(); } std::cout<<std::endl<<"Loading from file neuron.neu..."<<std::endl; filename = "res/neuron.neu"; ifile.open( filename.c_str() ); if( ifile.is_open() ){ cell.load(ifile); ifile.close(); } std::cout << cell << std::endl; delete p; }
void schro_encoder_calculate_subband_weights (SchroEncoder *encoder, double (*perceptual_weight)(double)) { int wavelet; int n_levels; double *matrix; int n; int i,j; double column[SCHRO_LIMIT_SUBBANDS]; double *weight; matrix = schro_malloc (sizeof(double)*SCHRO_LIMIT_SUBBANDS*SCHRO_LIMIT_SUBBANDS); weight = schro_malloc (sizeof(double)*CURVE_SIZE*CURVE_SIZE); for(j=0;j<CURVE_SIZE;j++){ for(i=0;i<CURVE_SIZE;i++){ double fv = j*encoder->cycles_per_degree_vert*(1.0/CURVE_SIZE); double fh = i*encoder->cycles_per_degree_horiz*(1.0/CURVE_SIZE); weight[j*CURVE_SIZE+i] = perceptual_weight (sqrt(fv*fv+fh*fh)); } } for(wavelet=0;wavelet<SCHRO_N_WAVELETS;wavelet++) { for(n_levels=1;n_levels<=4;n_levels++){ const float *h_curve[SCHRO_LIMIT_SUBBANDS]; const float *v_curve[SCHRO_LIMIT_SUBBANDS]; int hi[SCHRO_LIMIT_SUBBANDS]; int vi[SCHRO_LIMIT_SUBBANDS]; n = 3*n_levels+1; for(i=0;i<n;i++){ int position = schro_subband_get_position(i); int n_transforms; n_transforms = n_levels - SCHRO_SUBBAND_SHIFT(position); if (position&1) { hi[i] = (n_transforms-1)*2; } else { hi[i] = (n_transforms-1)*2+1; } if (position&2) { vi[i] = (n_transforms-1)*2; } else { vi[i] = (n_transforms-1)*2+1; } h_curve[i] = schro_tables_wavelet_noise_curve[wavelet][hi[i]]; v_curve[i] = schro_tables_wavelet_noise_curve[wavelet][vi[i]]; } if (0) { for(i=0;i<n;i++){ column[i] = weighted_sum(h_curve[i], v_curve[i], weight); matrix[i*n+i] = dot_product (h_curve[i], v_curve[i], h_curve[i], v_curve[i], weight); for(j=i+1;j<n;j++) { matrix[i*n+j] = dot_product (h_curve[i], v_curve[i], h_curve[j], v_curve[j], weight); matrix[j*n+i] = matrix[i*n+j]; } } solve (matrix, column, n); for(i=0;i<n;i++){ if (column[i] < 0) { SCHRO_ERROR("BROKEN wavelet %d n_levels %d", wavelet, n_levels); break; } } SCHRO_DEBUG("wavelet %d n_levels %d", wavelet, n_levels); for(i=0;i<n;i++){ SCHRO_DEBUG("%g", 1.0/sqrt(column[i])); encoder->subband_weights[wavelet][n_levels-1][i] = sqrt(column[i]); } } else { for(i=0;i<n;i++){ int position = schro_subband_get_position(i); int n_transforms; double size; n_transforms = n_levels - SCHRO_SUBBAND_SHIFT(position); size = (1.0/CURVE_SIZE)*(1<<n_transforms); encoder->subband_weights[wavelet][n_levels-1][i] = 1.0/(size * sqrt(weighted_sum(h_curve[i], v_curve[i], weight))); } } } } #if 0 for(wavelet=0;wavelet<8;wavelet++) { for(n_levels=1;n_levels<=4;n_levels++){ double alpha, beta, shift; double gain; alpha = schro_tables_wavelet_gain[wavelet][0]; beta = schro_tables_wavelet_gain[wavelet][1]; shift = (1<<filtershift[wavelet]); n = 3*n_levels+1; gain = shift; for(i=n_levels-1;i>=0;i--){ encoder->subband_weights[wavelet][n_levels-1][1+3*i+0] = sqrt(alpha*beta)*gain; encoder->subband_weights[wavelet][n_levels-1][1+3*i+1] = sqrt(alpha*beta)*gain; encoder->subband_weights[wavelet][n_levels-1][1+3*i+2] = sqrt(beta*beta)*gain; gain *= alpha; gain *= shift; } encoder->subband_weights[wavelet][n_levels-1][0] = gain / shift; if (wavelet == 3 && n_levels == 3) { for(i=0;i<10;i++){ SCHRO_ERROR("%g", encoder->subband_weights[wavelet][n_levels-1][i]); } } } } #endif schro_free(weight); schro_free(matrix); }