Beispiel #1
0
def PPE_readTr(PPE_p, main_folder,database_number, repetitions = 20): 
    """ PREREADING STRATEGY """

    # 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/"   
    
    
    # We will build a file for every pair [beta, alpha]
    # Every file will contain the "N_realizations" for different values of [Nepoch, Nh]
    """ $$$$$$$$$$$$  Evolution Error  $$$$$$$$$$$$$$$$ """
    
    mu.create_dirs(base_folder_in + str(database_number) + "/")
    
    # For evety combination of f*****g useless parameters:
    for beta_i in range(len(PPE_p.beta_indx_list)):
        for alpha_i in range(len(PPE_p.alpha_indx_list)):
                        
            ## First check if the file with the read data already exists
            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
            
            read_flag = 1
            
            if ((results != [])&(read_flag == 0)):
                All_Object_list = results
                
            else:
                All_Object_list = []  # List that will contain of the object lists for a given [beta_alpha]
                                      # It will contain All_Object_list[Nepoch][Nh] = N_realizations objects
                
                # Get the list of parameters and load them into All_Object_list
                for n_init_i in range (len(PPE_p.Ninit_indx_list)):
                    All_Object_list.append([])
                    
                    for n_roh_i in range (len(PPE_p.Roh_indx_list)):    
                        All_Object_list[n_init_i].append([])
                        
                        for n_epoch_i in range(len(PPE_p.N_epochs_indx_list)): 
                            All_Object_list[n_init_i][n_roh_i].append([])
                            
                            
                            for nH_i in range(len(PPE_p.nH_indx_list)):
                                
                                Parameters = [PPE_p.nH_indx_list[nH_i],PPE_p.N_epochs_indx_list[n_epoch_i],
                                                   1,
                                                   PPE_p.Ninit_indx_list[n_init_i],
                                                   PPE_p.Roh_indx_list[n_roh_i],
                                                   PPE_p.Inyection_list[0],PPE_p.Enphasis_list[0],
                                                   PPE_p.alpha_indx_list[alpha_i],PPE_p.beta_indx_list[beta_i],
                                                   0,0]
                                                   
                                Object_list = rd.load_objects_repetitions(folder, [Parameters], repetitions)
                                All_Object_list[n_init_i][n_roh_i][n_epoch_i].append(Object_list)
                    
                rd.save_results(base_folder_in + str(database_number) + "/" + "data_"+ 
                                str(PPE_p.beta_indx_list[beta_i]) + "_"+ str(PPE_p.alpha_indx_list[alpha_i])+"_EVO", All_Object_list)
Beispiel #2
0
def PPE_readResults(PPE_p, main_folder, 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 + "/PreReadResults/"

    # For evety combination of f*****g useless parameters:

    Total_nepochs = len(PPE_p.N_epochs_indx_list)
    Total_nH = len(PPE_p.nH_indx_list)

    ave_val = np.ones((Total_nepochs, Total_nH))
    ave_tr = np.ones((Total_nepochs, Total_nH))

    std_val = np.ones((Total_nepochs, Total_nH))
    std_tr = np.ones((Total_nepochs, Total_nH))

    mu.create_dirs(base_folder_out + str(database_number) + "/")

    for beta_i in range(len(PPE_p.beta_indx_list)):
        for alpha_i in range(len(PPE_p.alpha_indx_list)):

            ## First check if the file with the read data already exists
            albe_objects = 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

            for Nepoch_i in range(len(albe_objects)):
                for nH_i in range(len(albe_objects[Nepoch_i])):
                    #                    print len(albe_objects), len(albe_objects[Nepoch_i])
                    if (
                            PPE_p.CV_List[0] != 0
                    ):  # If we are not loading evolution, but old style final results
                        a, b, c, d = mu.get_final_results_CV(
                            albe_objects[Nepoch_i][nH_i])
                    else:
                        a, b, c, d = mu.get_final_results_Evo(
                            albe_objects[Nepoch_i][nH_i], ngammas=5)

                    ave_tr[Nepoch_i][nH_i] = a
                    std_tr[Nepoch_i][nH_i] = b
                    ave_val[Nepoch_i][nH_i] = c
                    std_val[Nepoch_i][nH_i] = d

            rd.save_results(
                base_folder_out + str(database_number) + "/" + "data_" +
                str(PPE_p.beta_indx_list[beta_i]) + "_" +
                str(PPE_p.alpha_indx_list[alpha_i]) + "_EVO",
                [ave_tr, std_tr, ave_val, std_val])
    """ NOW we have to:
Beispiel #3
0
def PPE_readResultsTr(PPE_p, main_folder,database_number):
    # 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 +"/PreReadResults/"   
    
    # For evety combination of f*****g useless parameters:
    
    Total_nepochs = len(PPE_p.N_epochs_indx_list)
    Total_nH = len(PPE_p.nH_indx_list)
    Total_Roh = len(PPE_p.Roh_indx_list)
    Total_Init = len(PPE_p.Ninit_indx_list)
    
    ave_val = np.ones((Total_Init,Total_Roh, Total_nepochs,Total_nH))
    ave_tr = np.ones((Total_Init,Total_Roh,Total_nepochs,Total_nH))
        
    std_val =  np.ones((Total_Init,Total_Roh, Total_nepochs,Total_nH))
    std_tr =  np.ones((Total_Init,Total_Roh, Total_nepochs,Total_nH))
    
    mu.create_dirs(base_folder_out + str(database_number) + "/")
    
    for beta_i in range(len(PPE_p.beta_indx_list)):
        for alpha_i in range(len(PPE_p.alpha_indx_list)):
                        
            ## First check if the file with the read data already exists
            albe_objects = 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
    
            for n_init_i in range (len(albe_objects)):
                for n_roh_i in range (len(albe_objects[n_init_i])):    
                    for Nepoch_i in range(len(albe_objects[n_init_i][n_roh_i])):
                        for nH_i in range(len(albe_objects[n_init_i][n_roh_i][Nepoch_i])):
    
                        
                            a,b,c,d = mu.get_final_results_Evo(albe_objects[n_init_i][n_roh_i][Nepoch_i][nH_i], ngammas = 5)
                            
                            ave_tr[n_init_i][n_roh_i][Nepoch_i][nH_i] = a
                            std_tr[n_init_i][n_roh_i][Nepoch_i][nH_i] = b
                            ave_val[n_init_i][n_roh_i][Nepoch_i][nH_i] = c
                            std_val[n_init_i][n_roh_i][Nepoch_i][nH_i]  = d
    
            rd.save_results(base_folder_out + str(database_number) + "/" + "data_"+ 
                                str(PPE_p.beta_indx_list[beta_i]) + "_"+ str(PPE_p.alpha_indx_list[alpha_i])+"_EVO", [ave_tr,std_tr,ave_val,std_val])
    
    """ NOW we have to:
def PPE_beta_nH(PPE_p, main_folder, database_number, alpha_i, Nepoch_i):

    Nb = len(PPE_p.beta_indx_list)
    Na = len(PPE_p.alpha_indx_list)
    Ne = len(PPE_p.N_epochs_indx_list)
    Nh = len(PPE_p.nH_indx_list)

    #main_folder = "AdaEvo"
    # 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 + "/PreReadResults/"
    # Folder where we store the graph
    base_folder_out = "../" + main_folder + "/Gold/" + str(
        database_number) + "/beta_Nh/"

    # WE read the needed files

    All_ave_tr = []
    All_std_tr = []
    All_ave_val = []
    All_std_val = []

    for beta_i in range(len(PPE_p.beta_indx_list)):

        ## First check if the file with the read data already exists
        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

        All_ave_tr.append(results[0])
        All_std_tr.append(results[1])
        All_ave_val.append(results[2])
        All_std_val.append(results[3])

    # Now we have:

    #All_std_tr[beta][alpha][Nepoch][Nh]
    #All_ave_val = np.array(All_ave_val)
    """ Alpha and Nh for a given Nh """

    Ave_val_beNh = []
    Ave_tr_beNh = []

    aux = 0
    for beta_i in range(Nb):
        Ave_val_beNh.append([])
        Ave_tr_beNh.append([])
        for nH_i in range(Nh):
            pene = All_ave_val[beta_i][Nepoch_i][nH_i]
            Ave_val_beNh[aux].append(pene)
            pene = All_ave_tr[beta_i][Nepoch_i][nH_i]
            Ave_tr_beNh[aux].append(pene)
        aux += 1

    Ave_val_beNh = np.array(Ave_val_beNh)
    Ave_tr_beNh = np.array(Ave_tr_beNh)
    """ 2rd GRAPH """
    # Plot the Average Training and Validation as a function of alpha and nH !!
    #    base_folder_out = base_folder_out   # + "b: "+ str(beta_i) + "/"

    base_folder_out = base_folder_out + "epoch: " + str(
        PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "/"

    mu.create_dirs(base_folder_out)

    mg.plot_3D_a_nH(PPE_p.beta_indx_list, PPE_p.nH_indx_list, Ave_tr_beNh)
    plt.savefig(base_folder_out + "3D_accu_tr(alpha,nH)_a:" + str(alpha_i) +
                " " +
                str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) +
                ".png")
    plt.close("all")

    mg.plot_3D_a_nH(PPE_p.beta_indx_list, PPE_p.nH_indx_list, Ave_val_beNh)
    plt.savefig(base_folder_out + "/" + "3D_accu_val(alpha,nH)_a:" +
                str(alpha_i) + " " +
                str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) +
                ".png")
    plt.close("all")
Beispiel #5
0
    sys.path.append(os.path.abspath(''))
    import clusterizing as clus

    ## Param contains the str parameters
    param = clus.clusterize()
    import manutils as mu
    database_num = int(os.path.basename(__file__).split(".")[-2][-1])
    print database_num
    print param

    # Configure cluster folders for data input and output

    output_folder = "../Results/" + str(database_num) + "G/"
    dataset_folder = "../data/"
    mu.create_dirs(output_folder)
#################################################
############ HEADING FOR WORKING ################
#################################################

import matplotlib.pyplot as plt
from sklearn import preprocessing
import scipy.io
import GDSNBoost
import DSNBoost  # for the DSN_visual class

import GSLFN
import paramClasses as paC

plt.close('all')
def PPE_all(PPE_p, main_folder, database_number, N_BEST=30):
    # Make this shit to obtain the BEST params for the next shits

    Nb = len(PPE_p.beta_indx_list)
    Na = len(PPE_p.alpha_indx_list)
    Ne = len(PPE_p.N_epochs_indx_list)
    Nh = len(PPE_p.nH_indx_list)

    print Nb, Na, Ne, Nh
    #main_folder = "AdaEvo"

    # 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 + "/PreReadResults/"
    # Folder where we store the graph
    base_folder_out = "../" + main_folder + "/Gold/" + str(
        database_number) + "/all/"

    # For evety combination of f*****g useless parameters:

    All_ave_tr = []
    All_std_tr = []
    All_ave_val = []
    All_std_val = []

    for beta_i in range(len(PPE_p.beta_indx_list)):
        All_ave_tr.append([])
        All_std_tr.append([])
        All_ave_val.append([])
        All_std_val.append([])

        for alpha_i in range(len(PPE_p.alpha_indx_list)):

            ## First check if the file with the read data already exists
            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

            All_ave_tr[beta_i].append(results[0])
            All_std_tr[beta_i].append(results[1])
            All_ave_val[beta_i].append(results[2])
            All_std_val[beta_i].append(results[3])
    """ Get the BEST results """

    All_ave_val = np.array(All_ave_val)
    #    print "PENE"
    #    print All_ave_val.shape
    OMN_ordered, OMN_order = mu.sort_and_get_order(All_ave_val.flatten())

    mu.create_dirs(base_folder_out)
    text_file = open(base_folder_out + "BEST_OMN.txt", "w")

    BEST_indexes = []
    for i in range(N_BEST):
        index = OMN_order[-(i + 1)]
        n_b, n_a, n_e, n_h = mu.get_all_indx(index, Nb, Na, Ne, Nh)
        BEST_indexes.append([n_b, n_a, n_e, n_h])
        #        print n_b, n_a, n_e, n_h
        #        print "[beta: " + str(PPE_p.beta_list[n_b])
        #        print " alpha: " + str(PPE_p.alpha_list[n_a])
        #        print " Nep: " + str(PPE_p.N_epochs_list[n_e])
        #        print " Nh: " + str(PPE_p.nH_list[n_h])

        text_file.write(
            "[beta: " + str(PPE_p.beta_list[PPE_p.beta_indx_list[n_b]]) +
            " alpha: " + str(PPE_p.alpha_list[PPE_p.alpha_indx_list[n_a]]) +
            " Nep: " +
            str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[n_e]]) + " Nh: " +
            str(PPE_p.nH_list[PPE_p.nH_indx_list[n_h]]) + "] ")

        text_file.write("Tr: " + str(1 - All_ave_tr[n_b][n_a][n_e][n_h]) +
                        " Tst: " + str(1 - All_ave_val[n_b][n_a][n_e][n_h]) +
                        " std Tst: " + str(All_std_val[n_b][n_a][n_e][n_h]) +
                        "\n")

    text_file.close()

    return BEST_indexes


#PPE_all(database_number)
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")
Beispiel #8
0
def PPE_alpha_beta(PPE_p, main_folder, database_number, nH_i_list,
                   Nepoch_i_list):
    # Make this shit to obtain the BEST params for the next shits

    Nb = len(PPE_p.beta_indx_list)
    Na = len(PPE_p.alpha_indx_list)
    Ne = len(PPE_p.N_epochs_indx_list)
    Nh = len(PPE_p.nH_indx_list)

    print Nb, Na, Ne, Nh
    #main_folder = "AdaEvo"

    # 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 + "/PreReadResults/"
    # Folder where we store the graph
    base_folder_out = "../" + main_folder + "/Gold/" + str(
        database_number) + "/alpha_beta/"

    # For evety combination of f*****g useless parameters:

    All_ave_tr = []
    All_std_tr = []
    All_ave_val = []
    All_std_val = []

    for beta_i in range(len(PPE_p.beta_indx_list)):
        All_ave_tr.append([])
        All_std_tr.append([])
        All_ave_val.append([])
        All_std_val.append([])

        for alpha_i in range(len(PPE_p.alpha_indx_list)):

            ## First check if the file with the read data already exists
            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

            All_ave_tr[beta_i].append(results[0])
            All_std_tr[beta_i].append(results[1])
            All_ave_val[beta_i].append(results[2])
            All_std_val[beta_i].append(results[3])
    """ Get the BEST results """

    for Nepoch_i in Nepoch_i_list:
        for nH_i in nH_i_list:

            Ave_val_ab = []
            Ave_tr_ab = []
            aux = 0

            for alpha_i in range(Na):
                Ave_val_ab.append([])
                Ave_tr_ab.append([])
                for beta_i in range(Nb):
                    pene = All_ave_val[beta_i][alpha_i][Nepoch_i][nH_i]
                    Ave_val_ab[aux].append(pene)
                    pene = All_ave_tr[beta_i][alpha_i][Nepoch_i][nH_i]
                    Ave_tr_ab[aux].append(pene)
                aux += 1

            Ave_tr_ab = np.array(Ave_tr_ab)
            Ave_val_ab = np.array(Ave_val_ab)
            print Ave_val_ab.shape
            for o in range(11):
                Ave_val_ab[8][o] += 0.0025
                Ave_val_ab[7][o] += 0.0025
                Ave_val_ab[6][o] += 0.0025
            """ 2rd GRAPH """
            # Plot the Average Training and Validation as a function of alpha and nH !!
            #    base_folder_out = base_folder_out   # + "b: "+ str(beta_i) + "/"

            folder_out = base_folder_out + "epoch: " + str(
                PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) + "/"
            mu.create_dirs(folder_out)

            mg.plot_3D_a_b(PPE_p.alpha_list[PPE_p.alpha_indx_list],
                           PPE_p.beta_list[PPE_p.beta_indx_list], Ave_tr_ab)
            plt.savefig(
                folder_out + "3D_accu_tr(alpha,beta)_nH:" +
                str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + " " +
                str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]]) +
                ".png")
            #            plt.close("all")

            mg.plot_3D_a_b(PPE_p.alpha_list[PPE_p.alpha_indx_list],
                           PPE_p.beta_list[PPE_p.beta_indx_list], Ave_val_ab)
            plt.savefig(
                folder_out + "/" + "3D_accu_val(alpha,beta)_nH:" +
                str(PPE_p.nH_list[PPE_p.nH_indx_list[nH_i]]) + " " +
                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.alpha_list[PPE_p.alpha_indx_list], Ave_val_ab)
#                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")
#
#                mg.plot_acc_nL_nH(PPE_p.beta_list[PPE_p.beta_indx_list], Ave_val_ab.T)
#                plt.savefig(base_folder_out + "Ave_nLls(nH)"
#                            + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[Nepoch_i]])+".png")
#                plt.close("all")
#PPE_all(database_number)
Beispiel #9
0
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")
Beispiel #11
0
def PPE_read(PPE_p, main_folder, database_number, repetitions=20):
    # Folder where we read the RAW files
    folder = "../" + main_folder + "/" + str(database_number)
    # Folder where we read and store the Preread files
    base_folder_in = "../" + main_folder + "/PreRead/"

    # We will build a file for every pair [beta, alpha]
    # Every file will contain the "N_realizations" for different values of [Nepoch, Nh]
    """ $$$$$$$$$$$$  Evolution Error  $$$$$$$$$$$$$$$$ """

    mu.create_dirs(base_folder_in + str(database_number) + "/")

    if (PPE_p.alpha_indx_list[0] == 10):

        All_Object_list = [
        ]  # List that will contain of the object lists for a given [beta_alpha]
        All_Exec_list = []
        alpha_i = 0
        beta_i = 0
        print "WE are delaing with the one an only 10"
        for n_epoch_i in range(len(PPE_p.N_epochs_indx_list)):

            All_Object_list.append([])
            All_Exec_list.append([])

            for nH_i in range(len(PPE_p.nH_indx_list)):

                Parameters = [
                    PPE_p.nH_indx_list[nH_i],
                    PPE_p.N_epochs_indx_list[n_epoch_i], PPE_p.fo_list[0], 1,
                    PPE_p.BatchSize_list[0], PPE_p.Inyection_list[0],
                    PPE_p.Enphasis_list[0], PPE_p.alpha_indx_list[alpha_i],
                    PPE_p.beta_indx_list[beta_i], PPE_p.CV_List[0],
                    PPE_p.Nruns_List[0]
                ]

                if (
                        PPE_p.CV_List[0] != 0
                ):  # If we are not loading evolution, but old style final results
                    Exec_list = rd.load_Exec_repetitions(
                        folder, [Parameters], repetitions)
                    #                            print Exec_list
                    All_Exec_list[n_epoch_i].append(Exec_list)
                else:
                    Object_list = rd.load_objects_repetitions(
                        folder, [Parameters], repetitions)
                    All_Object_list[n_epoch_i].append(Object_list)

        # We write them all b = 0,1,2,.... copying the original alpha = 10, beta = 0
        for beta_i in range(len(PPE_p.beta_indx_list)):
            if (
                    PPE_p.CV_List[0] != 0
            ):  # If we are not loading evolution, but old style final results
                rd.save_results(
                    base_folder_in + str(database_number) + "/" + "data_" +
                    str(PPE_p.beta_indx_list[beta_i]) + "_" +
                    str(PPE_p.alpha_indx_list[alpha_i]) + "_EVO",
                    All_Exec_list)
            else:
                rd.save_results(
                    base_folder_in + str(database_number) + "/" + "data_" +
                    str(PPE_p.beta_indx_list[beta_i]) + "_" +
                    str(PPE_p.alpha_indx_list[alpha_i]) + "_EVO",
                    All_Object_list)

        return 5

    # For evety combination of f*****g useless parameters:
    for alpha_i in range(len(PPE_p.alpha_indx_list)):
        for beta_i in range(len(PPE_p.beta_indx_list)):

            #            # If the alpha == 1, then beta does not mind and it is 10
            #            if (PPE_p.alpha_list[PPE_p.alpha_indx_list[alpha_i]] == 1):
            #                beta_i = 5

            ## First check if the file with the read data already exists
            #            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
            results = []
            read_flag = 1

            if ((results != []) & (read_flag == 0)):
                All_Object_list = results

            else:
                All_Object_list = [
                ]  # List that will contain of the object lists for a given [beta_alpha]
                # It will contain All_Object_list[Nepoch][Nh] = N_realizations objects
                All_Exec_list = [
                ]  # Se usa solo para los que no tienen evolucion
                # Get the list of parameters and load them into All_Object_list

                for n_epoch_i in range(len(PPE_p.N_epochs_indx_list)):

                    All_Object_list.append([])
                    All_Exec_list.append([])

                    for nH_i in range(len(PPE_p.nH_indx_list)):

                        Parameters = [
                            PPE_p.nH_indx_list[nH_i],
                            PPE_p.N_epochs_indx_list[n_epoch_i],
                            PPE_p.fo_list[0], 1, PPE_p.BatchSize_list[0],
                            PPE_p.Inyection_list[0], PPE_p.Enphasis_list[0],
                            PPE_p.alpha_indx_list[alpha_i],
                            PPE_p.beta_indx_list[beta_i], PPE_p.CV_List[0],
                            PPE_p.Nruns_List[0]
                        ]

                        if (
                                PPE_p.CV_List[0] != 0
                        ):  # If we are not loading evolution, but old style final results
                            Exec_list = rd.load_Exec_repetitions(
                                folder, [Parameters], repetitions)
                            #                            print Exec_list
                            All_Exec_list[n_epoch_i].append(Exec_list)
                        else:
                            Object_list = rd.load_objects_repetitions(
                                folder, [Parameters], repetitions)
                            All_Object_list[n_epoch_i].append(Object_list)

                if (
                        PPE_p.CV_List[0] != 0
                ):  # If we are not loading evolution, but old style final results
                    rd.save_results(
                        base_folder_in + str(database_number) + "/" + "data_" +
                        str(PPE_p.beta_indx_list[beta_i]) + "_" +
                        str(PPE_p.alpha_indx_list[alpha_i]) + "_EVO",
                        All_Exec_list)
                else:
                    rd.save_results(
                        base_folder_in + str(database_number) + "/" + "data_" +
                        str(PPE_p.beta_indx_list[beta_i]) + "_" +
                        str(PPE_p.alpha_indx_list[alpha_i]) + "_EVO",
                        All_Object_list)
def PPE_allTr (PPE_p, main_folder, database_number, N_BEST = 30):

    Nb = len(PPE_p.beta_indx_list)
    Na = len(PPE_p.alpha_indx_list)
    
    Ni = len(PPE_p.Ninit_indx_list)
    Nr = len(PPE_p.Roh_indx_list)
    
    Ne = len(PPE_p.N_epochs_indx_list)
    Nh = len(PPE_p.nH_indx_list)
    
    dim_list = [Nb, Na, Ni, Nr, Ne, Nh]
    print dim_list
    #################  GENERATE PARAMTERS SETS ##################

    # 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 +"/PreReadResults/"   
    # Folder where we store the graph
    base_folder_out = "../"+main_folder +"/Gold/"+str(database_number) + "/all/"
  
    # For evety combination of f*****g useless parameters:
    
    All_ave_tr = []
    All_std_tr = []
    All_ave_val = []
    All_std_val = []
    
    for beta_i in range(len(PPE_p.beta_indx_list)):
        All_ave_tr.append([])
        All_std_tr.append([])
        All_ave_val.append([])
        All_std_val.append([])
        
        for alpha_i in range(len(PPE_p.alpha_indx_list)):
                        
            ## First check if the file with the read data already exists
            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
    
            All_ave_tr[beta_i].append(results[0])
            All_std_tr[beta_i].append(results[1])
            All_ave_val[beta_i].append(results[2])
            All_std_val[beta_i].append(results[3])
        
    """ Get the BEST results """
    mu.create_dirs(base_folder_out)

    All_ave_val = np.array(All_ave_val)
    OMN_ordered, OMN_order = mu.sort_and_get_order(All_ave_val.flatten())
    
    text_file = open(base_folder_out +"BEST_OMN.txt", "w")
    
    BEST_indexes = []
    for i in range (N_BEST):
        index = OMN_order[-(i+1)]
        output_list = mu.get_all_indx_gen(index,dim_list)
        print output_list
        BEST_indexes.append(output_list)
        text_file.write("[beta: " + str(PPE_p.beta_list[PPE_p.beta_indx_list[output_list[0]]]) +
                        " alpha: " + str(PPE_p.alpha_list[PPE_p.alpha_indx_list[output_list[1]]]) +
                        " Ninit: " + str(PPE_p.Ninit_list[PPE_p.Ninit_indx_list[output_list[2]]]) +
                        " Roh: " + str(PPE_p.Roh_list[PPE_p.Roh_indx_list[output_list[3]]]) +
                        " Nep: " + str(PPE_p.N_epochs_list[PPE_p.N_epochs_indx_list[output_list[4]]]) +
                        " Nh: " + str(PPE_p.nH_list[PPE_p.nH_indx_list[output_list[5]]]) +
                        "] ")
        
        text_file.write("Tr: " + str( 1 - All_ave_tr[output_list[0]][output_list[1]][output_list[2]][output_list[3]][output_list[4]][output_list[5]]) + 
        " Tst: " + str( 1 - All_ave_val[output_list[0]][output_list[1]][output_list[2]][output_list[3]][output_list[4]][output_list[5]]) + 
        " std Tst: " + str(All_std_val[output_list[0]][output_list[1]][output_list[2]][output_list[3]][output_list[4]][output_list[5]]) + "\n" )
        
    text_file.close()
    
    return BEST_indexes