def plot_3D_a_b (alphas,betas, aves_OMN): # Plots the training and Validation score of a realization X = np.array(alphas) Y = np.array(betas) X, Y = np.meshgrid(X, Y) Z = mu.convert_to_matrix(aves_OMN).T from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt fig = plt.figure() ax = fig.gca(projection='3d') plt.tight_layout(pad=2, w_pad=0.5, h_pad=1.0) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.gray_r, linewidth=0) ax.set_zlim(np.min(Z.flatten()), np.max(Z.flatten())) ax.zaxis.set_major_locator(LinearLocator(8)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) plt.xlabel(r'$\alpha$', fontsize=25) plt.ylabel(r'$\beta$', fontsize=25) # fig.colorbar(surf, shrink=0.5, aspect=5) ax.set_zlabel('% accuracy rate', fontsize=15) plt.show()
def plot_3D_a_nH (alphas,nHs, aves_OMN): # Plots the training and Validation score of a realization X = np.array(alphas) Y = np.array(nHs) X, Y = np.meshgrid(X, Y) Z = mu.convert_to_matrix(aves_OMN).T from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) ax.set_zlim(np.min(Z.flatten()), np.max(Z.flatten())) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) plt.xlabel('alpha') plt.ylabel('Hidden neurons') fig.colorbar(surf, shrink=0.5, aspect=5) ax.set_zlabel('Z Label') plt.show()
nL_max = 200 # Maximum number of layers # 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)
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")
def plot_all_tr_val_nL_surface_XXXXXXXXX (nHs,nLs, aves_OMN, aves_OMN_tr): # Plots the training and Validation score of a realization N_neurons = len(aves_OMN) X = np.array(nHs) Y = np.array(nLs) X, Y = np.meshgrid(X, Y) # print X # print Y Z = mu.convert_to_matrix(aves_OMN).T # Z2 = mu.convert_to_matrix(aves_OMN_tr).T # Z = Z.flatten() # print X.shape, Y.shape # print Z.shape from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt fig = plt.figure() ax = fig.gca(projection='3d') """ VAL """ surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) ax.set_zlim(np.min(Z.flatten()), np.max(Z.flatten())) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) plt.xlabel('Hidden neurons') plt.ylabel('Number of Layers') fig.colorbar(surf, shrink=0.5, aspect=5) """ TR """ # surf = ax.plot_surface(X, Y, Z2, rstride=1, cstride=1, cmap=cm.coolwarm, # linewidth=0, antialiased=False) # # ax.set_zlim(np.min(Z2.flatten()), np.max(Z2.flatten())) # ax.zaxis.set_major_locator(LinearLocator(10)) # ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # # plt.xlabel('Hidden neurons') # plt.ylabel('Number of Layers') # fig.colorbar(surf, shrink=0.5, aspect=5) # # plt.show() #============================================================================== # X = np.array(nHs) # Y = np.array(nLs) # # X, Y = np.meshgrid(nHs, nLs) # # print X # print Y # Z = convert_to_matrix(aves_OMN) # # Z = Z.flatten() # # print X.shape, Y.shape # print Z.shape # # from mpl_toolkits.mplot3d import Axes3D # import matplotlib.pyplot as plt # # # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # # for x in range(X.size): # for y in range(Y.size): # ax.scatter(X[x], Y[y], Z[x,y]) # # ax.set_xlabel('X Label') # ax.set_ylabel('Y Label') #============================================================================== ax.set_zlabel('Z Label') plt.show()