<jupyter_start><jupyter_text>Dataset Preparation<jupyter_code>(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train/255.0, x_test/255.0<jupyter_output><empty_output><jupyter_text>Sherpa Setup<jupyter_code>parameters = [sherpa.Continuous('learning_rate', [1e-4, 1e-2]),
              sherpa.Discrete('num_units', [32, 128]),
              sherpa.Choice('activation', ['relu', 'tanh', 'sigmoid'])]
algorithm = bayesian_optimization.GPyOpt(max_num_trials=50)
study = sherpa.Study(parameters=parameters,
                     algorithm=algorithm,
                     lower_is_better=False)<jupyter_output><empty_output><jupyter_text>Hyperparameter Optimization<jupyter_code>epochs = 15

for trial in study:
    lr = trial.parameters['learning_rate']
    num_units = trial.parameters['num_units']
    act = trial.parameters['activation']

    # Create model
    model = Sequential([Flatten(input_shape=(28, 28)),
                        Dense(num_units, activation=act),
                        Dense(10, activation='softmax')])
    optimizer = Adam(lr=lr)
    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])

    # Train model
    for i in range(epochs):
        model.fit(x_train, y_train)
        loss, accuracy = model.evaluate(x_test, y_test)
        study.add_observation(trial=trial, iteration=i,
                              objective=accuracy,
                              context={'loss': loss})
        if study.should_trial_stop(trial):
            break 
    study.finalize(trial=trial)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12073170731707317, 'fraction_numerical': 0.016463414634146342, 'mean_word_length': 2.9927007299270074, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '383182', 'license': 'MIT', 'n_tokens_mistral': 559, 'n_tokens_neox': 518, 'n_words': 74, 'path': 'sherpa-seq-testing/examples/keras_mnist_mlp.ipynb', 'repo_name': 'LarsHH/reproducible-hyperparameter-optimization-jcgs-2019', 'size': '1640'}