double evaluate_results(gsl_vector* test_data[], gsl_vector* ys, gsl_vector* biases[], gsl_matrix* weights[], int nrow, int ncat, int num_layers, int * layer_sizes, int transformation_type, double probs[], int predicted[]) { par p; par_c q; p.weights = weights; p.biases = biases; p.num_layers = num_layers; p.layer_sizes = layer_sizes; p.transformation_type = transformation_type; p.trans = sigmoid; p.trans_prime = sigmoid_prime; p.trans_final_prime = sigmoid_prime; if (transformation_type == 1){ p.trans_final = softmax; p.cost = softmax_cost; p.cost_prime = softmax_prime; } else{ p.trans_final = sigmoid; p.cost = squared_error_cost; p.cost_prime = softmax_prime; } p.total_cost = 0; q.total_cost = 0; gsl_vector* z[num_layers]; gsl_vector* transf_x[num_layers]; init_bias_object(z, layer_sizes, num_layers); init_bias_object(transf_x, layer_sizes, num_layers); q.z = z; q.transf_x = transf_x; double total = 0.0; for (int i = 0; i < nrow; i++){ q.x = test_data[i]; q.y = (int) gsl_vector_get(ys,i); forward(&q, &p); predicted[i] = gsl_vector_max_index(q.transf_x[num_layers-1]); for (int j = 0; j < ncat; j++){ probs[i * ncat + j] = gsl_vector_get(q.transf_x[num_layers-1],j); } int y_fitted = gsl_vector_max_index(q.transf_x[num_layers-1]); total += (q.y==y_fitted); } for (int i = 0; i < num_layers - 1; i++){ gsl_vector_free(z[i]); gsl_vector_free(transf_x[i]); } return total / nrow; }
static int pcholesky_decomp (const int copy_uplo, gsl_matrix * A, gsl_permutation * p) { const size_t N = A->size1; if (N != A->size2) { GSL_ERROR("LDLT decomposition requires square matrix", GSL_ENOTSQR); } else if (p->size != N) { GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN); } else { gsl_vector_view diag = gsl_matrix_diagonal(A); size_t k; if (copy_uplo) { /* save a copy of A in upper triangle (for later rcond calculation) */ gsl_matrix_transpose_tricpy('L', 0, A, A); } gsl_permutation_init(p); for (k = 0; k < N; ++k) { gsl_vector_view w; size_t j; /* compute j = max_idx { A_kk, ..., A_nn } */ w = gsl_vector_subvector(&diag.vector, k, N - k); j = gsl_vector_max_index(&w.vector) + k; gsl_permutation_swap(p, k, j); cholesky_swap_rowcol(A, k, j); if (k < N - 1) { double alpha = gsl_matrix_get(A, k, k); double alphainv = 1.0 / alpha; /* v = A(k+1:n, k) */ gsl_vector_view v = gsl_matrix_subcolumn(A, k, k + 1, N - k - 1); /* m = A(k+1:n, k+1:n) */ gsl_matrix_view m = gsl_matrix_submatrix(A, k + 1, k + 1, N - k - 1, N - k - 1); /* m = m - v v^T / alpha */ gsl_blas_dsyr(CblasLower, -alphainv, &v.vector, &m.matrix); /* v = v / alpha */ gsl_vector_scale(&v.vector, alphainv); } } return GSL_SUCCESS; } }
QString Integration::logInfo() { ApplicationWindow *app = (ApplicationWindow *)parent(); QLocale locale = app->locale(); int prec = app->d_decimal_digits; QString logInfo = "[" + QDateTime::currentDateTime().toString(Qt::LocalDate); if (d_integrand == AnalyticalFunction){ logInfo += "\n" + tr("Numerical integration of") + " f(" + d_variable + ") = " + d_formula + " "; logInfo += tr("using a %1 order method").arg(d_method) + "\n"; logInfo += tr("From") + " x = " + locale.toString(d_from, 'g', prec) + " "; logInfo += tr("to") + " x = " + locale.toString(d_to, 'g', prec) + "\n"; logInfo += tr("Tolerance") + " = " + locale.toString(d_tolerance, 'g', prec) + "\n"; logInfo += tr("Iterations") + ": " + QString::number(romberg()) + "\n"; } else if (d_integrand == DataSet){ if (d_graph) logInfo += tr("\tPlot")+ ": ''" + d_graph->multiLayer()->objectName() + "'']\n"; else logInfo += "\n"; QString dataSet; if (d_curve) dataSet = d_curve->title().text(); else dataSet = d_y_col_name; logInfo += "\n" + tr("Numerical integration of") + ": " + dataSet + " "; logInfo += tr("using the Trapezoidal Rule") + "\n"; logInfo += tr("Points") + ": " + QString::number(d_n) + " " + tr("from") + " x = " + locale.toString(d_from, 'g', prec) + " "; logInfo += tr("to") + " x = " + locale.toString(d_to, 'g', prec) + "\n"; // use GSL to find maximum value of data set gsl_vector *aux = gsl_vector_alloc(d_n); for(int i=0; i < d_n; i++) gsl_vector_set (aux, i, fabs(d_y[i])); int maxID = gsl_vector_max_index (aux); gsl_vector_free(aux); logInfo += tr("Peak at") + " x = " + locale.toString(d_x[maxID], 'g', prec)+"\t"; logInfo += "y = " + locale.toString(d_y[maxID], 'g', prec)+"\n"; d_area = trapez(); } logInfo += tr("Area") + "=" + locale.toString(d_area, 'g', prec); logInfo += "\n-------------------------------------------------------------\n"; return logInfo; }
//MCMCMC algorithm double MC3 (int N, gsl_matrix_short * Adj, int Steps, int nChains, gsl_vector_short * BestSolution, gsl_rng * r, gsl_vector * lgammaLookup, gsl_vector * logLookup){ // create the chains gsl_vector_short * Chains[nChains]; //create copies for use by Gibbs and marginal functions gsl_vector_short * ChainCopy = gsl_vector_short_calloc(N); gsl_vector_short * ChainCopy2 = gsl_vector_short_calloc(N); // create the fitness vector gsl_vector * Marginals = gsl_vector_calloc(nChains); //initialize swapping vector for RGF gsl_vector_short * RGFswap = gsl_vector_short_calloc(N+1); int i,j,k; double BestMarginal; BestMarginal = -1000000000.0; //initialize chains for(i=0; i<nChains; i++){ // allocate memory Chains[i] = gsl_vector_short_calloc(N); // initialize the population Partition_Initialize(Chains[i], N, r); } //generate temperatures assuming uniform spacing gsl_vector * Temps = gsl_vector_calloc(nChains); //step size for incrementing temperatures double StepSize; StepSize = (COLDTEMP - HOTTEMP)/((double)nChains - 1); gsl_vector_set(Temps, 0, HOTTEMP); for(i=1; i<(nChains-1); i++){ gsl_vector_set(Temps, i, gsl_vector_get(Temps, i-1)+StepSize); } gsl_vector_set(Temps, nChains-1, COLDTEMP); //for convenience, we want a copy of the Temps vector that doesn't //get swapped around gsl_vector * TempsCopy = gsl_vector_calloc(nChains); gsl_vector_memcpy(TempsCopy, Temps); //RGF all of the chains to start with for(i=0; i<nChains; i++){ RGF(N, Chains[i], RGFswap); } int chInd1, chInd2; double dtmp; int itmp; int swapFlag; for(i=0; i<Steps; i++){ //print the best likelihood we've found so far every so often if(i % 1000 == 0){ fprintf(stderr, "Step %d Best solution %1.4f\n", i, BestMarginal); } //if enough steps have passed, swap temperatures if(i % SWAPSTEPS == 0){ //try to swap using "bucket brigade" for(j=0; j<(nChains-1); j++){ //find which chains have adjacent temperatures chInd1 = -1; chInd2 = -1; for(k=0; k<nChains; k++){ if(gsl_vector_get(TempsCopy, j) == gsl_vector_get(Temps, k)){ chInd1 = k; } if(gsl_vector_get(TempsCopy, j+1) == gsl_vector_get(Temps, k)){ chInd2 = k; } if(chInd1 >= 0 && chInd2 >=0){ break; } } //try to swap them swapFlag = TrySwap(N, Adj, Chains[chInd1], Chains[chInd2], ChainCopy, RGFswap, gsl_vector_get(Temps, chInd1), gsl_vector_get(Temps, chInd2), r, lgammaLookup, logLookup); if(swapFlag == 1){ dtmp = gsl_vector_get(Temps, chInd1); gsl_vector_set(Temps, chInd1, gsl_vector_get(Temps, chInd2)); gsl_vector_set(Temps, chInd2, dtmp); } } } //take a step for(j=0; j<nChains; j++){ dtmp = Gibbs(N, Chains[j], ChainCopy, ChainCopy2, Adj, gsl_vector_get(Temps, j), RGFswap, r, lgammaLookup, logLookup); gsl_vector_set(Marginals, j, dtmp); } //update the best solution, if appropriate if(gsl_vector_max(Marginals) > BestMarginal){ itmp = gsl_vector_max_index(Marginals); BestMarginal = gsl_vector_get(Marginals, itmp); gsl_vector_short_memcpy(BestSolution, Chains[itmp]); fprintf(stderr, "Steps %d Best solution %.4f\n", i, BestMarginal); } } //free memory gsl_vector_short_free(RGFswap); gsl_vector_free(Temps); gsl_vector_free(TempsCopy); gsl_vector_free(Marginals); gsl_vector_short_free(ChainCopy); gsl_vector_short_free(ChainCopy2); for(i=0; i<nChains; i++){ gsl_vector_short_free(Chains[i]); } return BestMarginal; }
void CrossVal(const gsl_matrix* XTrainData, const gsl_matrix* YTrainData, const gsl_matrix* XTestData, const gsl_matrix* YTestData, const int FOLD, const double* Lambda, const int sizelambda, const int* layer_sizes, const int num_layers, const int num_iterations, const int core_size, const double step_size, const int trans_type, double* probs_test, int* predicted_test) { int* layer_sizes_2 = layer_sizes; int ncat = layer_sizes_2[num_layers-1]; //int layer_sizes_2[3]={784,20,10}; int N_obs = XTrainData->size1; int YFeatures = YTrainData->size2; int XFeatures = XTrainData->size2; int GroupSize = N_obs/FOLD; int Nlambda = sizelambda; int N_obs_test = XTestData->size1; int* seq_fold; seq_fold = rand_fold(N_obs,FOLD); /*for (int i = 0; i < N_obs; i++){ printf("%d\n",seq_fold[i]); }*/ gsl_matrix* Xfolds[FOLD]; for (int d = 0; d < FOLD; d++) Xfolds[d] = gsl_matrix_alloc(GroupSize,XFeatures); gsl_matrix* Yfolds[FOLD]; for (int d = 0; d < FOLD; d++) Yfolds[d] = gsl_matrix_alloc(GroupSize,YFeatures); SplitFoldfunc(XTrainData, FOLD, seq_fold, Xfolds); SplitFoldfunc(YTrainData, FOLD, seq_fold, Yfolds); /* for (int ss = 0; ss < GroupSize; ss++) printf( "Fold1 %G, %G\n",gsl_matrix_get(Xfolds[0],ss,0),gsl_matrix_get(Xfolds[0],ss,1)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold2 %G, %G\n",gsl_matrix_get(Xfolds[1],ss,0),gsl_matrix_get(Xfolds[1],ss,1)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold3 %G, %G\n",gsl_matrix_get(Xfolds[2],ss,0),gsl_matrix_get(Xfolds[2],ss,1)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold4 %G, %G\n",gsl_matrix_get(Xfolds[3],ss,0),gsl_matrix_get(Xfolds[3],ss,1)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold5 %G, %G\n",gsl_matrix_get(Xfolds[4],ss,0),gsl_matrix_get(Xfolds[4],ss,1)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold1 %G \n",gsl_matrix_get(Yfolds[0],ss,0)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold2 %G \n",gsl_matrix_get(Yfolds[1],ss,0)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold3 %G, \n",gsl_matrix_get(Yfolds[2],ss,0)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold4 %G, \n",gsl_matrix_get(Yfolds[3],ss,0)); for (int ss = 0; ss < GroupSize; ss++) printf( "Fold5 %G, \n",gsl_matrix_get(Yfolds[4],ss,0)); */ gsl_matrix* CvTrainX[FOLD]; for (int d = 0; d < FOLD; d++) CvTrainX[d] = gsl_matrix_calloc(GroupSize*(FOLD-1), XFeatures); gsl_matrix* CvTrainY[FOLD]; for (int d = 0; d < FOLD; d++) CvTrainY[d] = gsl_matrix_calloc(GroupSize*(FOLD-1), YFeatures); combinefold(Xfolds, Yfolds, N_obs, FOLD, GroupSize, XFeatures, YFeatures, CvTrainX, CvTrainY); /* for (int ss = 0; ss < N_obs-GroupSize; ss++) printf( "Group1 %G, %G\n",gsl_matrix_get(CvTrainX[0],ss,0),gsl_matrix_get(CvTrainX[0],ss,1)); for (int ss = 0; ss < N_obs-GroupSize; ss++) printf( "GG2 %G, %G\n",gsl_matrix_get(CvTrainX[1],ss,0),gsl_matrix_get(CvTrainX[1],ss,1)); for (int ss = 0; ss < N_obs-GroupSize; ss++) printf( "GG3 %G, %G\n",gsl_matrix_get(CvTrainX[2],ss,0),gsl_matrix_get(CvTrainX[2],ss,1)); for (int ss = 0; ss < N_obs-GroupSize; ss++) printf( "GG4 %G, %G\n",gsl_matrix_get(CvTrainX[3],ss,0),gsl_matrix_get(CvTrainX[3],ss,1)); for (int ss = 0; ss < N_obs-GroupSize; ss++) printf( "G5 %G, %G\n",gsl_matrix_get(CvTrainX[4],ss,0),gsl_matrix_get(CvTrainX[4],ss,1)); */ /* for (int ss = 0; ss < N_obs-GroupSize; ss++) Rprintf( "Group1 %G, \n",gsl_matrix_get(CvTrainY[0],ss,0)); for (int ss = 0; ss < N_obs-GroupSize; ss++) Rprintf( "GG2 %G, \n",gsl_matrix_get(CvTrainY[1],ss,0)); for (int ss = 0; ss < N_obs-GroupSize; ss++) Rprintf( "GG3 %G \n",gsl_matrix_get(CvTrainY[2],ss,0)); for (int ss = 0; ss < N_obs-GroupSize; ss++) Rprintf( "GG4 %G, \n",gsl_matrix_get(CvTrainY[3],ss,0)); for (int ss = 0; ss < N_obs-GroupSize; ss++) Rprintf( "G5 %G, \n",gsl_matrix_get(CvTrainY[4],ss,0)); */ gsl_vector* results_lambda; results_lambda = gsl_vector_alloc((size_t) Nlambda); double results[Nlambda][FOLD]; //private(i, j, vec_cv_trainX, vec_cv_trainY, output_weights, output_biases, vec_cv_valX, vec_cv_valY) collapse(2) #pragma omp parallel for collapse(2) for (int i = 0; i < Nlambda; i++){ for (int j = 0; j < FOLD; j++){ gsl_vector* vec_cv_trainX[N_obs-GroupSize]; gsl_vector* vec_cv_trainY; gsl_matrix* output_weights[num_layers-1]; gsl_vector* output_biases[num_layers-1]; gsl_vector* vec_cv_valX[GroupSize]; gsl_vector* vec_cv_valY; // Rprintf("Lambda=%G\n", Lambda[i]); // Rprintf("fold not included = %d\n", j); //gsl_vector* vec_cv_trainX[N_obs-GroupSize]; for (int u = 0; u < (N_obs-GroupSize); u++ ){ vec_cv_trainX[u] = gsl_vector_alloc(XFeatures); } for (int c = 0; c < (N_obs-GroupSize); c++){ gsl_matrix_get_row(vec_cv_trainX[c], CvTrainX[j], c); } //for (int a = 0; a < (N_obs-GroupSize); a++){ //printf("%G %G\n",gsl_vector_get(vec_cv_trainX[a],0), gsl_vector_get(vec_cv_trainX[a],1)); //printf("%d\n", a); //} //gsl_vector* vec_cv_trainY; vec_cv_trainY = gsl_vector_alloc(N_obs-GroupSize); gsl_matrix_get_col(vec_cv_trainY, CvTrainY[j], 0); //for (int y = 0; y < (N_obs-GroupSize); y++){ //printf("%G\n",gsl_vector_get(vec_cv_trainY,y)); //printf("%d\n",y); //} //Note that always Y will be 1 column, so well defined. //gsl_matrix* output_weights[num_layers-1]; //gsl_vector* output_biases[num_layers-1]; init_bias_object(output_biases, (layer_sizes_2+1), num_layers-1); init_weight_object(output_weights, layer_sizes_2, num_layers); //printf(" Lambda = %G\n",Lambda[i]); double cost_hist[num_iterations]; NeuralNets(layer_sizes_2, num_layers, vec_cv_trainX, vec_cv_trainY, num_iterations, 1, step_size, output_weights, output_biases, (N_obs-GroupSize), XFeatures, Lambda[i], cost_hist, trans_type); //gsl_vector* vec_cv_valX[GroupSize]; for (int u = 0; u < (GroupSize); u++){ vec_cv_valX[u] = gsl_vector_alloc(XFeatures); } for (int c = 0; c < GroupSize; c++){ gsl_matrix_get_row(vec_cv_valX[c], Xfolds[j], c); // for (int s = 0; s < (N_obs-GroupSize); s++){ // if((gsl_vector_get(vec_cv_valX[c],1))==(gsl_vector_get(vec_cv_trainX[s],1))){ // printf("ERROR!!!!\n%G",gsl_vector_get(vec_cv_valX[c],1)); // } // } } //gsl_vector* vec_cv_valY; vec_cv_valY = gsl_vector_alloc(GroupSize); gsl_matrix_get_col(vec_cv_valY, Yfolds[j], 0); double probs[(GroupSize*ncat)]; int predicted_val[GroupSize]; for (int d = 0; d < (GroupSize*ncat); d++) probs[d] = 0; for (int s = 0; s < GroupSize; s++) predicted_val[s] = 0; results[i][j] = evaluate_results(vec_cv_valX, vec_cv_valY, output_biases, output_weights, GroupSize,ncat, num_layers, layer_sizes_2, trans_type, probs, predicted_val); /* gsl_vector* vec_cv_testX[N_obs_test]; for (int u = 0; u < (N_obs_test); u++){ vec_cv_testX[u] = gsl_vector_alloc(XFeatures); } for (int c = 0; c < N_obs_test; c++){ gsl_matrix_get_row(vec_cv_testX[c], XTestData, c); } */ //gsl_vector* vec_cv_testY; //vec_cv_testY = gsl_vector_alloc(N_obs_test); //gsl_matrix_get_col(vec_cv_testY, YTestData, 0); //for (int ss = 0; ss < N_obs_test; ss++) // Rprintf( "GG3 %G\n",gsl_vector_get(vec_cv_testY, ss)); //Note that always Y will be 1 column, so well defined. //double success_test_check = correct_guesses(vec_cv_testX, vec_cv_testY, output_biases, output_weights, N_obs_test, num_layers, layer_sizes_2); //printf("Check = %G\n", success_test_check); // gsl_vector* vec_cv_valX[GroupSize]; //printf("\n-------------------------------\n Lambda = %G \n i = %d, j = %d \n", Lambda[i] ,i , j); //printf("Result = %G \n Thread = %d\n--------------------------------\n",results[i][j],omp_get_thread_num()); gsl_vector_free(vec_cv_valY); for (int u = 0; u < (GroupSize); u++){ gsl_vector_free(vec_cv_valX[u]); } gsl_vector_free(vec_cv_trainY); for (int u = 0; u < (GroupSize); u++){ gsl_vector_free(vec_cv_trainX[u]); } } } //gsl_vector* results_lambda; //results_lambda = gsl_vector_alloc((size_t) Nlambda); double results_mean_fold[Nlambda]; for (int w = 0; w < Nlambda; w++) results_mean_fold[w] = 0; for (int s = 0; s < Nlambda ; s++){ for (int m = 0; m < FOLD ; m++){ printf("Lambda = %G, Result = %G\n",Lambda[s], results[s][m]); } } for (int s = 0; s < Nlambda ; s++){ for (int m = 0; m < FOLD ; m++){ results_mean_fold[s] = results[s][m]+ results_mean_fold[s]; } gsl_vector_set(results_lambda, s, results_mean_fold[s]/(FOLD)); } for (int s = 0; s < Nlambda ; s++){ printf("Lambda = %G, Success = %G\n", Lambda[s], gsl_vector_get(results_lambda, s)); } int OptimalLambda_index = gsl_vector_max_index(results_lambda); double Optimal_lambda = Lambda[OptimalLambda_index]; gsl_vector_free(results_lambda); gsl_matrix* output_weights_all[num_layers-1]; gsl_vector* output_biases_all[num_layers-1]; init_bias_object(output_biases_all, (layer_sizes_2+1), num_layers-1); init_weight_object(output_weights_all, layer_sizes_2, num_layers); gsl_vector* vec_cv_trainX_all[N_obs]; for (int u = 0; u < (N_obs); u++){ vec_cv_trainX_all[u] = gsl_vector_alloc(XFeatures); } for (int c = 0; c < N_obs; c++){ gsl_matrix_get_row(vec_cv_trainX_all[c], XTrainData, c); } gsl_vector* vec_cv_trainY_all; vec_cv_trainY_all = gsl_vector_alloc(N_obs); gsl_matrix_get_col(vec_cv_trainY_all, YTrainData, 0); // for (int ss = 0; ss < N_obs; ss++) // printf( "GG3 %G\n",gsl_vector_get(vec_cv_trainY_all,ss)); printf("Optimal Lambda = %G\n", Optimal_lambda); double cost_hist_test[num_iterations]; NeuralNets(layer_sizes_2, num_layers, vec_cv_trainX_all, vec_cv_trainY_all, num_iterations, core_size, step_size, output_weights_all, output_biases_all, N_obs, XFeatures, Optimal_lambda, cost_hist_test, trans_type); gsl_vector_free(vec_cv_trainY_all); for (int u = 0; u < (N_obs); u++){ gsl_vector_free(vec_cv_trainX_all[u]); } //printf("Optimal Lambda = %G\n", Optimal_lambda); gsl_vector* vec_cv_testX[N_obs_test]; for (int u = 0; u < (N_obs_test); u++){ vec_cv_testX[u] = gsl_vector_alloc(XFeatures); } for (int c = 0; c < N_obs_test; c++){ gsl_matrix_get_row(vec_cv_testX[c], XTestData, c); } gsl_vector* vec_cv_testY; vec_cv_testY = gsl_vector_alloc(N_obs_test); gsl_matrix_get_col(vec_cv_testY, YTestData, 0); // for (int ss = 0; ss < N_obs_test; ss++) // printf( "GG %G\n",gsl_vector_get(vec_cv_testY, ss)); //Note that always Y will be 1 column, so well defined. double success_test = evaluate_results(vec_cv_testX, vec_cv_testY, output_biases_all, output_weights_all, N_obs_test,ncat, num_layers, layer_sizes_2, trans_type, probs_test, predicted_test); gsl_vector_free(vec_cv_testY); for (int u = 0; u < (N_obs_test); u++){ gsl_vector_free(vec_cv_testX[u]); } /*gsl_vector* vec_cv_valX[GroupSize]; for (int u = 0; u < (GroupSize); u++){ vec_cv_valX[u] = gsl_vector_alloc(XFeatures); } for (int c = 0; c < GroupSize; c++){ gsl_matrix_get_row(vec_cv_valX[c], Xfolds[0], c); } gsl_vector* vec_cv_valY; vec_cv_valY = gsl_vector_alloc(GroupSize); gsl_matrix_get_col(vec_cv_valY, Yfolds[0], 0); double success_test = correct_guesses(vec_cv_valX, vec_cv_valY, output_biases_all, output_weights_all, GroupSize, num_layers, layer_sizes); */ printf("Test Success = %G \n",success_test); }