Exemplo n.º 1
0
def load_RNN_data_generated(folder_data =  "../data/artificial/"):

    Ndivisions = 10;

    
    X_list = pkl.load_pickle(folder_data +"X_values.pkl",Ndivisions)
    Y_list = pkl.load_pickle(folder_data +"Y_values.pkl",Ndivisions)
    
    num_steps, X_dim = X_list[0].shape
    num_chains = len(X_list)
    
    
    ## Divide in train val and test
    proportion_tr = 0.8
    proportion_val = 0.1
    proportion_tst = 1 -( proportion_val + proportion_tr)
    
    num_tr = 1000
    num_val = 500
    num_tst = 500
    
    train_X = [X_list[i] for i in range(num_tr)]
    train_Y = [Y_list[i] for i in range(num_tr)]
    
    val_X = [X_list[i] for i in range(num_tr, num_tr + num_val)]
    val_Y = [Y_list[i] for i in range(num_tr, num_tr + num_val)]
    
    tst_X = [X_list[i] for i in range(num_tr + num_val,  num_tr + num_val + num_tst)]
    tst_Y = [Y_list[i] for i in range(num_tr + num_val,  num_tr + num_val + num_tst)]

    train_X = np.concatenate(train_X,1).T
    return train_X
Exemplo n.º 2
0
def load_objects_repetitions(folder_name, params_list=[], repetitions=1):

    # Loads the objects (for intermediate results) of the param list.
    # The difference is that we do get all the objects of one param_list
    # instead of only getting one of them.
    # It is meant to be for just one object configuration

    Object_list = []  # List with the paramters of the simulation

    for paramS in params_list:

        file_name = str('')

        if type(paramS) is list:
            for param in paramS:
                file_name += str(param) + "_"

        else:  # If there was only one parameter validaded
            file_name += str(paramS) + "_"

        path_obj = "./" + folder_name + "/" + file_name

        ### NOW WE INTRODUCE THE REPETITION PART !!!!

        Object_list = []
        for i in range(repetitions):
            path_obj_rep = path_obj + str(i) + "_"
            obj = pkl.load_pickle(path_obj_rep, 1)

            if (obj == []):  # The object does not exist
                print path_obj + " does not exist"
            else:
                Object_list.append(obj[1])
                # we saved the object as a list.
    return Object_list
Exemplo n.º 3
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)
Exemplo n.º 4
0
def load_results(folder_name, params_list=[]):
    # params is a bidimensional list where each row [i,:] is a list
    # of all the parameters that were crossvalidated !!

    Object_list = []  # List with the paramters of the simulation
    Exec_list = []  # Object with the results of the simulation

    for paramS in params_list:
        file_name = str('')

        if type(paramS) is list:

            for param in paramS:
                file_name += str(param) + "_"

        else:  # If there was only one parameter validaded
            file_name += str(paramS) + "_"

        path_obj = "./" + folder_name + "/" + file_name
        obj = pkl.load_pickle(path_obj, 1)
        if (obj == []):  # The object does not exist
            print path_obj + " does not exist"
        else:
            Exec_list.append(obj[0])
            Object_list.append(
                obj[1])  # The 0 is because pickle only saves lists
            # we saved the object as a list.
    return Exec_list, Object_list
Exemplo n.º 5
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:
Exemplo n.º 6
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:
Exemplo n.º 7
0
def load_results_repetitions(folder_name, params_list=[], repetitions=1):

    # Loads the results that have been donde in various repetitions in the cluster
    # params is a bidimensional list where each row [i,:] is a list
    # of all the parameters that were crossvalidated !!

    Object_list = []  # List with the paramters of the simulation
    Exec_list = []  # Object with the results of the simulation

    for paramS in params_list:

        file_name = str('')

        if type(paramS) is list:
            for param in paramS:
                file_name += str(param) + "_"

        else:  # If there was only one parameter validaded
            file_name += str(paramS) + "_"

        path_obj = "./" + folder_name + "/" + file_name

        ### NOW WE INTRODUCE THE REPETITION PART !!!!

        obj_rep = []
        for i in range(repetitions):
            path_obj_rep = path_obj + str(i) + "_"
            obj = pkl.load_pickle(path_obj_rep, 1)

            if (obj == []):  # The object does not exist
                print path_obj + " does not exist"
            else:
                obj_rep.append(obj)

        if (obj_rep != []):  # IF there was any file of the repetition
            Exec, Object = fuse_rep(obj_rep)

            Exec_list.append(Exec)
            Object_list.append(
                Object)  # The 0 is because pickle only saves lists
            # we saved the object as a list.
    return Exec_list, Object_list
Exemplo n.º 8
0
        print "ReEstimated: ", mu_est2, kappa_est2

        if (kflag == 0):
            Xdata = copy.deepcopy(Xdata_k)
            kflag = 1
        else:
            Xdata = np.concatenate((Xdata, copy.deepcopy(Xdata_k)), axis=0)

################################################################
######## Or Load the same data as for HMM 3 sets ###############
###############################################################

HMM_data = 1
if (HMM_data):
    folder = "./HMM_data/"
    HMM_list = pkl.load_pickle(folder + "HMM_datapoints.pkl", 1)
    #gl.scatter_3D([0,1,1,1,1,-1,-1,-1,-1], [0,1,1,-1,-1,1,1,-1,-1],[0,1,-1,1,-1,1,-1,1,-1], nf = 1, na = 0)
    #    gl.scatter_3D(0, 0,0, nf = 1, na = 0)
    k = 0  # For the initial
    for Xdata_chain in HMM_list:

        Xdata_chain = np.array(Xdata_chain)
        #    print Xdata_k.shape
        #        gl.scatter_3D(Xdata_chain[:,0], Xdata_chain[:,1],Xdata_chain[:,2], nf = 0, na = 0, color = "k")
        if (k == 0):
            Xdata = copy.deepcopy(Xdata_chain)
        else:
            Xdata = np.concatenate((Xdata, copy.deepcopy(Xdata_chain)), axis=0)
        k = 1

################################################################
Exemplo n.º 9
0
import pickle_lib as pklib
import CDBSCAN as CDBSCAN

problem_indx = 4
files_list = ["./data/data_10points_10dims.dat",
              "./data/data_100points_100dims.dat",
              "./data/data_1000points_1000dims.dat",
              "./data/data_10000points_10000dims.dat",
              "./data/data_100000points_100000dims.dat"]

eps_list = [0.4, 0.3, 0.15, 0.15, 0.15]
data = pklib.load_pickle (files_list[problem_indx],verbose = 1);

myDBSCAN = CDBSCAN.CDBSCAN(eps=eps_list[problem_indx], MinPts=2, reuse_computed = 0)
myDBSCAN.fit(data)

#print myDBSCAN.Already_Calculated_Regions[1]
print "Number of clusters " + str(myDBSCAN.K + 1)
myDBSCAN.print_clusters_sizes()

# 28470, 28121, 9, 19, 5, 13, 28412, 8, 2, 17, 8, 15, 16, 16, 16, 14, 14, 6, 13, 7, 21, 15, 15, 21, 10, 13, 10, 8, 12, 25, 13, 21
Exemplo n.º 10
0
                    np.array([ll_train_best[ic], ll_test_best[ic]]).T,
                    legend=["tr", "Val"],
                    labels=["EM class = " + str(ic), "States", "loglike"])
            gl.savefig(
                file_dir="./OnePerson_5fold_cluster" + str(ic) + "/Iteration" +
                str(i) + ".png",
                bbox_inches='tight',
                sizeInches=[],  # The size in inches as a list
                close=True,  # If we close the figure once saved
                dpi=100
            )  # Density of pixels !! Same image but more cuality ! Pixels

loading_precomputed_centroids = 1
if (loading_precomputed_centroids):
    #    pkl.store_pickle("./OnePerson1FoldEM.pkl",[ll_train_best, ll_test_best, All_Ks_params_best])
    cosas = pkl.load_pickle("./OnePerson1FoldEM.pkl")
    class_i = 1
    n_cluster_opt = 5
    good_clusters_EM = cosas[2][n_cluster_opt][class_i]
    Ks_params = good_clusters_EM
    pi_opt = good_clusters_EM[0]
    mu_opt = good_clusters_EM[1][0]

    Ks_params = cosas[2][n_cluster_opt]
#    cosas = pkl.load_pickle("./OnePerson1FoldHMM.pkl")
#    class_i = 1;
#    n_cluster_opt = 3;
#    good_clusters_EM = cosas[2][n_cluster_opt][class_i]
#    Ks_params_HMM = good_clusters_EM
#    pix_opt = good_clusters_EM[0]
#    A_opt = good_clusters_EM[1]
Exemplo n.º 11
0
               legend = 12, xticks = 12, yticks = 12)
     gl.savefig(folder_images +'GP_database_realizations.png', 
             dpi = 100, sizeInches = [2*6.5, 2*2])
 
     
 ########## Using Pickle ###############
 
 Ndivisions = 10;
 
 # Cannot use it due to incompatibilities Python 2 and 3
 pkl.store_pickle(folder_data +"X_values.pkl",X_list,Ndivisions)
 pkl.store_pickle(folder_data +"Y_values.pkl",Y_list,Ndivisions)
 pkl.store_pickle(folder_data +"t_values.pkl",t_list,Ndivisions)
 
 ## Test to load the files back 
 X_list2 = pkl.load_pickle(folder_data +"X_values.pkl",Ndivisions)
 Y_list2 = pkl.load_pickle(folder_data +"Y_values.pkl",Ndivisions)
 
 ###### Using Json ###############
 if (0):
     # Cannot put vectors as elements...
     import json
     data_dict = {"X":X_list, "Y": Y_list}
     
     
     with open(folder_data + 'data.json', 'w') as f:
          json.dump(data_dict, f)
     
     # Reading data back
     with open(folder_data + 'data.json', 'r') as f:
          data_dict_recovered = json.load(f)
Exemplo n.º 12
0
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)
Exemplo n.º 13
0
                                                    list_models_epoch_i[i],source_path = source_path)
    pickle_results_path_list.append(pickle_results_path)
    model_file_path_list.append(model_file_path)
    

"""
##################################################################
LOAD THE CONFIGURATION FILES
##################################################################
"""
dtype = torch.float
device = pytut.get_device_name(cuda_index = 0)
cf_a_list = []
training_logger_list = []
for i in range(Nmodels):
    [cf_a,training_logger] = pkl.load_pickle(pickle_results_path_list[i])
    ## Set data and device parameters
    cf_a.dtype = dtype  # Variable types
    cf_a.device = device
    cf_a.datareader_lazy = True # Force lazyness for RAM optimization
    cf_a.batch_size_train = 30
    cf_a.batch_size_validation = 30
    cf_a.max_instances_in_memory = 1000
    ## Fix backwards compatibility
    cf_a.phrase_layer_hidden_size = cf_a.modeling_span_end_hidden_size
    cf_a.phrase_layer_hidden_size = cf_a.modeling_span_end_hidden_size
    
    cf_a_list.append(cf_a)
    training_logger_list.append(training_logger)
    
Exemplo n.º 14
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")
Exemplo n.º 15
0
    Y = np.array(data_df.loc[:,'Target_clas']).reshape(-1,1)
    

## Normalize variables !!! 
from sklearn import preprocessing
"""
Load the model and the scalers
"""


folder_model = "../models/"
folder_predictions = "../predictions/"
key_classifier = "LSVM"  # QDA  # GNB RF

ul.create_folder_if_needed(folder_predictions)
scaler_X = pkl.load_pickle(folder_model + "scaler_X" +".pkl")[0]
scaler_Y = pkl.load_pickle(folder_model + "scaler_Y" +".pkl")[0]
classifier = pkl.load_pickle(folder_model + key_classifier +".pkl")[0]

X = scaler_X.transform(X)            
Y = scaler_Y.transform(Y)

Ypred = classifier.predict_proba(X)[:,1]
output_df = tut.get_output_df(dates_predictions,Ypred)

output_df = output_df[output_df.index > dt.datetime(2015,4,1)]
output_df.to_csv(folder_predictions + key_classifier + ".csv")

## Reload predictions for checking
loaded_predictions = pd.read_csv(folder_predictions + key_classifier + ".csv",  
                                 sep = ',', index_col = 0, header = 0)
Exemplo n.º 16
0
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")
Exemplo n.º 17
0
LDA_F = 0
LDAT_F = 0

######## CLUSTER PARAMETERS !!!!! ########
""" If we execute in da cluuster (spanish u) we overwritte the parameters """

N_epochs = 100
Learning_Rate = 0.01
BatchSize = 10

if ((Cluster_exec == 1) | (Console_exec == 1)):
    """ LIST OF POSSIBLE VALUES"""
    # Only 2 paramters !! The set of parameters to use and the repetition for the cluster.

    import pickle_lib as pkl
    param_list = pkl.load_pickle(output_folder + "BEST_params", 1)

    params = param_list[int(param[0])]
    Repetition = int(param[1])  # Number of the repetitions in the cluster

    ### SET THE VALUES ###
    nH = params[0]
    N_epochs = params[1]
    fo_name = params[2]

    Learning_Rate = params[3]
    BatchSize = params[4]

    Inyection = params[5]
    Enphasis = params[6]
Exemplo n.º 18
0
        print ("Model Type")
        print (model_type)
        config.prior_pi = global_prior_pi
        config.log_sigma1 = global_log_sigma1
        config.log_sigma2 = global_log_sigma2
    
        return config

if (load_data):

    print (model_type)
    ####### Global data reading #########
    Ndivisions = 10;
    folder_data = "./data/artificial/"
    
    X_list = pkl.load_pickle(folder_data +"X_values.pkl",Ndivisions)
    Y_list = pkl.load_pickle(folder_data +"Y_values.pkl",Ndivisions)
    t_list = pkl.load_pickle(folder_data +"t_values.pkl",Ndivisions)
    
    num_steps, X_dim = X_list[0].shape
    num_chains = len(X_list)
    
    
    ## Divide in train val and test
    proportion_tr = 0.8
    proportion_val = 0.1
    proportion_tst = 1 -( proportion_val + proportion_tr)
    
    num_tr = 10000
    num_val = 5000
    num_tst = 5000
plt.close("all")

folder = "./data/test_data/"
folder_HMM = "./data/HMM_data/"
folder_images = "../pics/Trapying/EM_HMM/"

## Flags
load_HMM_data = 1  # To load the data from the HMM structure
CV_EM = 1          # To perform the CV of the EM
CV_HMM = 0         # To perform the CV of the HMM
plot_combined_EM_HMM = 1 # To plot the data 
################################################################
######## Or Load the same data as for HMM 3 sets ###############
###############################################################
if (load_HMM_data):
    Chains_list = pkl.load_pickle(folder_HMM +"HMM_labels.pkl",1)
    HMM_list = pkl.load_pickle(folder_HMM +"HMM_datapoints.pkl",1)
    params = pkl.load_pickle(folder_HMM +"HMM_param.pkl",1)
    pi = params[0]
    A = params[1]
    pi_end = HMMlf.get_final_probabilities(pi,A,20)
    #print pi_end
    
    print "Real pi"
    print pi
    print "Real A"
    print A

#######################################################################################
######## Create the Distribution object ##############################################
#######################################################################################
Exemplo n.º 20
0
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")
Exemplo n.º 21
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)
Exemplo n.º 22
0
#################  GENERATE PARAMTERS SETS ##################

database_number = 2
beta_i = 0
alpha_i = 0

# Folder where we read the RAW files
folder = "/NeoEvo/ResultsNeoDSN" + str(database_number)
# Folder where we read and store the Preread files
base_folder_in = "../NeoEvo/PreRead/"
# Folder where we store the graph
base_folder_out = "../NeoEvo/Gold/"

results = pkl.load_pickle(
    base_folder_in + str(database_number) + "/" + "data_" + "_" +
    str(beta_indx_list[beta_i]) + "_" + str(alpha_indx_list[alpha_i]) + "_EVO",
    1)  # If the result file existis coz it was previously read

if (results == []):
    print "FILE NOT PREREAD"
    exit(-1)

All_Object_list = results

Nepoch = len(All_Object_list[0])
N_neurons = len(All_Object_list[0][0])

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 = []
Exemplo n.º 23
0
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")
pickle_results_path, model_file_path = pytut.get_models_paths(
    model_id, epoch_i=-1, source_path=source_path)
"""
############# Add the positibility of filtering by desired properties !!
"""
pickle_results_path = None
model_file_path = None

source_path = "../all_Bayesian/"
pickle_results_path, models_path = pytut.get_all_pickles_training(
    source_path, include_models=True)
Nmodels = len(pickle_results_path)
cf_a_list = []
training_logger_list = []
for i in range(Nmodels):
    [cf_a, training_logger] = pkl.load_pickle(pickle_results_path[i])
    ## Set data and device parameters
    cf_a_list.append(cf_a)
    training_logger_list.append(training_logger)

List_analyses = [
    "Layers DOr", "LSTMs hidden size", r"$\zeta$", "sigma2", "Run"
]
List_f_analyses = [
    return_layers_dropout, return_hidden_size, return_etaKL, return_sigma2,
    return_initialization_index
]

Standard_values_alyses = [0., 100, 0.001, 0.5, "whatever"]

N_analyses = len(List_analyses)
Exemplo n.º 25
0
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