Esempio n. 1
0
def tune_with_kerastuner1():
    photos = load('dogs_vs_cats_photos.npy')
    labels = load('dogs_vs_cats_labels.npy')
    (trainX, testX, trainY, testY) = train_test_split(photos,
                                                      labels,
                                                      test_size=0.25,
                                                      random_state=42)

    trainY = keras.utils.to_categorical(trainY, 2)
    testY = keras.utils.to_categorical(testY, 2)

    model = define_three_block_model()

    # history = model.fit(photos, labels, batch_size=16, epochs=10, validation_split=0.33, verbose=1, use_multiprocessing=True)

    tuner = RandomSearch(model,
                         objective='val_accuracy',
                         max_trials=5,
                         executions_per_trial=3,
                         directory="tuner_dir",
                         project_name="cats_vs_dogs_tuner")
    tuner.search_space_summary()

    # tuner.search(trainX, trainY,
    #              epochs=5,
    #              validation_data=(testX, testY))

    models = tuner.get_best_models(num_models=2)
    tuner.results_summary()
    return tuner
def search(dt=600,
           window_size=360,
           future_steps=144,
           epochs=50,
           with_time=True,
           batch_size=128,
           max_trials=200):
    bathroom1 = Dataset.parse('dataset/', 'bathroom1')
    kitchen1 = Dataset.parse('dataset/', 'kitchen1')
    combined1 = bathroom1.combine(kitchen1)

    X, y = prepare_data_future_steps(combined1,
                                     window_size=window_size,
                                     dt=dt,
                                     with_time=with_time,
                                     future_steps=future_steps)
    X_train = X[:-4 * (3600 // dt) * 24, :, :]
    X_val = X[-4 * (3600 // dt) * 24:-2 * (3600 // dt) * 24, :, :]
    X_test = X[-2 * (3600 // dt) * 24:, :, :]

    # For now only sensor 24
    y_train = y[:-4 * (3600 // dt) * 24, :, 0]
    y_val = y[-4 * (3600 // dt) * 24:-2 * (3600 // dt) * 24, :, 0]
    y_test = y[-2 * (3600 // dt) * 24:, :, 0]

    tuner = RandomSearch(FuturePredictionModelHyperparameters(
        window_size=window_size,
        num_features=X.shape[2],
        future_steps=future_steps),
                         objective='val_loss',
                         max_trials=max_trials,
                         directory='test_dir')

    tuner.search_space_summary()

    tuner.search(x=X_train,
                 y=y_train,
                 epochs=epochs,
                 batch_size=batch_size,
                 validation_data=(X_val, y_val),
                 callbacks=[IsNanEarlyStopper(monitor='loss')])

    tuner.results_summary()
Esempio n. 3
0
    # Build model
    model = keras.Model(inputs, outputs)
    model.compile(optimizer=Adam(lr),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model


# Initialize the tuner by passing the `build_model` function
# and specifying key search constraints: maximize val_acc (objective),
# and the number of trials to do. More efficient tuners like UltraBand() can
# be used.
tuner = RandomSearch(build_model, objective='val_accuracy', max_trials=TRIALS,
                     project_name='hello_world_tutorial_results')

# Display search space overview
tuner.search_space_summary()

# Perform the model search. The search function has the same signature
# as `model.fit()`.
tuner.search(x_train, y_train, batch_size=128, epochs=EPOCHS,
             validation_data=(x_val, y_val))

# Display the best models, their hyperparameters, and the resulting metrics.
tuner.results_summary()

# Retrieve the best model and display its architecture
best_model = tuner.get_best_models(num_models=1)[0]
best_model.summary()
Esempio n. 4
0
def hyper_tuner(month_path, log_path, time_step, train_data, valid_data,
                test_data, item, item_index, item_name):

    train_x, valid_x, test_x, train_y, valid_y, test_y = data_split_norm(
        train_data, valid_data, test_data, item_index, time_step)

    hyper_model = MyHyperModel(input_shape=train_x.shape[-2:])

    project_name = item_name + "_" + str(time_step)

    tuner = RandomSearch(hyper_model,
                         objective='val_loss',
                         max_trials=150,
                         executions_per_trial=5,
                         directory=log_path,
                         project_name=project_name)

    callbacks = [
        tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                         min_delta=1e-3,
                                         patience=15,
                                         restore_best_weights=True),
        tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                             factor=0.1,
                                             patience=5,
                                             verbose=0,
                                             mode='auto',
                                             min_delta=0.0001),
        ClearTrainingOutput()
    ]

    tuner.search_space_summary()

    tuner.search(train_x,
                 train_y,
                 epochs=200,
                 batch_size=256,
                 validation_data=(valid_x, valid_y),
                 callbacks=callbacks,
                 verbose=2)

    # Get the optimal hyperparameters
    best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]

    model = tuner.hypermodel.build(best_hps)
    history = model.fit(train_x,
                        train_y,
                        epochs=200,
                        batch_size=256,
                        validation_data=(valid_x, valid_y),
                        callbacks=callbacks,
                        verbose=2)
    models_path = os.path.join(month_path, "models")
    if not os.path.exists(models_path):
        os.mkdir(models_path)
    model_name = item + "_model.h5"
    model_path = os.path.join(models_path, model_name)
    model.save(model_path)
    # 清除tuner
    del tuner

    layers_path = os.path.join(month_path, item)
    if not os.path.exists(layers_path):
        os.mkdir(layers_path)
    plot_path_1 = os.path.join(layers_path, "loss_epoch.png")
    plot_path_2 = os.path.join(layers_path, "measure_predict.png")
    csv_path = os.path.join(layers_path, "measure_predict.csv")

    history = history.history

    plt.plot(history['loss'], linewidth=2, label='Train')
    plt.plot(history['val_loss'], linewidth=2, label='Test')
    plt.legend(loc='upper right')
    plt.ylabel('Mean Relative Error [$' + item + '$]')
    plt.xlabel('Epoch')
    plt.savefig(plot_path_1, dpi=300, bbox_inches="tight")
    plt.close()

    test_predictions = model.predict(test_x)
    plt.figure(figsize=(20, 6))
    plt.plot(test_y, label='measure')
    plt.plot(test_predictions, label='predict')
    plt.legend(loc='upper right')
    if item is 'PH':
        plt.ylabel(item)
    else:
        plt.ylabel(item + '(mg/L)')
    plt.xlabel('Test set')
    plt.savefig(plot_path_2, dpi=300, bbox_inches="tight")
    plt.close()

    test_predictions = test_predictions.flatten()  # flatten()降为一维
    valid_predictions = model.predict(valid_x).flatten()

    train_fvu, train_rmse, train_mre = history['loss'][-1], history[
        'root_mean_squared_error'][-1], history[
            "mean_absolute_percentage_error"][-1]

    valid_fvu, valid_rmse, valid_mre = model.evaluate(valid_x,
                                                      valid_y,
                                                      verbose=0)
    valid_nse, valid_cc = calc_nse_cc(valid_y, valid_predictions)
    test_fvu, test_rmse, test_mre = model.evaluate(test_x, test_y, verbose=0)

    test_nse, test_cc = calc_nse_cc(test_y, test_predictions)

    measure_predict_data = pd.DataFrame()
    measure_predict_data['predict'] = test_predictions
    measure_predict_data['measure'] = test_y
    measure_predict_data.to_csv(csv_path, encoding='utf-8')
    try:
        hidden_layer_1 = best_hps.get('units_0')
    except:
        hidden_layer_1 = 0
    try:
        hidden_layer_2 = best_hps.get('units_1')
    except:
        hidden_layer_2 = 0

    model_info = {
        'output': item,
        "time_step": time_step,
        'hidden_layer_1': hidden_layer_1,
        'hidden_layer_2': hidden_layer_2,
        'hidden_layer_3': best_hps.get('units_n'),
        'train_fvu': train_fvu,
        'train_rmse': train_rmse,
        'train_mre': train_mre,
        "valid_fvu": valid_fvu,
        'valid_nse': valid_nse,
        'valid_rmse': valid_rmse,
        'valid_mre': valid_mre,
        'valid_cc': valid_cc,
        "test_fvu": test_fvu,
        'test_nse': test_nse,
        'test_rmse': test_rmse,
        'test_mre': test_mre,
        'test_cc': test_cc
    }
    return model_info
Esempio n. 5
0
def hyper_tuner(log_path, time_step, train_data, valid_data, test_data, item,
                item_index, item_name):
    keras.backend.clear_session()

    train_x, valid_x, test_x, train_y, valid_y, test_y = data_split_norm(
        train_data, valid_data, test_data, item_index, time_step)

    hyper_model = MyHyperModel(input_shape=train_x.shape[-2:])

    project_name = item_name + "_" + str(time_step)

    tuner = RandomSearch(hyper_model,
                         objective='val_loss',
                         max_trials=150,
                         executions_per_trial=10,
                         directory=log_path,
                         project_name=project_name)

    callbacks = [
        tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                         min_delta=1e-3,
                                         patience=15,
                                         restore_best_weights=True),
        tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
                                             factor=0.1,
                                             patience=5,
                                             verbose=0,
                                             mode='auto',
                                             min_delta=0.0001),
        ClearTrainingOutput()
    ]

    tuner.search_space_summary()

    tuner.search(train_x,
                 train_y,
                 epochs=200,
                 batch_size=256,
                 validation_data=(valid_x, valid_y),
                 callbacks=callbacks,
                 verbose=2)

    # Get the optimal hyperparameters
    best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]

    try:
        hidden_layer_1 = best_hps.get('units_0')
    except:
        hidden_layer_1 = 0
    try:
        hidden_layer_2 = best_hps.get('units_1')
    except:
        hidden_layer_2 = 0

    model_info = {
        'output': item,
        "time_step": time_step,
        'hidden_layer_1': hidden_layer_1,
        'hidden_layer_2': hidden_layer_2,
        'hidden_layer_3': best_hps.get('units_n'),
    }

    # 清除tuner
    del tuner

    return model_info
Esempio n. 6
0
        model.add(layers.Dropout(rate=0.5))
        model.add(layers.Dense(6, activation='softmax'))

        model.compile(optimizer=Optimizer.Adam(learning_rate=hp.Choice(
            'learning_rate', values=[1e-2, 1e-3, 1e-4])),
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])

        return model


# Initialize the model
hypermodel = RegressionHyperModel()

# Run the random search
tuner_rs = RandomSearch(hypermodel,
                        objective='val_accuracy',
                        max_trials=5,
                        executions_per_trial=1)

tuner_rs.search_space_summary()
tuner_rs.search(train_ds, train_classes, epochs=15, validation_split=0.30)

# Get the best model
best_model = tuner_rs.get_best_models(num_models=1)[0]

# Evaluate
val_ds, val_classes = getImages(
    '../input/intel-image-classification/seg_test/seg_test/', 150)
best_model.evaluate(val_ds, val_classes, verbose=1)
Esempio n. 7
0
    df = one_hot_encode(df, colnames=ohe_cols)

    x_train, x_val, y_train, y_val, train_ids, val_ids = split_dataset(
        df, test_size=0.1, seed=42)
    # X_train, Y_train = np.array(x_train), np.array(y_train)
    # X_val, Y_val = np.array(x_val), np.array(y_val)

    model = get_model(input_size=118, output_size=2, magic='tanh', dropout=0.5)
    tuner = RandomSearch(get_tuned_model,
                         objective='val_binary_accuracy',
                         max_trials=7,
                         executions_per_trial=4,
                         directory='project',
                         project_name='Air Quality Index')

    print(tuner.search_space_summary())

    x_train = np.asarray(x_train).astype(np.float32)
    y_train = np.asarray(y_train).astype(np.float32)
    x_val = np.asarray(x_val).astype(np.float32)
    y_val = np.asarray(y_val).astype(np.float32)

    tuner.search(x_train, y_train, epochs=5, validation_data=(x_val, y_val))

    print(tuner.results_summary(num_trials=3))  # 3 best models

    test_acc, test_loss = fit_and_evaluate(model,
                                           x_train,
                                           y_train,
                                           x_val,
                                           y_val,