def get_final_results_Evo(Objects, ngammas=5): # From all the realizations for a set of paramters. # This function applies a stop condition and outputs # the ave_tr, std_tr, ave_tst, std_tst gammas = rd.get_gammas(Objects) N_realizations = len(gammas) # print N_realizations scoreTr_layers, scoreVal_layers = rd.get_scores_layers(Objects) # Obtain nLs nLs = np.ones(N_realizations) for j in range(N_realizations): # For every realization nLs[j] = check_stop(gammas[j], ngammas) # Get the NLs statistics acc_val = np.ones((N_realizations, 1)) acc_tr = np.ones((N_realizations, 1)) for j in range(N_realizations): # For every realization acc_val[j] = scoreVal_layers[j][nLs[j] - 1] acc_tr[j] = scoreTr_layers[j][nLs[j] - 1] ave_val = np.mean(acc_val) std_val = np.std(acc_val) ave_tr = np.mean(acc_tr) std_tr = np.std(acc_tr) return ave_tr, std_tr, ave_val, std_val
matrix = mu.convert_to_matrix(All_scoreTr_layers[i], nL_max) aves_rea_tr, stds_rea_tr = mu.get_ave_std_unfilled_matrix(matrix) All_aves_rea_tr.append(aves_rea_tr) All_stds_rea_tr.append(stds_rea_tr) # Get the gammas values and their average and std All_gammas = [] N_realizations = [] All_aves_gammas = [] All_stds_gam = [] for i in range(N_neurons): gammas = rd.get_gammas(All_Object_list[i]) All_gammas.append(gammas) N_realizations.append(len(gammas)) matrix = mu.convert_to_matrix(gammas) aves_gam, stds_gam = mu.get_ave_std_unfilled_matrix(matrix) All_aves_gammas.append(aves_gam) All_stds_gam.append(stds_gam) """ 1rd GRAPH """ # Plot the Average Training and Validation score !! nLs_list = range(nL_max) # Plot the average and shit mu.plot_all_tr_val_nL(nLs_list, All_aves_rea_val, All_aves_rea_tr)
def PPE_1(PPE_p, main_folder, database_number,beta_i,alpha_i,Nepoch_i,nH_i, nL_max = 200): # Maximum number of layers): PPE_p.nH_list = np.array(PPE_p.nH_list) # Esta conversion es para luego poderle pasar una lista de indices PPE_p.N_epochs_list = np.array(PPE_p.N_epochs_list) # Folder where we read the RAW files folder = "../"+main_folder +"/ResultsNeoDSN"+str(database_number) # Folder where we read and store the Preread files base_folder_in = "../"+main_folder +"/PreRead/" # Folder where we store the graph base_folder_out = "../"+main_folder +"/Gold/" + str(database_number) +"/Nl/" results = pkl.load_pickle(base_folder_in + str(database_number) + "/" + "data_" + str(beta_i) + "_"+ str(alpha_i)+"_EVO",1) # If the result file existis coz it was previously read if (results == []): print "FILE NOT PREREAD" raise ValueError exit(0) All_Object_list = results nLs_list = np.array(range(nL_max)) + 1 # print Nepoch_i, nH_i Object_list = All_Object_list[Nepoch_i][nH_i] # Obtain the score of training and validation for every layer and realization scoreTr_layers, scoreVal_layers = rd.get_scores_layers (Object_list) # Get the average value and std for all the layers of the validation score matrix = mu.convert_to_matrix(scoreVal_layers,nL_max) aves_rea_val, stds_rea_val = mu.get_ave_std_unfilled_matrix(matrix) # Get the average value and std for all the layers of the training score matrix = mu.convert_to_matrix(scoreTr_layers,nL_max) aves_rea_tr, stds_rea_tr = mu.get_ave_std_unfilled_matrix(matrix) # Get the gammas values and their average and std gammas = rd.get_gammas (Object_list) N_realizations = len(gammas) # Number of realizations matrix = mu.convert_to_matrix(gammas) aves_gam, stds_gam = mu.get_ave_std_unfilled_matrix(matrix) base_folder_out = base_folder_out + "a:"+ str(alpha_i)+"/" +"b:" +str(beta_i)+ "/" mu.create_dirs(base_folder_out) """ 1st GRAPH """ # Plot all realizations for the given number of Epoch and Neurons mg.plot_all_realizations_EVO (scoreTr_layers,scoreVal_layers ) # Save figure !! plt.savefig(base_folder_out + "All_rea_" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) +"_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) +".png") plt.close("all") """ 2nd GRAPH """ # Plot the Average Training and Validation score !! # Plot the average and shit mg.plot_tr_val_nL(nLs_list, aves_rea_val, aves_rea_tr,stds_rea_val,stds_rea_tr) plt.savefig(base_folder_out + "Ave_Acc(nL)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) +"_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) +".png")
def PostProcessEvolution_nH(All_Object_list): N_neurons = len(All_Object_list) nL_max = 100 # Maximum number of layers nLs_list = np.array(range(nL_max)) + 1 # Obtain the score of training and validation for every layer and realization All_scoreTr_layers = [] All_scoreVal_layers = [] for i in range(N_neurons): scoreTr_layers, scoreVal_layers = rd.get_scores_layers( All_Object_list[i]) All_scoreTr_layers.append(scoreTr_layers) All_scoreVal_layers.append(scoreVal_layers) # Get the average value and std for all the layers of the validation score All_aves_rea_val = [] All_stds_rea_val = [] for i in range(N_neurons): matrix = mu.convert_to_matrix(All_scoreVal_layers[i], nL_max) aves_rea_val, stds_rea_val = mu.get_ave_std_unfilled_matrix(matrix) All_aves_rea_val.append(aves_rea_val) All_stds_rea_val.append(stds_rea_val) # Get the average value and std for all the layers of the training score All_aves_rea_tr = [] All_stds_rea_tr = [] for i in range(N_neurons): matrix = mu.convert_to_matrix(All_scoreTr_layers[i], nL_max) aves_rea_tr, stds_rea_tr = mu.get_ave_std_unfilled_matrix(matrix) All_aves_rea_tr.append(aves_rea_tr) All_stds_rea_tr.append(stds_rea_tr) # Get the gammas values and their average and std All_gammas = [] N_realizations = [] All_aves_gammas = [] All_stds_gam = [] for i in range(N_neurons): gammas = rd.get_gammas(All_Object_list[i]) All_gammas.append(gammas) N_realizations.append(len(gammas)) matrix = mu.convert_to_matrix(gammas) aves_gam, stds_gam = mu.get_ave_std_unfilled_matrix(matrix) All_aves_gammas.append(aves_gam) All_stds_gam.append(stds_gam) """ 1rd GRAPH """ # Plot the Average Training and Validation score !! # Plot the average and shit mg.plot_acc_nL_nH(nH_list, All_aves_rea_val) plt.savefig(base_folder_out + str(database_number) + "/" + "Ave_Accu_val(nL,nH)_" + str(N_epochs_list[Nepoch_i]) + ".png") plt.close("all") mg.plot_acc_nL_nH(nH_list, All_aves_rea_tr) plt.savefig(base_folder_out + str(database_number) + "/" + "Ave_Accu_tr(nL,nH)_" + str(N_epochs_list[Nepoch_i]) + ".png") plt.close("all") """ 2rd GRAPH """ # Plot the gammas evolution # Obtain the average Acc and nL for the different neurons applying # a common strop criteria. ave_val = np.ones((N_neurons, 1)) std_val = np.ones((N_neurons, 1)) ave_NLs = np.ones((N_neurons, 1)) std_Nls = np.ones((N_neurons, 1)) for nh_i in range(N_neurons): # Obtain nLs nLs = np.ones(N_realizations[nh_i]) accuracies = np.ones((N_realizations[nh_i], 1)) for j in range(N_realizations[nh_i]): # For every realization nLs[j] = mu.check_stop(All_gammas[nh_i][j], 5) # Get the NLs statistics ave_NLs[nh_i] = np.mean(nLs) std_Nls[nh_i] = np.std(nLs) for j in range(N_realizations[nh_i]): # For every realization pene = All_scoreVal_layers[nh_i][j] accuracies[j] = pene[nLs[j] - 1] ave_val[nh_i] = np.mean(accuracies) std_val[nh_i] = np.std(accuracies) mg.plot_accu_nH(nH_list, ave_val, std_val) plt.savefig(base_folder_out + str(database_number) + "/" + "Ave_Accu(nH)" + str(N_epochs_list[Nepoch_i]) + ".png") plt.close("all") mg.plot_nL_nH(nH_list, ave_NLs, std_Nls) plt.savefig(base_folder_out + str(database_number) + "/" + "Ave_nLs(nH)" + str(N_epochs_list[Nepoch_i]) + ".png") plt.close("all") """ 2rd GRAPH """ # Plot the Average Training and Validation score !! mg.plot_3D_nH_nL(nH_list, nLs_list, All_aves_rea_tr) plt.savefig(base_folder_out + str(database_number) + "/" + "3D_Accu_tr(nH,nL)" + str(N_epochs_list[Nepoch_i]) + ".png") plt.close("all") mg.plot_3D_nH_nL(nH_list, nLs_list, All_aves_rea_val) plt.savefig(base_folder_out + str(database_number) + "/" + "3D_Accu_val(nH,nL)" + str(N_epochs_list[Nepoch_i]) + ".png") plt.close("all")
def PPE_nH(PPE_p, main_folder, database_number, beta_i, alpha_i, Nepoch_i, nL_max=300): # Maximum number of layers): # Folder where we read the RAW files folder = "../" + main_folder + "/ResultsNeoDSN" + str(database_number) # Folder where we read and store the Preread files base_folder_in = "../" + main_folder + "/PreRead/" # Folder where we store the graph base_folder_out = "../" + main_folder + "/Gold/" + str( database_number) + "/Nh/" results = pkl.load_pickle( base_folder_in + str(database_number) + "/" + "data_" + str(PPE_p.beta_indx_list[beta_i]) + "_" + str(PPE_p.alpha_indx_list[alpha_i]) + "_EVO", 1) # If the result file existis coz it was previously read if (results == []): print "FILE NOT PREREAD" raise ValueError exit(0) All_Object_list = results All_Object_list = All_Object_list[Nepoch_i] N_neurons = len(All_Object_list) nLs_list = np.array(range(nL_max)) + 1 # Obtain the score of training and validation for every layer and realization All_scoreTr_layers = [] All_scoreVal_layers = [] for i in range(N_neurons): scoreTr_layers, scoreVal_layers = rd.get_scores_layers( All_Object_list[i]) All_scoreTr_layers.append(scoreTr_layers) All_scoreVal_layers.append(scoreVal_layers) # Get the average value and std for all the layers of the validation score All_aves_rea_val = [] All_stds_rea_val = [] for i in range(N_neurons): matrix = mu.convert_to_matrix(All_scoreVal_layers[i], nL_max) aves_rea_val, stds_rea_val = mu.get_ave_std_unfilled_matrix(matrix) All_aves_rea_val.append(aves_rea_val) All_stds_rea_val.append(stds_rea_val) # Get the average value and std for all the layers of the training score All_aves_rea_tr = [] All_stds_rea_tr = [] for i in range(N_neurons): matrix = mu.convert_to_matrix(All_scoreTr_layers[i], nL_max) aves_rea_tr, stds_rea_tr = mu.get_ave_std_unfilled_matrix(matrix) All_aves_rea_tr.append(aves_rea_tr) All_stds_rea_tr.append(stds_rea_tr) # Get the gammas values and their average and std All_gammas = [] N_realizations = [] All_aves_gammas = [] All_stds_gam = [] for i in range(N_neurons): gammas = rd.get_gammas(All_Object_list[i]) All_gammas.append(gammas) N_realizations.append(len(gammas)) matrix = mu.convert_to_matrix(gammas) aves_gam, stds_gam = mu.get_ave_std_unfilled_matrix(matrix) All_aves_gammas.append(aves_gam) All_stds_gam.append(stds_gam) base_folder_out = base_folder_out + "a:" + str(alpha_i) + "/" + "b:" + str( beta_i) + "/" mu.create_dirs(base_folder_out) """ 1rd GRAPH """ # Plot the Average Training and Validation score !! # Plot the average and shit mg.plot_acc_nL_nH(PPE_p.nH_list[PPE_p.nH_indx_list], All_aves_rea_val) plt.savefig(base_folder_out + "Ave_Accu_val(nL,nH)_" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + ".png") plt.close("all") mg.plot_acc_nL_nH(PPE_p.nH_list[PPE_p.nH_indx_list], All_aves_rea_tr) plt.savefig(base_folder_out + "Ave_Accu_tr(nL,nH)_" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + ".png") plt.close("all") """ 2rd GRAPH """ # Plot the gammas evolution # Obtain the average Acc and nL for the different neurons applying # a common strop criteria. ave_val = np.ones((N_neurons, 1)) std_val = np.ones((N_neurons, 1)) ave_NLs = np.ones((N_neurons, 1)) std_Nls = np.ones((N_neurons, 1)) for nh_i in range(N_neurons): # Obtain nLs nLs = np.ones(N_realizations[nh_i]) accuracies = np.ones((N_realizations[nh_i], 1)) for j in range(N_realizations[nh_i]): # For every realization nLs[j] = mu.check_stop(All_gammas[nh_i][j], 5) # Get the NLs statistics ave_NLs[nh_i] = np.mean(nLs) std_Nls[nh_i] = np.std(nLs) for j in range(N_realizations[nh_i]): # For every realization pene = All_scoreVal_layers[nh_i][j] accuracies[j] = pene[nLs[j] - 1] ave_val[nh_i] = np.mean(accuracies) std_val[nh_i] = np.std(accuracies) mg.plot_accu_nH(PPE_p.nH_list[PPE_p.nH_indx_list], ave_val, std_val) plt.savefig(base_folder_out + "Ave_Accu(nH)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + ".png") plt.close("all") mg.plot_nL_nH(PPE_p.nH_list[PPE_p.nH_indx_list], ave_NLs, std_Nls) plt.savefig(base_folder_out + "Ave_nLs(nH)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + ".png") plt.close("all") """ 2rd GRAPH """ # Plot the Average Training and Validation score !! mg.plot_3D_nH_nL(PPE_p.nH_list[PPE_p.nH_indx_list], nLs_list, All_aves_rea_tr) plt.savefig(base_folder_out + "3D_Accu_tr(nH,nL)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + ".png") plt.close("all") mg.plot_3D_nH_nL(PPE_p.nH_list[PPE_p.nH_indx_list], nLs_list, All_aves_rea_val) plt.savefig(base_folder_out + "3D_Accu_val(nH,nL)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + ".png") plt.close("all")
def PPE_1Tr(PPE_p, main_folder, database_number, beta_i, alpha_i, Ninit_i, Roh_i, Nepoch_i, nH_i, nL_max=200): # Maximum number of layers): # Folder where we read the RAW files folder = "../" + main_folder + "/ResultsNeoDSN" + str(database_number) # Folder where we read and store the Preread files base_folder_in = "../" + main_folder + "/PreRead/" # Folder where we store the graph base_folder_out = "../" + main_folder + "/Gold/" + str( database_number) + "/Nl/" results = pkl.load_pickle( base_folder_in + str(database_number) + "/" + "data_" + str(beta_i) + "_" + str(alpha_i) + "_EVO", 1) # If the result file existis coz it was previously read if (results == []): print "FILE NOT PREREAD" raise ValueError exit(0) All_Object_list = results nLs_list = np.array(range(nL_max)) + 1 Object_list = All_Object_list[Ninit_i][Roh_i][Nepoch_i][nH_i] # Obtain the score of training and validation for every layer and realization scoreTr_layers, scoreVal_layers = rd.get_scores_layers(Object_list) # Get the average value and std for all the layers of the validation score matrix = mu.convert_to_matrix(scoreVal_layers, nL_max) aves_rea_val, stds_rea_val = mu.get_ave_std_unfilled_matrix(matrix) # Get the average value and std for all the layers of the training score matrix = mu.convert_to_matrix(scoreTr_layers, nL_max) aves_rea_tr, stds_rea_tr = mu.get_ave_std_unfilled_matrix(matrix) # Get the gammas values and their average and std gammas = rd.get_gammas(Object_list) N_realizations = len(gammas) # Number of realizations matrix = mu.convert_to_matrix(gammas) aves_gam, stds_gam = mu.get_ave_std_unfilled_matrix(matrix) base_folder_out = base_folder_out + "a:" + str(alpha_i) + "/" + "b:" + str( beta_i) + "/" base_folder_out = base_folder_out + "Ninit:" + str( Ninit_i) + "/" + "Roh:" + str(Roh_i) + "/" mu.create_dirs(base_folder_out) """ 1st GRAPH """ # Plot all realizations for the given number of Epoch and Neurons mg.plot_all_realizations_EVO(scoreTr_layers, scoreVal_layers) # Save figure !! plt.savefig(base_folder_out + "All_rea_" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + ".png") plt.close("all") """ 2nd GRAPH """ # Plot the Average Training and Validation score !! # Plot the average and shit mg.plot_tr_val_nL(nLs_list, aves_rea_val, aves_rea_tr, stds_rea_val, stds_rea_tr) plt.savefig(base_folder_out + "Ave_Acc(nL)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + ".png") plt.close("all") """ 3rd GRAPH """ # Plot the average gamma value in function of the number of layers # Also plot where the nL would be stopped applying the rule. nLs = np.ones(N_realizations) for i in range(N_realizations): # For every realization nLs[i] = mu.check_stop(gammas[i]) mg.plot_gamma_nL(aves_gam, stds_gam) plt.scatter(nLs, np.mean(aves_gam) * np.ones(N_realizations)) plt.savefig(base_folder_out + "Ave_gamma(nLs)" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + ".png") plt.close("all") """ 4th GRAPH """ # Plot the average Accuracy and Number of layers depending on the # stopping condition given by the ngamma ngammas_list = range(1, 20) accuracies = np.ones((N_realizations, 1)) ave_val = np.ones((len(ngammas_list), 1)) std_val = np.ones((len(ngammas_list), 1)) ave_NLs = np.ones((len(ngammas_list), 1)) std_Nls = np.ones((len(ngammas_list), 1)) for i in range(len(ngammas_list)): # Obtain nLs nLs = np.ones(N_realizations) for j in range(N_realizations): # For every realization nLs[j] = mu.check_stop(gammas[j], ngammas_list[i]) # Get the NLs statistics ave_NLs[i] = np.mean(nLs) std_Nls[i] = np.std(nLs) for j in range(N_realizations): # For every realization accuracies[j] = scoreVal_layers[j][nLs[j] - 1] ave_val[i] = np.mean(accuracies) std_val[i] = np.std(accuracies) mg.plot_accu_ngamma(ngammas_list, ave_val, std_val) plt.savefig(base_folder_out + "/" + "Ave_Accu(ngamma)_" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + ".png") plt.close("all") mg.plot_nL_ngamma(ngammas_list, ave_NLs, std_Nls) plt.savefig(base_folder_out + "Ave_nL(ngamma)_" + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "_" + str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + ".png") plt.close("all")