keras tuner does not create checkpoint files

61 views Asked by At

I used keras tuner in direct approach in time series forecasting on VScode on windows11. I attempted to predict values for 7 days in the future, and created 7 models for each day.

I created a tuner 7 times in a for loop.

def slice_direct(i):
    # use input as it is
    # y[:, i] ... : = all parts of a batch.  i = prediction-period (forecast horizon)
    
    return lambda x, y: (x, y[:, i])

i=0

class MyHyperModel(keras_tuner.HyperModel):

    def build(self, hp):
        tuned_window_size = hp.Choice('window_size', [10,20,30])
        activation = 'tanh'
        model = tf.keras.models.Sequential()
        # Tune the number of hidden layers.
        second_hidden_layer = hp.Choice('second_hidden_layer?', [0, 1])
        third_hidden_layer = hp.Choice('third_hidden_layer?', [0, 1])

        if second_hidden_layer == 0:
            model.add(GRU(
                    units=hp.Int("units_first_layer", min_value=10, max_value=200, step=10),  # tuning the num of units
                    activation=activation,
                    input_shape=[tuned_window_size, feature_nums],  # use self.window_size here
                    return_sequences=False
                ))
        else:
            model.add(GRU(
                    units=hp.Int("units_first_layer", min_value=10, max_value=200, step=10),  # tuning the num of units
                    activation=activation,
                    input_shape=[tuned_window_size, feature_nums],  # use self.window_size here
                    return_sequences=True
                ))
            
            if third_hidden_layer == 0:
                model.add(GRU(
                        units=hp.Int("units_last_hidden", min_value=10, max_value=200, step=10),  # tuning the num of units
                        activation=activation,
                        return_sequences=False
                        ))
            else:
                model.add(GRU(
                        units=hp.Int("units_last_hidden", min_value=10, max_value=200, step=10),  # tuning the num of units
                        activation=activation,
                        return_sequences=True
                        ))
                model.add(GRU(
                            units=hp.Int("units_last_hidden", min_value=10, max_value=200, step=10),  # tuning the num of units
                            activation=activation,
                            return_sequences=False
                ))
        
       
        model.add(Dense(1))
  
        optimizer_name = hp.Choice('optimizer', ['adam', 'sgd'])
        if optimizer_name == 'adam':
            optimizer = keras.optimizers.Adam(learning_rate=0.001)
        elif optimizer_name == 'sgd':
            learning_rate = hp.Float("learning_rate", min_value=1e-5, max_value=1e-2, sampling="log")
            optimizer = keras.optimizers.SGD(learning_rate=learning_rate, momentum=0.9)
  
        
        model.compile(
            optimizer=optimizer,
            loss=hp.Choice('compiled_loss', ["mse",'mae']),
            metrics=[tf.keras.metrics.MeanAbsoluteError(), tf.keras.metrics.RootMeanSquaredError()],
        )
        
        return model
    
    def fit(self, hp, model, train_data, val_data, i=i, callbacks=None):
        # convert train_data into train_dataset
        tuned_window_size = hp.get('window_size')
        fd = Form_data(forecast_horizon, tuned_window_size, batch_size)
        train_x, train_y = fd.split_data_into_xy_multi_direct(train_data)
        val_x, val_y = fd.split_data_into_xy_multi_direct(val_data)
        train_dataset = fd.transform_npdataset_tfdataset(train_x, train_y)
        val_dataset = fd.transform_npdataset_tfdataset(val_x, val_y)
        model.fit(train_dataset.map(slice_direct(i)), epochs=50, verbose=0, validation_data=val_dataset.map(slice_direct(i)), callbacks=[early_stopping])
        
        # evaluate the performance for one of the prediction days
        predictions = []
        actuals = []
        for x, y in val_dataset.map(slice_direct(i)):
            prediction = model.predict(x, verbose=0) # (409, 7) slide windows * 409 times
            predictions.append(prediction)
            actuals.append(y.numpy())
        
        # Convert lists to numpy arrays
        predictions = np.concatenate(predictions)
        actuals = np.concatenate(actuals)

        # unscaling
        unscaled_predictions = np.copy(predictions)
        unscaled_actuals = np.copy(actuals)
        for i in range(predictions.shape[0]):
            unscaled_predictions[i] = scaler.inverse_transform(predictions[i].reshape(-1, 1)).flatten()
            unscaled_actuals[i] = scaler.inverse_transform(actuals[i].reshape(-1, 1)).flatten()
        mse = MSE(unscaled_actuals, unscaled_predictions)
       
        return mse



models = {}
hyperparameters = {}
for i in range(forecast_horizon):
    tuner = keras_tuner.BayesianOptimization(
            MyHyperModel(),
            objective=keras_tuner.Objective("fit", direction="min"),
            max_trials=5,
            overwrite=True,
            directory="hp_tuning",
            project_name='direct'
        )
    tuner.search_space_summary()
    with tf.device('/device:GPU:0'):
        tuner.search(train_data=scaled_train_data, val_data=scaled_val_data)

    best_model = tuner.get_best_models(num_models=5)[0]
    best_hyperparameters = tuner.get_best_hyperparameters(num_trials=1)[0]

    # 辞書に保存
    models[i] = best_model
    hyperparameters[i] = best_hyperparameters

and the error below occured. The folder 'hp_tuner' and 'direct' were created, checkpoint files were not.

NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for hp_direct\untitled_project\trial_2\checkpoint

0

There are 0 answers