Is there a way to store the results summary of keras tuner in a data frame?

Viewed 17

I am working with keras_tuner and I am really new in this. I created a class to iterate the input data that i needed, and I would like to explore more the hyperparameters than obtain "best hyperparameters". To do so, I would like to have access to the values in the tuner.results_summry() Is there any way to store this values in a data frame or to access them. When ever I tried, I can visualize them but when I assigned to a variable and I want to print the values is None.

class model ():
        
        def __init__(self,columns,window_size):
            self.columns = columns
            self.window_size = window_size
            
        def new_df(self):
            new_df =df_pr[self.columns]
            x = new_df.iloc[: , :-1]
            df_as_np = x.to_numpy()
            X = []
            for i in range(len(df_as_np)-self.window_size):
                row = [r for r in df_as_np[i:i+self.window_size]]
                X.append(row)
            y0 = np.array((new_df.iloc[:,-1]))
            y = np.delete(y0, np.s_[0:self.window_size], axis=0)
            return np.array(X), np.array(y)
        
        def reshape(self):
            x,y = self.new_df()
            x_train, x_test, y_train, y_test = 
            train_test_split(x,y,test_size = 0.1, shuffle = 
            False) 
            x_train = np.reshape(x_train, 
            (len(x_train),self.window_size*x_train.shape[-1]))
            x_test = np.reshape(x_test, 
            (len(x_test),self.window_size*x_test.shape[-1]))
            return x_train, x_test, y_train, y_test
            
        def build_model(self, hp):
            model = keras.Sequential()
            for i in range(hp.Int('num_layers', 1, 20)):
                model.add(layers.Dense(units=hp.Int('units_' + str(i),
                                                    min_value=32,
                                                    max_value=512,
                                                    step=64),
                                      activation='relu'))
            model.add(layers.Dense(1, activation='linear'))
            model.compile(
                optimizer=keras.optimizers.Adam(
                    hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
                loss='mse',
                metrics=[tf.keras.metrics.MeanSquaredError()])
            return model 
        
        def tuner(self):
            
            x_train, x_test, y_train, y_test=self.reshape()
            tuner = keras_tuner.RandomSearch(
            self.build_model,
            objective=keras_tuner.Objective("val_mean_squared_error", 
            direction="min"),
            max_trials=5,
            executions_per_trial=1,
            overwrite = True,
            directory='my_dir',
            project_name='Hypertuning_MLP2')   
            tuner.search(x_train,y_train, epochs=1, validation_data= 
            (x_test,y_test))
            sum_hps =tuner.results_summary(num_trials = 3)
            best_hps = tuner.get_best_hyperparameters(num_trials = 5) 
            [0]
            
            return tuner, best_hps, sum_hps
        
    well = 'pozo1'
    window_size = [7,14,]
    columns = { 0: ['Precipitation', f'{well}'],} 
                        1: ['Temperature', f'{well}'],}
            
    for j in columns: 
    print(columns[j])
        for i in window_size:
        print(i)
        demo = model(columns[j],window_size=i)
        tuner, best_hps, sum_hps  = demo.tuner()

From this I obtained the summary as a summary of the run process, but the variable that I assigned to tuner.results_summary () is empty and I cannot access any value to create a dictionary or a dataframe. I attach an image of the results.

enter image description here

thank you again, sorry if there are mistakes, as I said I am pretty new in this topic.

0 Answers
Related