Пример #1
0
def hyperparam_optimization( x, y ) :
    nBits = x.shape[1]
    frac_test = 0.3
    x_train, x_test, y_train, y_test = train_test_split( x, y, test_size=frac_test )

    TRAIN_EPOCHS = 20
    MAX_TRIALS=20
    EXECUTIONS_PER_TRIAL = 5

    b_tuner = BayesianOptimization(
        make_optimizer_model(nBits),
        objective = 'val_mean_squared_error',
        max_trials = MAX_TRIALS,
        executions_per_trial=EXECUTIONS_PER_TRIAL,
        directory='test_dir',
        project_name='tune_optimizer',
        seed=1
    )
    b_tuner.search_space_summary()

    b_tuner.search( x=x_train, y=y_train, epochs=TRAIN_EPOCHS, validation_data=(x_test, y_test))
    b_tuner.results_summary()
    best_model = b_tuner.get_best_models()[0]

    return best_model
Пример #2
0
def predict_rem(col_name):
    print(
        'Creating a best model for predicting {} MU and Checking its accuracy with test data'
        .format(col_name))
    import pandas as pd
    import warnings

    warnings.filterwarnings("ignore")

    df3 = pd.read_csv('pycharm_data.csv')
    df3 = df3.drop(columns=['Unnamed: 0', 'date'])
    new_df = df3[col_name].copy()
    new_df

    # create a differenced series
    from pandas import Series

    def difference(dataset, interval=1):
        diff = list()
        for i in range(interval, len(dataset)):
            value = dataset[i] - dataset[i - interval]
            diff.append(value)
        return Series(diff)

    differenced = difference(new_df, 1)
    differenced.head()

    X = []
    y = []
    for i in range(0, differenced.shape[0] - 48):
        X.append(
            differenced.iloc[i:i +
                             48])  # taking 48 rows for training incrementally
        y.append(differenced.iloc[i + 48])

    import numpy as np
    X, y = np.array(X, dtype=np.float32), np.array(y, dtype=np.float32)
    y = np.reshape(y, (len(y), 1))

    X_train, X_test = X[:-480], X[-480:]
    y_train, y_test = y[:-480], y[-480:]

    for i in range(len(y_test)):
        y_test_diff = y_test[i] + new_df[1786 + i]

    X_train = np.reshape(
        X_train,
        (X_train.shape[0], X_train.shape[1], 1))  # reshaping the data to 3d
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

    import warnings
    warnings.filterwarnings("ignore")
    import keras
    from keras.layers import LSTM, Activation, Dense
    from keras import optimizers
    from keras.callbacks import ModelCheckpoint, EarlyStopping
    from keras.layers import ReLU, LeakyReLU
    import h5py
    from keras.models import load_model
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import Dropout
    from keras.layers import LSTM
    from kerastuner.tuners import BayesianOptimization

    def build_model(hp):
        for i in range(hp.Int('num_layers', 2, 5)):
            model = keras.Sequential()
            model.add(
                LSTM(units=hp.Int(
                    'units_' + str(i), min_value=32, max_value=300, step=32)))
            model.add(ReLU())
            model.add(
                Dropout(rate=hp.Float('dropout_1',
                                      min_value=0.1,
                                      max_value=0.5,
                                      default=0.1,
                                      step=0.05)))
            model.add(Dense(1, activation='linear'))
            model.compile(optimizer=keras.optimizers.Adam(
                hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
                          loss='mean_absolute_error',
                          metrics=['mse'])
        return model

    bayesian_opt_tuner = BayesianOptimization(build_model,
                                              objective='mse',
                                              max_trials=3,
                                              executions_per_trial=1,
                                              overwrite=True)

    bayesian_opt_tuner.search(X_train,
                              y_train,
                              epochs=1000,
                              validation_data=(X_test, y_test),
                              callbacks=[
                                  keras.callbacks.EarlyStopping(
                                      monitor='val_loss', patience=100)
                              ],
                              verbose=0,
                              batch_size=5)

    best_model = bayesian_opt_tuner.get_best_models(num_models=1)
    model = best_model[0]

    predictions = model.predict(X_test)
    final_pred = []
    for i in range(len(predictions)):
        y_test_diff = predictions[i] + new_df[1786 + i]
        final_pred.append(y_test_diff[0])
    final_pred

    y_test_orig = []
    for i in range(len(y_test)):
        y_test_diff = y_test[i] + new_df[1786 + i]
        y_test_orig.append(y_test_diff[0])
    y_test_orig

    total = 0
    for i, j in zip(y_test_orig, final_pred):
        value = abs(i - j) / abs(i)
        total += value
    error = float(total * 100 / (len(y_test_orig)))  # calculate mape
    mape = round(error, 1)  # round to 3 significant figures
    accuracy = 100 - mape  # Calculate accuracy

    import matplotlib.pyplot as plt
    print("The LSTM's accuracy in predicting the mega units on test data : " +
          str(accuracy) + "%")
    print(' ')
    df = pd.DataFrame(y_test_orig, final_pred).reset_index()
    df.columns = ['original', 'predicted']
    df.plot(title='Original data vs Predicted data (for test data)')
    plt.show()
    model.save('models_with_tuner\\' + col_name + '_model_75.h5')
    print(' ')
    print(
        '================================================================================'
    )

    print('')
    print('Now training the model with 100% data for future predictions....')
    X_train = X
    y_train = y
    X_train = np.reshape(
        X_train,
        (X_train.shape[0], X_train.shape[1], 1))  # reshaping the data to 3d
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

    callbacks = [EarlyStopping(monitor='val_loss', patience=100)]

    model.fit(X_train,
              y_train,
              validation_split=0.2,
              epochs=1000,
              callbacks=callbacks,
              batch_size=5,
              shuffle=False)
    model.save('models_with_tuner\\' + col_name + '_model_100.h5')
    print(' ')
    print('saved the model successfully')
Пример #3
0
        max_trials=TOTAL_TRIALS,
        objective=kerastuner.Objective("val_auc", direction="max"),
        executions_per_trial=EXECUTION_PER_TRIAL,
        directory=base_dir,
        project_name=exp_name
    )

    history = tuner.search(train_gen, 
                           epochs=EPOCHS,
                           validation_data=val_gen, 
                            callbacks = [es, cp],
                            verbose =2,
                            use_multiprocessing=False)

    # Save best model and weight
    best_model = tuner.get_best_models()[0]
    best_config = best_model.optimizer.get_config()

    best_hyperparameters = tuner.get_best_hyperparameters()[0].get_config()
    best_hyperparameters_values = tuner.get_best_hyperparameters()[0].values

    best_model.save(model_dir)
    best_model.save_weights(weight_dir)

    with open(os.path.join(param_dir, 'hyperparameters.txt'), "w") as text_file:
        text_file.write(str(best_hyperparameters))

    pickle.dump(best_hyperparameters_values, open(os.path.join(param_dir,'hyperparameters.pickle'), 'wb'))
    print('Done')

Пример #4
0
                     default=1e-3)),
                      loss='mse',
                      metrics=['mse'])
        return model


hypermodel = RGModel(n_hidden=2)

HYPERBAND_MAX_EPOCHS = 40
MAX_TRIALS = 40
EXECUTION_PER_TRIAL = 4

tuner = BayesianOptimization(hypermodel,
                             objective='val_mean_squared_error',
                             seed=1,
                             max_trials=MAX_TRIALS,
                             executions_per_trial=EXECUTION_PER_TRIAL,
                             directory='random_search',
                             project_name='RGBFV8')

print(tuner.search_space_summary())

N_EPOCH_SEARCH = 10
# train_generator, steps_per_epoch=200, epochs=60, validation_data=validation_generator
tuner.search(train_gen_bf, epochs=N_EPOCH_SEARCH, validation_data=valid_gen_bf)

print(tuner.results_summary())

best_model = tuner.get_best_models(num_models=1)[0]
best_model.save('/DFS-L/DATA/pritchard/ankitesg/models/BFv12.h5')